[GitHub Actions] matrix strategy

문린이·2025년 2월 7일

matrix strategy

여러 버전이나 환경에서 동일한 작업을 실행할 때 매우 유용한 기능이다.

예시

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개의 작업이 동시에 실행된다.

  • {os: ubuntu-latest, node-version: 14}
  • {os: ubuntu-latest, node-version: 16}
  • {os: ubuntu-latest, node-version: 18}
  • {os: windows-latest, node-version: 14}
  • {os: windows-latest, node-version: 16}
  • {os: windows-latest, node-version: 18}
  • {os: macos-latest, node-version: 14}
  • {os: macos-latest, node-version: 16}
  • {os: macos-latest, node-version: 18}

object의 array도 가능하다.

matrix:
  os:
    - ubuntu-latest
    - macos-latest
  node:
    - version: 14
    - version: 20
      env: NODE_OPTIONS=--openssl-legacy-provider
  • {os: ubuntu-latest, node-version: 14}
  • {os: ubuntu-latest, node-version: 20, env: NODE_OPTIONS=--openssl-legacy-provider}
  • {os: macos-latest, node-version: 14}
  • {os: macos-latest, node-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 조합 추가

  • {os: ubuntu-latest, node-version: 14}
  • {os: ubuntu-latest, node-version: 16}
  • {os: ubuntu-latest, node-version: 18}
  • {os: windows-latest, node-version: 16}
  • {os: windows-latest, node-version: 18}
  • {os: macos-latest, node-version: 14}
  • {os: macos-latest, node-version: 16}
  • {os: macos-latest, node-version: 18}
  • {os: ubuntu-latest, node-version: 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
  1. 기본

    • {fruit: apple, animal: cat}
    • {fruit: apple, animal: dog}
    • {fruit: pear, animal: cat}
    • {fruit: pear, animal: dog}
  2. {color: green} 추가
    {color: green}는 원래 조합의 일부를 덮어쓰지 않고 추가될 수 있으므로 모든 원래 행렬 조합에 추가된다.

    • {fruit: apple, animal: cat, color: green}
    • {fruit: apple, animal: dog, color: green}
    • {fruit: pear, animal: cat, color: green}
    • {fruit: pear, animal: dog, color: green}
  3. {color: pink, animal: cat} 추가
    {color: pink, animal: cat}는 animal: cat을 포함하는 원래 행렬 조합에만 color:pink을 추가한다. 이렇게 하면 이전 include 항목이 추가한 color: green을 덮어쓴다.

    • {fruit: apple, animal: cat, color: pink}
    • {fruit: apple, animal: dog, color: green}
    • {fruit: pear, animal: cat, color: pink}
    • {fruit: pear, animal: dog, color: green}
  4. {fruit: apple, shape: circle} 추가
    {fruit: apple, shape: circle}는 fruit: apple을 포함하는 원래 행렬 조합에만 shape: circle을 추가한다.

    • {fruit: apple, animal: cat, color: pink, shape: circle}
    • {fruit: apple, animal: dog, color: green, shape: circle}
    • {fruit: pear, animal: cat, color: pink}
    • {fruit: pear, animal: dog, color: green}
  5. {fruit: banana} 추가
    {fruit: banana}는 값을 덮어쓰지 않고는 원래 행렬 조합에 추가될 수 없으므로 추가 행렬 조합으로 추가된다.

    • {fruit: apple, animal: cat, color: pink, shape: circle}
    • {fruit: apple, animal: dog, color: green, shape: circle}
    • {fruit: pear, animal: cat, color: pink}
    • {fruit: pear, animal: dog, color: green}
    • {fruit: banana}
  6. {fruit: banana, animal: cat} 추가
    {fruit: banana, animal: cat}는 값을 덮어쓰지 않고는 원래 행렬 조합에 추가될 수 없으므로 추가 행렬 조합으로 추가된다. {fruit: banana} 행렬 조합은 원래 행렬 조합 중 하나가 아니었기 때문에 해당 조합에 추가하지 않는다.

    • {fruit: apple, animal: cat, color: pink, shape: circle}
    • {fruit: apple, animal: dog, color: green, shape: circle}
    • {fruit: pear, animal: cat, color: pink}
    • {fruit: pear, animal: dog, color: green}
    • {fruit: banana}
    • {fruit: banana, animal: cat}

실패 처리

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개의 작업만 실행
profile
Software Developer

1개의 댓글

comment-user-thumbnail
2025년 3월 7일

여러개 배포할 때 사용해볼게요!

답글 달기