简体   繁体   English

GitHub Actions 的工作流中是否可以有动态策略矩阵?

[英]Is it possible to have a dynamic strategy matrix in a workflow in GitHub Actions?

I would like to specify a strategy matrix dynamically within the workflow.我想在工作流中动态指定一个策略矩阵。 So, instead of:所以,而不是:

strategy:
  matrix:
    foo: [bar, baz]

I want to first call some script which will compute and return an array such as [bar, baz] to me, and then I want to use that as the strategy matrix.我想首先调用一些脚本,该脚本将计算并返回一个数组,例如[bar, baz]给我,然后我想将其用作策略矩阵。

Is this possible?这可能吗?

It's not possible with the available GitHub Actions workflow features but it is possible with a bit hacky solution to provide all the required matrix parameter values' combinations.可用的 GitHub 操作工作流功能无法实现,但可以通过一些 hacky 解决方案来提供所有必需的矩阵参数值组合。 You can generate all the combinations as a JSON snippet in one of the previous workflow jobs and expose it as the job outputs then use it with matrix include keyword in the next job to provide all the matrix parameters and its values' combinations using fromJson() function as demonstrated in the official announcement .您可以在以前的工作流作业之一中将所有组合生成为 JSON 片段并将其公开为作业outputs ,然后在下一个作业中将其与矩阵include关键字一起使用,以使用fromJson()提供所有矩阵参数及其值的组合function 如官方公告所示 To better explain the concept lets look at the example static matrix job:为了更好地解释这个概念,让我们看一下示例 static 矩阵作业:

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 }}

The workflow outcome is:工作流结果是:

在此处输入图像描述

In this workflow, all the matrix parameter values combinations are statically provided.在此工作流程中,所有矩阵参数值组合都是静态提供的。 We can convert it to be dynamically provided like this:我们可以将其转换为动态提供,如下所示:

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 }}

and the outcome:结果:

在此处输入图像描述

The two workflows have equivalent outcomes for the matirx-job but the last one provides a matrix input which is generated dynamically.这两个工作流对于matirx-job有相同的结果,但最后一个提供了一个动态生成的矩阵输入。 This is the only way you can dynamically generate a matrix build, you have to provide all the combinations on your own using matrix.include .这是您可以动态生成矩阵构建的唯一方法,您必须使用matrix.include自己提供所有组合。 It's not possible (at the time of writing this) to dynamically provide an array of available values for the given matrix parameter (like in your question) but you have at least dynamic matrix job.不可能(在撰写本文时)为给定的矩阵参数(如您的问题)动态提供可用值的数组,但您至少有动态矩阵作业。

Yes, you can use a matrix that is ingested from an output of a prior step that your step depends on ("needs"):是的,您可以使用从您的步骤所依赖的先前步骤的 output 摄取的矩阵(“需要”):

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)}}

I have a similar structure as shown above, but I am generating that structure by executing Python code which reads from a Yaml input file.我有如上所示的类似结构,但我通过执行从 Yaml 输入文件读取的 Python 代码生成该结构。 But, somehow, I can't get it working.但是,不知何故,我无法让它工作。 Since I am using Python script, I need to write the structure (below) to an environment variable before I can assign it to set-output.由于我使用的是 Python 脚本,因此我需要将结构(如下)写入环境变量,然后才能将其分配给设置输出。 Then I noticed (after a lot of trials) that I need to put every set in a new line, but it doesn't seem to work.然后我注意到(经过大量试验后)我需要将每一组都放在一个新行中,但这似乎不起作用。 Any help will be appreciated.任何帮助将不胜感激。

 MATRIX_PARAMS_COMBINATIONS='
          {"foo": "foo-1", "bar": "bar-1"},
          {"foo": "foo-1", "bar": "bar-2"},
          {"foo": "foo-2", "bar": "bar-1"},
          '
          

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM