NODE_ENV环境变量在GitHub Action的分开的任务中没有改变。

huangapple go评论101阅读模式
英文:

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"
    
  • needs 上下文

  • 使用 needs 上下文的示例

参考: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"
    
  • needs context

  • Example usage of the needs context

Cf. GitHub Action - Define Workflow Level Environment Variable Between Jobs

huangapple
  • 本文由 发表于 2023年5月25日 19:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定