【问题标题】:Is it possible to have a dynamic strategy matrix in a workflow in GitHub Actions?是否可以在 GitHub Actions 的工作流程中拥有动态策略矩阵?
【发布时间】:2022-01-03 02:16:12
【问题描述】:

我想在工作流中动态指定一个策略矩阵。所以,而不是:

strategy:
  matrix:
    foo: [bar, baz]

我想首先调用一些脚本来计算并返回一个数组,例如[bar, baz],然后我想将其用作策略矩阵。

这可能吗?

【问题讨论】:

    标签: github github-actions


    【解决方案1】:

    使用可用的 GitHub Actions 工作流功能是不可能的,但可以使用一些 hacky 的解决方案来提供所有必需的矩阵参数值的组合。您可以在以前的工作流作业之一中将所有组合生成为 JSON sn-p 并将其公开为作业 outputs 然后在下一个作业中将其与矩阵 include 关键字一起使用以提供所有矩阵参数及其值' 使用fromJson() 的组合作为demonstrated in the official announcement。为了更好地解释这个概念,让我们看一下示例静态矩阵作业:

    jobs:
      matrix-job:
        runs-on: ubuntu-latest
        strategy:
          matrix:
            includes:
              - foo: foo-1
                bar: bar-1
              - foo: foo-1
                bar: bar-2
              - foo: foo-2
                bar: bar-1
        steps:
          - run: |
              echo foo=${{ matrix.foo }}
              echo bar=${{ matrix.bar }}
    

    工作流程的结果是:

    在此工作流程中,所有矩阵参数值组合都是静态提供的。我们可以将其转换为动态提供,如下所示:

    jobs:
      setup-matrix:
        runs-on: ubuntu-latest
        steps:
          - name: Setup matrix combinations
            id: setup-matrix-combinations
            run: |
              MATRIX_PARAMS_COMBINATIONS='
                  {"foo": "foo-1", "bar": "bar-1"},
                  {"foo": "foo-1", "bar": "bar-2"},
                  {"foo": "foo-2", "bar": "bar-1"},
              '
              echo ::set-output name=matrix-combinations::{\"include\":[$MATRIX_PARAMS_COMBINATIONS]}
        outputs:
          matrix-combinations: ${{ steps.setup-matrix-combinations.outputs.matrix-combinations }}
      matrix-job:
        runs-on: ubuntu-latest
        needs: setup-matrix
        strategy:
          matrix: ${{ fromJson(needs.setup-matrix.outputs.matrix-combinations) }}
        steps:
          - run: |
              echo foo=${{ matrix.foo }}
              echo bar=${{ matrix.bar }}
    

    结果:

    对于matirx-job,这两个工作流程具有相同的结果,但最后一个工作流程提供动态生成的矩阵输入。这是您可以动态生成矩阵构建的唯一方法,您必须使用matrix.include 自己提供所有组合。不可能(在撰写本文时)为给定的矩阵参数(如您的问题)动态提供可用值的数组,但您至少有动态矩阵作业。

    【讨论】:

    • 当我尝试使用它得到的矩阵时 - 找到无效类型:预期对象但在此行矩阵中找到字符串:${{ fromJson(needs.setup-matrix.outputs.matrix-combinations ) }} ,有什么帮助吗?
    【解决方案2】:

    是的,您可以使用从您的步骤所依赖的先前步骤的输出中提取的矩阵(“需要”):

    jobs:
      set:
        runs-on: ubuntu-20.04
        outputs:
          matrix: ${{steps.list_dirs.outputs.matrix}}
        steps:
          - uses: actions/checkout@v2
          - id: list_dirs
            run: echo "::set-output name=matrix::$(ls|jq -cnR '[inputs | select(length>0)]')"
    
      get:
        runs-on: ubuntu-20.04
        needs: set
        strategy:
          matrix:
            subdir: ${{fromJson(needs.set.outputs.matrix)}}
    

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 1970-01-01
      • 2020-03-19
      • 2021-11-04
      • 2019-11-28
      • 1970-01-01
      • 2020-04-03
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多