본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

워크플로에서 잡 변형 실행하기

원문 보기 위키 갱신

매트릭스(matrix)를 정의해 하나의 잡을 여러 구성으로 반복 실행하는 방법을 알아봐요.

출처: GitHub Actions 문서

본문

매트릭스 전략에 대해

매트릭스 전략을 사용하면 하나의 잡 정의 안에서 변수를 활용해, 변수 조합에 따라 여러 잡 실행을 자동으로 만들 수 있어요. 예를 들어 여러 언어 버전이나 여러 운영 체제에서 코드를 테스트할 때 매트릭스 전략을 활용할 수 있어요.

워크플로 잡에 매트릭스 전략 추가하기

jobs.<job_id>.strategy.matrix를 사용하면 서로 다른 잡 구성의 매트릭스를 정의할 수 있어요. 매트릭스 안에서 변수를 하나 이상 정의하고, 각 변수 뒤에 값 배열을 나열해요. 예를 들어 다음 매트릭스는 값이 [10, 12, 14]인 version 변수와, 값이 [ubuntu-latest, windows-latest]인 os 변수를 가지고 있어요:

jobs:
  example_matrix:
    strategy:
      matrix:
        version: [10, 12, 14]
        os: [ubuntu-latest, windows-latest]

변수의 가능한 조합마다 잡이 하나씩 실행돼요. 이 예시에서는 워크플로가 여섯 개의 잡을 실행하고, 각 잡은 os와 version 변수 조합 하나씩을 담당해요.

위 매트릭스는 다음 순서로 잡을 만들어요.

  • {version: 10, os: ubuntu-latest}
  • {version: 10, os: windows-latest}
  • {version: 12, os: ubuntu-latest}
  • {version: 12, os: windows-latest}
  • {version: 14, os: ubuntu-latest}
  • {version: 14, os: windows-latest}

참조 정보와 예시는 GitHub Actions 워크플로 문법 문서를 참고하세요.

컨텍스트를 사용해 매트릭스 만들기

워크플로 실행, 변수, 러너 환경, 잡, 스텝에 대한 정보로 매트릭스를 만들려면 `${{ context }}` 표현식 구문으로 컨텍스트에 접근할 수 있어요. 컨텍스트에 대한 자세한 내용은 컨텍스트 문서를 참고하세요.

예를 들어 다음 워크플로는 repository_dispatch 이벤트로 트리거되고, 이벤트 페이로드의 정보를 이용해 매트릭스를 만들어요. 아래와 같은 페이로드로 repository dispatch 이벤트가 생성되면 매트릭스의 version 변수 값은 [12, 14, 16]이 돼요. repository_dispatch 트리거에 대한 자세한 내용은 워크플로를 트리거하는 이벤트 문서를 참고하세요.

{
  "event_type": "test",
  "client_payload": {
    "versions": [12, 14, 16]
  }
}
on:
  repository_dispatch:
    types:
      - test

jobs:
  example_matrix:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        version: ${{ github.event.client_payload.versions }}
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.version }}

매트릭스 구성 확장 또는 추가하기

기존 매트릭스 구성을 확장하거나 새 구성을 추가하려면 jobs.<job_id>.strategy.matrix.include를 사용해요. include의 값은 객체 목록이에요.

예를 들어 다음 매트릭스를 살펴볼게요.

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

그러면 다음 매트릭스 조합을 가진 여섯 개의 잡이 만들어져요.

  • {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}

각 include 항목은 다음 방식으로 적용됐어요.

  • {color: green}은 원래 매트릭스 조합의 어느 값도 덮어쓰지 않고 추가할 수 있어서, 모든 원래 조합에 더해져요.
  • {color: pink, animal: cat}은 animal: cat이 포함된 원래 매트릭스 조합에만 color: pink를 더해요. 이 값은 앞선 include 항목이 더한 color: green을 덮어써요.
  • {fruit: apple, shape: circle}은 fruit: apple이 포함된 원래 매트릭스 조합에만 shape: circle을 더해요.
  • {fruit: banana}는 값을 덮어쓰지 않고는 어떤 원래 매트릭스 조합에도 추가할 수 없어서, 별도의 매트릭스 조합으로 추가돼요.
  • {fruit: banana, animal: cat}도 값을 덮어쓰지 않고는 어떤 원래 매트릭스 조합에도 추가할 수 없어서 별도의 조합으로 추가돼요. 다만 {fruit: banana} 조합 자체가 원래 매트릭스 조합이 아니었기 때문에 이 조합에는 더해지지 않아요.

참조 정보와 예시 구성은 GitHub Actions 워크플로 문법 문서를 참고하세요.

매트릭스 구성 제외하기

매트릭스에 정의된 특정 구성을 제거하려면 jobs.<job_id>.strategy.matrix.exclude를 사용해요.

예를 들어 다음 워크플로는 아홉 개의 잡을 실행해요. 12개 구성 각각에 잡이 하나씩 생기지만, {os: macos-latest, version: 12, environment: production}에 해당하는 잡 하나와 {os: windows-latest, version: 16}에 해당하는 잡 두 개가 제외되는 거죠.

strategy:
  matrix:
    os: [macos-latest, windows-latest]
    version: [12, 14, 16]
    environment: [staging, production]
    exclude:
      - os: macos-latest
        version: 12
        environment: production
      - os: windows-latest
        version: 16
runs-on: ${{ matrix.os }}

참조 정보는 GitHub Actions 워크플로 문법 문서를 참고하세요.

출력으로 두 매트릭스 정의하기

한 잡의 출력을 사용해 여러 잡의 매트릭스를 정의할 수 있어요.

예를 들어 다음 워크플로는 한 잡에서 값의 매트릭스를 정의하고, 두 번째 잡에서 그 매트릭스로 아티팩트를 만든 뒤, 세 번째 잡에서 그 아티팩트를 소비하는 방법을 보여줘요. 각 아티팩트는 매트릭스의 값 하나와 연결돼요.

name: shared matrix
on:
  push:
  workflow_dispatch:

jobs:
  define-matrix:
    runs-on: ubuntu-latest

    outputs:
      colors: ${{ steps.colors.outputs.colors }}

    steps:
      - name: Define Colors
        id: colors
        run: |
          echo 'colors=["red", "green", "blue"]' >> "$GITHUB_OUTPUT"

  produce-artifacts:
    runs-on: ubuntu-latest
    needs: define-matrix
    strategy:
      matrix:
        color: ${{ fromJSON(needs.define-matrix.outputs.colors) }}

    steps:
      - name: Define Color
        env:
          color: ${{ matrix.color }}
        run: |
          echo "$color" > color
      - name: Produce Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.color }}
          path: color

  consume-artifacts:
    runs-on: ubuntu-latest
    needs:
    - define-matrix
    - produce-artifacts
    strategy:
      matrix:
        color: ${{ fromJSON(needs.define-matrix.outputs.colors) }}

    steps:
    - name: Retrieve Artifact
      uses: actions/download-artifact@v4
      with:
        name: ${{ matrix.color }}

    - name: Report Color
      run: |
        cat color

실패 처리하기

잡 실패를 어떻게 처리할지 제어하려면 jobs.<job_id>.strategy.fail-fast와 jobs.<job_id>.continue-on-error를 사용해요.

두 옵션은 함께 사용할 수도 있어요. 예를 들어 다음 워크플로는 네 개의 잡을 시작해요. 각 잡의 continue-on-error 값은 matrix.experimental 값으로 결정돼요. continue-on-error: false인 잡 중 하나라도 실패하면 진행 중이거나 대기 중인 모든 잡이 취소돼요. 반면 continue-on-error: true인 잡이 실패해도 다른 잡은 영향을 받지 않아요.

jobs:
  test:
    runs-on: ubuntu-latest
    continue-on-error: ${{ matrix.experimental }}
    strategy:
      fail-fast: true
      matrix:
        version: [6, 7, 8]
        experimental: [false]
        include:
          - version: 9
            experimental: true

참조 정보는 jobs.<job_id>.strategy.fail-fast와 jobs.<job_id>.continue-on-error를 참고하세요.

동시에 실행할 최대 잡 수 정의하기

matrix 잡 전략을 사용할 때 동시에 실행할 수 있는 최대 잡 수를 정하려면 jobs.<job_id>.strategy.max-parallel을 사용해요.

예를 들어 다음 워크플로는 여섯 개의 잡을 모두 실행할 수 있는 러너가 있더라도 한 번에 최대 두 개의 잡만 실행해요.

jobs:
  example_matrix:
    strategy:
      max-parallel: 2
      matrix:
        version: [10, 12, 14]
        os: [ubuntu-latest, windows-latest]

참조 정보는 GitHub Actions 워크플로 문법 문서를 참고하세요.

더 알아보기 (Learn more)

  • 매트릭스는 워크플로 실행당 최대 256개의 잡을 만들 수 있어요.
  • include로 원래 조합을 덮어쓰지 않는 값을 추가하고, exclude로 불필요한 조합을 걸러내세요.
  • 잡이 많아지면 fail-fast와 max-parallel로 실행 시간과 비용을 조절할 수 있어요.
  • 출력 기반 매트릭스는 잡 간 데이터 전달과 함께 쓰면 강력해요.