GitHub Actions的工作流程中是否可能有动态策略矩阵? [英] Is it possible to have a dynamic strategy matrix in a workflow in GitHub Actions?

查看:64
本文介绍了GitHub Actions的工作流程中是否可能有动态策略矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在工作流程中动态指定策略矩阵.因此,代替:

 策略:矩阵:foo:[bar,baz] 

我想先调用一些脚本,该脚本将计算并返回诸如 [bar,baz] 之类的数组,然后再将其用作策略矩阵.

这可能吗?

解决方案

可用的GitHub Actions工作流功能是不可能的,但是有一点棘手的解决方案可以提供所有必需的矩阵参数值的组合.您可以在先前的工作流程作业之一中将所有组合生成为JSON代码片段,并将其作为作业显示为

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

 职位:设置矩阵:运行:ubuntu-latest脚步:-名称:设置矩阵组合id:setup-matrix-combinations运行: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]}输出:矩阵组合:$ {{steps.setup-matrix-combinations.outputs.matrix-combinations}}矩阵工作:运行:ubuntu-latest需要:设置矩阵战略:矩阵:$ {{fromJson(needs.setup-matrix.outputs.matrix-combinations)}}脚步:-运行:echo foo = $ {{matrix.foo}}echo bar = $ {{matrix.bar}} 

和结果:

这两个工作流程在 matirx-job 上具有相同的结果,但是最后一个提供了动态生成的矩阵输入.这是动态生成矩阵构建的唯一方法,您必须使用 解决方案

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. 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. To better explain the concept lets look at the example static matrix job:

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

这篇关于GitHub Actions的工作流程中是否可能有动态策略矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆