GitHub Actions - 定义作业之间的工作流级别环境变量

问题描述 投票:0回答:5

我想在我的 Github Actions Workflow 内的作业之间定义和设置环境变量。 下面的工作流程是我尝试过的,但不幸的是环境变量 GIT_PR_SHA_SHORTE2E_GIT_PR_SHA 不起作用。

可以吗?

name: Git Pull Request Workflow

on:
  workflow_dispatch:
  pull_request:
    branches:
      - master

env:
  GIT_PR_SHA: ${{github.event.pull_request.head.sha}}
  GIT_PR_SHA_SHORT: "${{ env.GIT_PR_SHA:0:10 }}"
  ENV_NAME: test
  E2E_GIT_PR_SHA: "${{ env.ENV_NAME }}-${{ env.GIT_PR_SHA_SHORT }}"

jobs:
  first-job:
    name: Build Docker Image
    runs-on: ubuntu-latest

    steps:
    - name: First Echo Step
      run: |
            echo "GIT_PR_SHA_SHORT = ${GIT_PR_SHA_SHORT}"
            echo "E2E_GIT_PR_SHA = ${E2E_GIT_PR_SHA}"

  second-job:
    name: Build Docker Image
    runs-on: ubuntu-latest

    steps:
    - name: Second Echo Step
      run: |
            echo "GIT_PR_SHA_SHORT = ${GIT_PR_SHA_SHORT}"
            echo "E2E_GIT_PR_SHA = ${E2E_GIT_PR_SHA}"
github-actions
5个回答
52
投票

您使用 ${{ env.VARIABLE_NAME }} 而不是

${VARIABLE_NAME}
引用
工作流程的
环境变量。 后者是 bash 语法,但这些不是 shell 环境变量,它们是 workflow 环境变量。 它们是工作流程执行的一部分,而不是 shell 上下文的一部分。

要引用工作流环境变量:

name: Git Pull Request Workflow

on:
  workflow_dispatch:
  pull_request:
    branches:
      - master

env:
  one: 1
  two: zwei
  three: tres

jobs:
  first-job:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "${{ env.one }}"
        echo "${{ env.two }}"
        echo "${{ env.three }}"

(我喜欢使用

lower-case
作为我的工作流程环境变量,使用
UPPER_CASE
作为我的 shell 环境变量,这样我就更清楚哪个是哪个。)

同样,这也行不通:

env:
  GIT_PR_SHA_SHORT: "${{ env.GIT_PR_SHA:0:10 }}"

这是混合 bash 语法

:0:10
与工作流语法,但工作流变量不通过任何 shell 运行。 解析工作流文件时尚未启动虚拟机,因此没有 shell 来运行。

如果您想使用 bash 表达式来操作环境,则需要创建一个运行 bash 的步骤来执行此操作,并且需要使用

::set-env
::set-output
语法。

然后您可以使用

step
上下文引用
${{ steps... }}
的输出。

不幸的是,在“不同作业”之间传递东西比较棘手,因为它们运行在不同的虚拟机上。 您需要在整个工作流程本身上设置变量。 您需要首先

::set-output 使其对 job

 可见,然后您可以提高从作业到工作流程的可见性。
name: Demonstration on: push: branches: [master] jobs: first-job: runs-on: ubuntu-latest steps: - id: identify run: | # use bash variable expression to get the substring export GIT_PR_SHA="${{ github.sha }}" export GIT_PR_SHA_SHORT="${GIT_PR_SHA:0:10}" echo "git_pr_sha=${GIT_PR_SHA}" >> $GITHUB_OUTPUT echo "git_pr_sha_short=${GIT_PR_SHA_SHORT}" >> $GITHUB_OUTPUT outputs: git_pr_sha: ${{ steps.identify.outputs.git_pr_sha }} git_pr_sha_short: ${{ steps.identify.outputs.git_pr_sha_short }} second-job: needs: first-job runs-on: ubuntu-latest steps: - run: | echo "${{ needs.first-job.outputs.git_pr_sha }}" echo "${{ needs.first-job.outputs.git_pr_sha_short }}"

我想为此添加一个扩展,因为我在寻找如何计算和设置多步骤使用的环境变量方面也遇到了类似的困难。

10
投票
下面是一个基本示例,说明如果需要处理来计算环境变量以供以后使用,如何从步骤内推回 github 环境。您还可以以同样的方式更新现有变量,而不仅仅是创建新变量。

name: minimal variable example on: push: env: MAJOR: "1" MINOR: "0" PATCH: "1" jobs: vars-example: runs-on: ubuntu-latest steps: - name: only available local variable run: LOCAL_VERSION=${MAJOR}.${MINOR}.${PATCH} - name: available across multiple steps run: echo "GLOBAL_VERSION=${MAJOR}.${MINOR}.${PATCH}" >> $GITHUB_ENV - name: Vars run: | echo LOCAL_VERSION = $LOCAL_VERSION echo GLOBAL_VERSION = $GLOBAL_VERSION

这会导致 Vars 输出为
  echo LOCAL_VERSION = $LOCAL_VERSION
  echo GLOBAL_VERSION = $GLOBAL_VERSION
  shell: /usr/bin/bash -e {0}
  env:
    MAJOR: 1
    MINOR: 0
    PATCH: 1
    GLOBAL_VERSION: 1.0.1
LOCAL_VERSION =
GLOBAL_VERSION = 1.0.1

以下是

7
投票

后跨作业共享变量的新方法 还介绍了

value
    本身是一个变量并且需要在运行时计算的情况。
  • 推送时会调用以下内容
    master
  • 分支
  • name: Sharing envs across jobs
    
    on:
      push:
        branches: ['master']
    
    env:
      one: onevalue
      two: twovalue
      three: threevalue
    
    jobs:
      job0:
        runs-on: ubuntu-latest
        steps:
          - run: |
              echo "${{ env.one }}"
              echo "${{ env.two }}"
              echo "${{ env.three }}"
    
      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=$(date +"%d-%m-%Y")-asdfads223" >> $GITHUB_OUTPUT
          - id: step2
            run: echo "test=world" >> $GITHUB_OUTPUT
      job2:
        runs-on: ubuntu-latest
        needs: job1
        steps:
          - run: echo ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}}
    
您不能在 

1
投票
元素下的表达式中使用

env

。除了使用重复值而不是表达式之外,我没有看到其他方法。

env
上下文语法允许您在工作流程文件中使用环境变量的值。您可以在

step

 中的任何键的值中使用 
env
 上下文,除了 
iduses
 键之外。有关步骤语法的更多信息,请参阅 
“GitHub Actions 的工作流程语法”

https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#env-context

我已成功通过输入传递“ENV_NAME”。所以我有一个“outer.yaml”脚本,它设置 ENV_NAME=QA,然后是一个“inner.yaml”,它需要“ENV_NAME”,然后您可以分配作业环境:${{inputs.env_name}}。

0
投票
Outer.yaml:

name: test/mywf on: pull_request: types: - opened - synchronize workflow_dispatch: jobs: management: permissions: # <- this section is needed for workload identity contents: 'read' id-token: 'write' uses: ./.github/workflows/inner.yaml with: working-directory: . env_name: QA

然后是inner.yaml
name: "inner"
on:
  workflow_call:
    inputs:
      working-directory:
        required: true
        type: string
      env_name:
        required: true
        type: string

jobs:
  package-debug:
    runs-on: ubuntu-latest
    environment: ${{ inputs.env_name}}
    steps:
      - name: Environment
        run: echo "Environment ${{ inputs.environment }}"
  package-debug2:
    runs-on: ubuntu-latest
    environment: ${{ inputs.env_name}}
    steps:
      - name: Environment
        run: echo "Environment2 ${{ inputs.environment }}"


© www.soinside.com 2019 - 2024. All rights reserved.