C.W.K.
Stream
Lesson 10 of 14 · published

Dynamic matrix

~12 min · matrix, dynamic, fromJSON

Level 0Apprentice
0 XP0/101 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Runtime 에 matrix 계산

때로 matrix 를 hardcode 할 수 없어 — PR 에 뭐 바뀌었는지, monorepo 에 어떤 패키지가 있는지, 어떤 test 가 태그됐는지, config 파일이 어떤 플랫폼 선언하는지에 의존. Dynamic matrix 가 setup job 에서 matrix 계산하고 downstream matrix job 에 공급.

패턴

  1. Setup job 이 repo 점검 (변경 파일, 패키지 리스트 등).
  2. JSON 배열을 job output 으로 emit.
  3. Downstream matrix job 이 strategy.matrix.: ${{ fromJSON(needs.setup.outputs.) }} 설정.
  4. 각 matrix cell 이 실제 작업 실행.

실제 예시

  • Monorepo — 이 PR 에서 코드 바뀐 패키지만 test.
  • 크로스 플랫폼 builder — repo 에서 platforms.json 읽고 엔트리 당 matrix cell 실행.
  • Tag 기반 — test 프레임워크에 표시된 test 그룹 리스트, matrix cell 로 확장.

Code

Monorepo — 변경된 패키지만 test·yaml
jobs:
  detect:
    runs-on: ubuntu-latest
    outputs:
      packages: ${{ steps.changed.outputs.packages }}
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }
      - id: changed
        run: |
          # Find unique top-level package directories changed in this PR
          base=${{ github.event.pull_request.base.sha || 'HEAD~1' }}
          pkgs=$(git diff --name-only "$base" | awk -F/ '/^packages\//{print $2}' | sort -u)
          json=$(echo "$pkgs" | jq -R . | jq -sc .)
          echo "packages=$json" >> $GITHUB_OUTPUT

  test:
    needs: detect
    if: needs.detect.outputs.packages != '[]'
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        package: ${{ fromJSON(needs.detect.outputs.packages) }}
    steps:
      - uses: actions/checkout@v4
      - run: cd packages/${{ matrix.package }} && npm ci && npm test

External links

Exercise

Monorepo 있으면 '변경된 패키지' 탐지 job 빌드해서 matrix 에 공급. 한 패키지만 만지는 PR 열고 그 cell 만 도는지 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.