我在 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 }} ]
有沒有辦法像這樣在矩陣定義中使用變數?還是我只需要對所有內容進行硬編碼?
uj5u.com熱心網友回復:
根據檔案(參考 1和參考 2)
環境變數 (在作業流級別)可用于
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 發送輸入(在某種程度上,它為您提供了更多的值靈活性,但增加了復雜性)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515923.html
