GitHub Actions:在矩阵定义中使用变量?

9

我在 GitHub Action 配置文件中有以下代码:

name: Build & Tests

on:
  pull_request:

env:
  CARGO_TERM_COLOR: always
  ZEROCOPY_MSRV: 1.61.0
  ZEROCOPY_CURRENT_STABLE: 1.64.0
  ZEROCOPY_CURRENT_NIGHTLY: nightly-2022-09-26

jobs:
  build_test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        # See `INTERNAL.md` for an explanation of these pinned toolchain
        # versions.
        channel: [ ${{ env.ZEROCOPY_MSRV }}, ${{ env.ZEROCOPY_CURRENT_STABLE }}, ${{ env.ZEROCOPY_CURRENT_NIGHTLY }} ]
        target: [ "i686-unknown-linux-gnu", "x86_64-unknown-linux-gnu", "arm-unknown-linux-gnueabi", "aarch64-unknown-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc64-unknown-linux-gnu", "wasm32-wasi" ]
        features: [ "" , "alloc,simd", "alloc,simd,simd-nightly" ]
        exclude:
          # Exclude any combination which uses a non-nightly toolchain but
          # enables nightly features.
          - channel: ${{ env.ZEROCOPY_MSRV }}
            features: "alloc,simd,simd-nightly"
          - channel: ${{ env.ZEROCOPY_CURRENT_STABLE }}
            features: "alloc,simd,simd-nightly"

我在这个文件上遇到了以下解析错误:
Invalid workflow file: .github/workflows/ci.yml#L19
You have an error in your yaml syntax on line 19

似乎是在引用这一行(实际上是偏移了一行,但也许它使用的是从零开始编号的行号?)
channel: [ ${{ env.ZEROCOPY_MSRV }}, ${{ env.ZEROCOPY_CURRENT_STABLE }}, ${{ env.ZEROCOPY_CURRENT_NIGHTLY }} ]

有没有办法在矩阵定义中像这样使用变量?还是我需要硬编码所有内容?

2个回答

9
根据文档(参考文献1参考文献2),

环境变量(在工作流程级别)对工作流中所有作业的步骤都可用。

在您的示例中,环境变量是在作业级别(在作业策略/矩阵定义内部)使用的,而不是在作业步骤内部使用。在那个级别上,环境变量不会被GitHub解释器插值。

第一种替代方案

在您的矩阵策略strategy中,将值硬编码到channel字段内:

示例:

channel: [ "1.61.0", "1.64.0", "nightly-2022-09-26" ]

然而,您将不得不针对每个作业执行此操作(重复代码对维护不利)。
第二种替代方案
使用inputs(带有可重用工作流workflow_call触发器或workflow_dispatch触发器)。
示例:
on:
  workflow_dispatch: # or workflow_call
    inputs:
      test1:
        description: "Test1"
        required: false
        default: "test1"
      test2:
        description: "Test2"
        required: false
        default: "test2"
      test3:
        description: "Test3"
        required: false
        default: "test3"

jobs:
  build_test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        channel: [ "${{ inputs.test1 }}", "${{ inputs.test2 }}", "${{ inputs.test3 }}" ]

在这种情况下,inputs 将由 GitHub 解释器插值。
但是,您需要从另一个工作流或通过 GitHub API 触发工作流来发送输入(以某种方式,它可以为您提供更多的灵活性与值,但会增加复杂性)。

0
我们可以使用outputs来将变量传递给矩阵。
  1. 使用一个中间作业来从变量计算outputs
  2. 在矩阵中使用计算得到的outputs
name: Build & Tests

on:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  ZEROCOPY_MSRV: 1.61.0
  ZEROCOPY_CURRENT_STABLE: 1.64.0
  ZEROCOPY_CURRENT_NIGHTLY: nightly-2022-09-26

jobs:
  compute:
    runs-on: ubuntu-latest
    outputs:
      ZEROCOPY_MSRV: ${{ env.ZEROCOPY_MSRV }}
      ZEROCOPY_CURRENT_STABLE: ${{ env.ZEROCOPY_CURRENT_STABLE }}
      ZEROCOPY_CURRENT_NIGHTLY: ${{ env.ZEROCOPY_CURRENT_NIGHTLY }}
    steps:
      - name: Compute outputs
        run: |
          echo "ZEROCOPY_MSRV=${{ env.ZEROCOPY_MSRV }}" >> $GITHUB_OUTPUT
          echo "ZEROCOPY_CURRENT_STABLE=${{ env.ZEROCOPY_CURRENT_STABLE }}" >> $GITHUB_OUTPUT
          echo "ZEROCOPY_CURRENT_NIGHTLY=${{ env.ZEROCOPY_CURRENT_NIGHTLY }}" >> $GITHUB_OUTPUT

  build_test:
    runs-on: ubuntu-latest
    needs: compute
    strategy:
      matrix:
        channel: ["${{ needs.compute.outputs.ZEROCOPY_MSRV }}", "${{ needs.compute.outputs.ZEROCOPY_CURRENT_STABLE }}", "${{ needs.compute.outputs.ZEROCOPY_CURRENT_NIGHTLY }}"]
        target: ["i686-unknown-linux-gnu", "x86_64-unknown-linux-gnu", "arm-unknown-linux-gnueabi", "aarch64-unknown-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc64-unknown-linux-gnu", "wasm32-wasi"]
        features: ["" , "alloc,simd", "alloc,simd,simd-nightly"]
        exclude:
          - channel: ${{ needs.compute.outputs.ZEROCOPY_MSRV }}
            features: "alloc,simd,simd-nightly"
          - channel: ${{ needs.compute.outputs.ZEROCOPY_CURRENT_STABLE }}
            features: "alloc,simd,simd-nightly"
    steps:
      - name: Run matrix for ${{ matrix.channel }}-${{ matrix.target }}-${{ matrix.features }}"
        run: |
          echo "Channel: ${{ matrix.channel }}"
          echo "Target: ${{ matrix.target }}"
          echo "Features: ${{ matrix.features }}"

解决方案基于以下问题:
- [如何在 GitHub Actions 中将字符串数组读入矩阵?](link1) - [如何将环境变量输入传递给可重用的工作流程?](link2)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接