【问题标题】:Use dynamic input value for Environment in GitHub Actions workflow job在 GitHub Actions 工作流作业中为环境使用动态输入值
【发布时间】:2021-04-25 19:20:56
【问题描述】:

我正在尝试创建一个 GitHub Actions 工作流程,其中一项作业对其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。 (术语变得混乱,我知道)

【问题讨论】:

标签: github-actions


【解决方案1】:

你试过了吗

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

【讨论】:

    【解决方案2】:

    实际上,我必须为我工作的地方解决这个问题。您可以使用包含环境名称列表的矩阵并动态设置环境名称。查看工作流文件的link

    【讨论】:

      猜你喜欢
      • 2022-12-13
      • 2020-07-27
      • 2022-11-11
      • 2021-09-23
      • 2023-01-16
      • 2020-08-20
      • 1970-01-01
      • 2022-10-23
      • 2021-10-18
      相关资源
      最近更新 更多