GitHub Actions能否在matrix定义中使用环境变量?
问题
我在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 }} ]
请问是否可以在matrix定义中这样使用变量?还是只能硬编码所有内容?
解决方案
不能直接在matrix的数组定义里嵌入${{ env.XXX }}这类表达式,GitHub Actions的YAML解析器在处理matrix配置时不支持这种写法,会直接触发语法错误。
要实现变量复用,有两种可行方式:
方式一:硬编码+Job级Env复用
把版本号直接写在matrix中,同时在Job的env里定义变量,方便后续步骤复用:
name: Build & Tests on: pull_request: env: CARGO_TERM_COLOR: always jobs: build_test: runs-on: ubuntu-latest env: ZEROCOPY_MSRV: 1.61.0 ZEROCOPY_CURRENT_STABLE: 1.64.0 ZEROCOPY_CURRENT_NIGHTLY: nightly-2022-09-26 strategy: matrix: channel: [1.61.0, 1.64.0, nightly-2022-09-26] 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: 1.61.0 features: "alloc,simd,simd-nightly" - channel: 1.64.0 features: "alloc,simd,simd-nightly"
方式二:动态生成矩阵(复杂场景)
如果需要更灵活的变量复用,比如从外部文件或API获取版本号,可以用actions/github-script在工作流运行时动态生成matrix。示例如下:
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: generate_matrix: runs-on: ubuntu-latest outputs: matrix: ${{ steps.set_matrix.outputs.matrix }} steps: - id: set_matrix run: | echo "matrix={\"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\": [{\"channel\": \"${{ env.ZEROCOPY_MSRV }}\", \"features\": \"alloc,simd,simd-nightly\"}, {\"channel\": \"${{ env.ZEROCOPY_CURRENT_STABLE }}\", \"features\": \"alloc,simd,simd-nightly\"}]}" >> $GITHUB_OUTPUT build_test: needs: generate_matrix runs-on: ubuntu-latest strategy: matrix: ${{ fromJson(needs.generate_matrix.outputs.matrix) }} steps: # 后续构建测试步骤 - uses: actions/checkout@v4 - run: cargo build --target ${{ matrix.target }} --features ${{ matrix.features }}
总结
- 直接在matrix数组中嵌入
${{ env }}表达式不被支持,会触发YAML语法错误 - 简单场景推荐用硬编码+Job级Env的方式,兼顾可读性和复用性
- 复杂动态场景可以用动态生成矩阵的方法实现变量复用
内容的提问来源于stack exchange,提问作者joshlf
相关产品推荐
相关产品推荐

