Github 工作流不从环境中读取变量

Github workflow does not read variables from environments

以下是我的简单 github 工作流程。它旨在打印环境变量。

name: verify

on:
  workflow_dispatch:

jobs:
  read_env_variables:
    environment: build 
    runs-on: [ self-hosted, onprem_dae, docker ]
    steps:
      - name: cat on branch file
        run: |
          echo ${{ env.SOME_VARIABLE }}

我创建了一个名为“build”的环境。在这个环境中,我有一个名为 SOME_VARIABLE 的环境变量设置为 xyz.

触发工作流时,我希望回显值 xyz 但实际值​​为“”。是不是少了什么?

您的问题与语法有关。

要使用 ${{ env.SOME_VARIABLE }} 语法,您需要在工作流、作业或步骤级别设置环境变量。

举个例子:

name: Environment Workflow

on:
  workflow_dispatch:

env:
  WORKFLOW_VARIABLE: WORKFLOW

jobs:

  job1:
    runs-on: ubuntu-latest
    env:
      JOB_VARIABLE: JOB
    steps:
      - name: Run Commands with various variables
        if: ${{ env.WORKFLOW_VARIABLE == 'WORKFLOW' }}
        env:
          STEP_VARIABLE: STEP
        run: |
          echo "Hello World"
          echo "This is the $WORKFLOW_VARIABLE environment variable"
          echo "This is the $JOB_VARIABLE environment variable"
          echo "This is the $STEP_VARIABLE environment variable"

现在,如果您想使用 环境机密进行部署as explained here on the Github Documentation, the syntax would be different using the job_id.environment as you are already using following this doc

这是一个例子:

  job4:
    runs-on: ubuntu-latest
    environment: build
    steps:
      - name: Show repo env secret
        run: |
          echo ${{ secrets.REPO_ENV_SECRET }}

请注意这个变量是一个秘密,因此您将无法通过步骤上的回显命令看到它(它将显示***


如果您想看一下,这是我用来验证所有这些实现的工作流程: