在多个操作系统上运行工作流程的标准方法是使用矩阵:
jobs:
package:
strategy:
matrix:
os: [ubuntu, macos, windows]
runs-on: ${{ matrix.os }}-latest
在某些情况下,仅在其中一种操作系统上运行工作流很有用,例如,在调试时或仅其中一个操作系统发生更改而无需运行其他两个操作系统时。怎么只能运行其中一个?
一个解决方案是注释掉不需要的元素,但这是一个非常本地化且脆弱的解决方案。 另一种可能是创建 4 个不同的工作流程(每个系统一个 + 所有系统一个),但是当您有一系列工作流程(例如现在需要的
build
-> package
-> deploy
)时,这很麻烦并且变得复杂每种有 4 种变体。
choice
输入来选择系统,
on:
workflow_dispatch:
inputs:
os:
type: choice
options:
- windows
- macos
- ubuntu
- ubuntu, macos, windows
required: true
jobs:
package:
strategy:
matrix:
os: "${{ github.event.inputs.os }}"
runs-on: ${{ matrix.os }}-latest
适用于单个系统选择,但不适用于多个系统选择,因为它被视为单个字符串
"ubuntu, macos, windows"
。
我还尝试使用
boolean
输入来选择我需要的任何组合:
on:
workflow_dispatch:
inputs:
ubuntu:
required: true
type: boolean
macos:
required: true
type: boolean
windows:
required: true
type: boolean
但我不知道如何将它们组合成一个矩阵。我可以做
...
jobs:
ubuntu:
if: ${{ inputs.ubuntu }}
runs-on: ubuntu-latest
steps: ...
macos:
if: ${{ inputs.macos}}
runs-on: macos-latest
steps: ...
windows:
if: ${{ inputs.windows}}
runs-on: windows-latest
steps: ...
我“应该”使用某种方法来实现此功能吗?还要考虑这些工作流程可以重复使用(在单个操作系统或全部操作系统上运行
build
-> package
-> deploy
序列),因此也可以从调用工作流程接收操作系统输入。
要使其直接工作而不需要中间作业,您可以提供
options
作为 JSON 数组,然后使用 fromJson
函数创建 matrix
,如下所示:
name: os_matrix_from_inputs
on:
workflow_dispatch:
inputs:
os:
description: Select OS
type: choice
options:
- '["ubuntu-latest"]'
- '["windows-latest"]'
- '["macos-latest"]'
- '["ubuntu-latest", "macos-latest", "windows-latest"]'
default: '["ubuntu-latest"]'
required: true
jobs:
ci:
strategy:
matrix:
os: ${{ fromJson(inputs.os) }}
runs-on: ${{ matrix.os }}
steps:
- name: Check
run: echo '${{ matrix.os }}'