英文:
NODE_ENV environment variable in Github action is not changed in separated jobs
问题
我已将Github操作的工作分为两个部分。
一个是用于设置GITHUB_ENV变量的,命名为set-env-variables
,另一个用于部署,命名为deploy
。
然而,除了"NODE_ENV"之外的每个变量在deploy
工作中都完美设置,正如我所期望的那样,而"NODE_ENV"仍然保持为默认值。
以下是我的简化工作流程文件。
name: Deploy
on:
push:
branches:
- main
- staging
defaults:
run:
shell: bash
env:
NODE_ENV: development
DEPLOY_ENV: development
jobs:
set-env-variables:
runs-on: ubuntu-latest
steps:
- name: 'Check Pushed Branch'
run: |
BRANCH=$(echo ${GITHUB_REF##*/})
if [ ${BRANCH} = staging ]; then
echo "DEPLOY_ENV=staging" >> $GITHUB_ENV
echo "NODE_ENV=staging" >> $GITHUB_ENV
fi
deploy:
runs-on: ubuntu-latest
steps:
- name: 'Deploy by environment'
run: |
echo $NODE_ENV
echo $DEPLOY_ENV
# 做一些部署工作...
当推送到staging
分支时,deploy
工作中的Deploy by environment
步骤显示以下结果。
development
staging
我不知道为什么"NODE_ENV"没有更改,但其他每个变量都已成功更改。
英文:
I've separated Github action jobs in two.
One is for setting GITHUB_ENV variables, named set-env-variables
and another is for deployment, named deploy
.
However every variable except "NODE_ENV" is set perfectly in deploy
job as I intended, and "NODE_ENV" is remained to be a default value.
Here is my simplified workflow file.
name: Deploy
on:
push:
branches:
- main
- staging
defaults:
run:
shell: bash
env:
NODE_ENV: development
DEPLOY_ENV: development
jobs:
set-env-variables:
runs-on: ubuntu-latest
steps:
- name: 'Check Pushed Branch'
run: |
BRANCH=$(echo ${GITHUB_REF##*/})
if [ ${BRANCH} = staging ]; then
echo "DEPLOY_ENV=staging" >> $GITHUB_ENV
echo "NODE_ENV=staging" >> $GITHUB_ENV
fi
deploy:
runs-on: ubuntu-latest
steps:
- name: 'Deploy by environment'
run: |
echo $NODE_ENV
echo $DEPLOY_ENV
# do some deploying jobs...
when pushing to staging
branch, Deploy by environment
step in deploy
job shows the result below.
development
staging
I have no idea why NODE_ENV hasn't changed, but every other variables have changed well.
答案1
得分: 1
Mind the gap:
> 通过定义或更新环境变量并将其写入 GITHUB_ENV
环境文件,您可以使环境变量在工作流作业的后续步骤中可用。
(来源:设置环境变量)
您有一个不同的作业。
请使用 GITHUB_OUTPUT
代替:
> 设置步骤的输出参数。请注意,步骤需要定义一个 id
以后能够检索输出值。
(来源:设置输出参数)
-
- name: 设置颜色 id: 随机颜色生成器 run: echo "SELECTED_COLOR=green" >> "$GITHUB_OUTPUT" - name: 获取颜色 run: echo "所选颜色是 ${{ steps.random-color-generator.outputs.SELECTED_COLOR }}"
-
作业: 作业1: 运行在: ubuntu-latest # 将步骤输出映射到作业输出 输出: 输出1: ${{ steps.step1.outputs.test }} 输出2: ${{ steps.step2.outputs.test }} 步骤: - id: 步骤1 run: echo "test=hello" >> "$GITHUB_OUTPUT" - id: 步骤2 run: echo "test=world" >> "$GITHUB_OUTPUT" 作业2: 运行在: ubuntu-latest 需要: 作业1 步骤: - env: 输出1: ${{needs.job1.outputs.output1}} 输出2: ${{needs.job1.outputs.output2}} run: echo "$OUTPUT1 $OUTPUT2"
参考:GitHub Action - 在作业之间定义工作流级别环境变量
英文:
Mind the gap:
> You can make an environment variable available to any subsequent steps in a workflow job by defining or updating the environment variable and writing this to the GITHUB_ENV
environment file.
(from : Setting an environment variable)
You have a different job.
Use GITHUB_OUTPUT
instead:
> Sets a step's output parameter. Note that the step will need an id
to be defined to later retrieve the output value.
(from: Setting an output parameter)
-
Example of setting an output parameter
- name: Set color id: random-color-generator run: echo "SELECTED_COLOR=green" >> "$GITHUB_OUTPUT" - name: Get color run: echo "The selected color is ${{ steps.random-color-generator.outputs.SELECTED_COLOR }}"
-
Defining outputs for jobs - Create a map of outputs for your jobs.
-
Example: Defining outputs for a job
jobs: job1: runs-on: ubuntu-latest # Map a step output to a job output outputs: output1: ${{ steps.step1.outputs.test }} output2: ${{ steps.step2.outputs.test }} steps: - id: step1 run: echo "test=hello" >> "$GITHUB_OUTPUT" - id: step2 run: echo "test=world" >> "$GITHUB_OUTPUT" job2: runs-on: ubuntu-latest needs: job1 steps: - env: OUTPUT1: ${{needs.job1.outputs.output1}} OUTPUT2: ${{needs.job1.outputs.output2}} run: echo "$OUTPUT1 $OUTPUT2"
Cf. GitHub Action - Define Workflow Level Environment Variable Between Jobs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论