本文介绍了使用github操作进行部署时如何在node.js进程中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用github动作为我的node.js服务器构建CI管道。

I am trying to build an CI pipeline for my node.js server using github actions.

我只需要解决一个问题。
我需要设置环境变量,以便我的node.js服务器可以通过 process.env

I just need to solve one issue.I need to set environment variable, so that my node.js server can access the env variable via process.env

下面是github操作工作流文件。

Below is the github action workflow file.

name: Build and Deploy to GKE

on:
  pull_request:
    branches:
      - master

# Environment variables available to all jobs and steps in this workflow
env:
  ENGINE_API_KEY: ${{ secrets.ENGINE_API_KEY }}

jobs:
  setup-build-publish-deploy:
    name: Setup, Build, Publish, and Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Apollo Schema Update
        env:
          ENGINE_API_KEY: ${{ secrets.ENGINE_API_KEY }}
        run: |
          sudo npm install
          sudo npm install -g apollo
          sudo npm run dev &
          sleep 3
          sudo apollo service:push --serviceURL=http://auth-cluster-ip-service --serviceName=auth --tag=master --endpoint=http://localhost:3051

我尝试声明环境变量,包括工作流级别和作业级别,但是当我 console.log(process.env.ENGINE_API_KEY),它返回 undefined

I have tried declaring environment variable both workflow level and job's level, but when I console.log(process.env.ENGINE_API_KEY), it returns undefined.

我还尝试了 ENGINE_API_KEY = $ ENGINE_API_KEY npm run dev& 而不是 npm run dev& 。这适用于我的Macbook,但是通过github操作,它仍然返回 undefined

I also tried ENGINE_API_KEY=$ENGINE_API_KEY npm run dev & instead of npm run dev &. This works on my macbook, but with github action, it still returns undefined.

(我确实将ENGINE_API_KEY存储在设置->秘密。对于其他变量,效果很好)

(I did store ENGINE_API_KEY in settings -> secret. worked fine for other variables)

推荐答案

创建 .env 文件可以由您的节点服务器读取,并以这种方式传递您的存储库密码。这应该在结帐步骤之后完成:

Create an .env file that can be read by your node server and pass in your repository secret that way. This should be done after your checkout step:

    - name: create env file
      run: |
        touch .env
        echo ENGINE_API_KEY =${{ secrets.ENGINE_API_KEY }} >> .env

这篇关于使用github操作进行部署时如何在node.js进程中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 07:55