使用 github 操作部署时如何在 node.js 进程中设置环境变量 [英] How to set environment variable in node.js process when deploying with github action
问题描述
我正在尝试使用 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 进程中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!