여러 버전이나 환경에서 동일한 작업을 실행할 때 매우 유용한 기능이다.
name: Matrix Example
on:
push:
branches:
- main
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [14, 16, 18]
steps:
- uses: actions/checkout@v4
- name: Node.js ${{ matrix.node-version }} 설정
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: 의존성 설치
run: npm install
- name: 테스트 실행
run: npm test
위 예시에서는 3개의 OS × 3개의 Node.js 버전 = 총 9개의 작업이 동시에 실행된다.
object의 array도 가능하다.
matrix:
os:
- ubuntu-latest
- macos-latest
node:
- version: 14
- version: 20
env: NODE_OPTIONS=--openssl-legacy-provider
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [14, 16, 18]
exclude:
- os: windows-latest
node-version: 14
include:
- os: ubuntu-latest
node-version: 20
experimental: true
windows-latest + node 14 조합 제외
ubuntu-latest + node 20 + experimental: true 조합 추가
다른 예시를 보자
strategy:
matrix:
fruit: [apple, pear]
animal: [cat, dog]
include:
- color: green
- color: pink
animal: cat
- fruit: apple
shape: circle
- fruit: banana
- fruit: banana
animal: cat
기본
{color: green} 추가
{color: green}는 원래 조합의 일부를 덮어쓰지 않고 추가될 수 있으므로 모든 원래 행렬 조합에 추가된다.
{color: pink, animal: cat} 추가
{color: pink, animal: cat}는 animal: cat을 포함하는 원래 행렬 조합에만 color:pink을 추가한다. 이렇게 하면 이전 include 항목이 추가한 color: green을 덮어쓴다.
{fruit: apple, shape: circle} 추가
{fruit: apple, shape: circle}는 fruit: apple을 포함하는 원래 행렬 조합에만 shape: circle을 추가한다.
{fruit: banana} 추가
{fruit: banana}는 값을 덮어쓰지 않고는 원래 행렬 조합에 추가될 수 없으므로 추가 행렬 조합으로 추가된다.
{fruit: banana, animal: cat} 추가
{fruit: banana, animal: cat}는 값을 덮어쓰지 않고는 원래 행렬 조합에 추가될 수 없으므로 추가 행렬 조합으로 추가된다. {fruit: banana} 행렬 조합은 원래 행렬 조합 중 하나가 아니었기 때문에 해당 조합에 추가하지 않는다.
jobs:
test:
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [16, 18]
experimental: [false]
include:
- os: ubuntu-latest
node-version: 20
experimental: true
fail-fast: true : 행렬의 작업이 실패할 경우 행렬의 진행 중인 작업과 대기 중인 작업을 모두 취소한다. (default: true)continue-on-error: true : 이 작업이 실패하면 다른 작업은 영향을 받지 않는다.strategy:
matrix:
# ... matrix 설정 ...
max-parallel: 2 # 동시에 최대 2개의 작업만 실행
여러개 배포할 때 사용해볼게요!