在 GitHub 操作工作流作业中为环境使用动态输入值

时间:2021-01-21 11:19:34

标签: github-actions

我正在尝试创建一个 GitHub 操作工作流,其中一项作业的 environment 设置具有动态值。 https://docs.github.com/en/actions/reference/environments 在这个工作流程中我有两个工作。第一个作业确定第二个作业将在哪个 environment 中运行,而这又取决于 Actions 作业从哪个 git 分支运行。

这是我尝试让它工作的天真尝试,但我收到一个错误提示

<块引用>

(第 29 行,第 18 列):无法识别的命名值:“需要”。位于表达式中的位置 1:needs.get-environment.outputs.environment_name

name: Environments

on:
  push:
  workflow_dispatch:

jobs:
  get-environment:
    runs-on: ubuntu-latest
    outputs:
      environment_name: ${{ steps.get_environment.outputs.environment_name }}
    steps:
      - id: get_environment
        run: |
          if [ "$GITHUB_REF" = "refs/heads/test" ]
          then
            echo "::set-output name=environment_name::test"
          elif [ "$GITHUB_REF" = "refs/heads/qa" ]
          then
            echo "::set-output name=environment_name::qa"
          elif [ "$GITHUB_REF" = "refs/heads/master" ]
          then
            echo "::set-output name=environment_name::production"
          fi
    
  use-environment:
    runs-on: ubuntu-latest
    needs: [get-environment]
    environment: ${{ needs.get-environment.outputs.environment_name }}
    steps:
      - uses: actions/checkout@v2

      - name: Run a one-line script
        run: echo ${{ secrets.ENV_DEPENDENT_SECRET }}

是否有可能实现我想要做的事情?我的最终目标是为三个不同的应用环境(测试、QA 和生产)创建一个工作流文件,其中每个应用环境使用单独的操作 environment。 (术语变得混乱,我知道)

1 个答案:

答案 0 :(得分:-1)

你试过了吗

>   use-environment:
>     runs-on: ubuntu-latest
>     needs: [get-environment]
>     environment: 
>          name: ${{ needs.get-environment.outputs.environment_name }}