GitHub操作:使用strategy.matrix为作业动态输出



我有一个包,它是组织内多个其他包的核心依赖项。我的目标是编写一个操作来自动化/促进这些反向依赖关系的测试。大致来说,行动应该是:

  • 触发PR中的注释
  • 使用该PR中的代码运行一组反向依赖项的单元测试
  • 回复PR,说明哪些测试失败(如果有的话(

第1步和第3步我开始工作,但第2步遇到了问题。我目前的解决方案是对所有作业输出进行硬编码,以将结果从步骤2传递到步骤3,但我想知道是否有一种方法可以避免对其进行硬编码。

下面的示例工作流程说明了我的问题:

name: Test
on: push
jobs:
unit-tests:
runs-on: ${{ matrix.os }}
continue-on-error: true
name: ${{ matrix.os }} (${{ matrix.pkg }})
strategy:
fail-fast: false
matrix:
# there will be more pkgs and OSes
os: [ubuntu-latest]
pkg: [pkgA, pkgB]
# how to avoid hardcoding these?
outputs:
ubuntu-latest-pkgA: ${{ steps.update-output.outputs.ubuntu-latest-pkgA }}
ubuntu-latest-pkgB: ${{ steps.update-output.outputs.ubuntu-latest-pkgB }}     

steps:
- uses: actions/checkout@v2

- name: fake unit tests
run: |
exit 1 # fail all tests for now
shell: bash
- name: set error if tests fail
id: update-output
if: ${{ failure() }}
run: echo "::set-output name=${{ matrix.os }}-${{ matrix.pkg }}::error"
shell: bash
aggregate-results:
runs-on: ubuntu-latest
needs: unit-tests
steps:
- name: Aggregate results
env:
NEEDS: ${{ toJSON(needs) }}
run: echo "$NEEDS"

工作汇总结果(受此帖子启发(运行良好并打印:

{
"unit-tests": {
"result": "success",
"outputs": {
"ubuntu-latest-pkgA": "error",
"ubuntu-latest-pkgB": "error"
}
}
}

我可以用它来创建一个信息性的评论。但是,作业unit-tests要求我对os和pkg的所有组合的输出进行硬编码。有没有一种方法可以动态地做到这一点?

我也在寻找这个解决方案。实现这种方法的唯一方法是切换到可重复使用的工作流,该工作流允许访问一个特定的作业上下文

https://docs.github.com/en/actions/learn-github-actions/contexts#jobs-上下文

最新更新