我在 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 }} ]
有没有办法像这样在矩阵定义中使用变量?或者我只需要对所有内容进行硬编码?
环境变量 (在工作流程级别) 可用于工作流程中所有作业的
。steps
在您的示例中,环境变量在
job
级别使用(在作业strategy
/矩阵定义内),而不是在作业steps
内部。
在该级别,环境变量不会由 GitHub 解释器进行插值。
对矩阵内
channel
字段内的值进行硬编码 strategy
:
示例:
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 发送输入(在某种程度上,它为您提供了更大的值灵活性,但增加了复杂性)。
我们可以使用如何在 github 操作中读取字符串数组以进行矩阵中描述的解决方案?
outputs
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 matrix
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 }}"