본문 바로가기
C.W.K.
Stream
Lesson 03 of 14 · published

재사용 워크플로

~13 min · reusable, workflow_call, centralization

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

하나의 워크플로, 여러 호출자

재사용 워크플로on: workflow_call를 가진 워크플로 파일이야. 다른 워크플로가 uses:로 참조하지. 호출된 워크플로는 자기 작업을 자기 러너에서 실행하지만, 비밀값과 변수는 호출자에게서 상속받아.

왜 이게 중앙화 메커니즘일까

12개 저장소가 모두 '빌드, 테스트, GHCR 푸시, 스모크 배포' 순서를 필요로 한다면 12개 복사본을 유지하고 싶지 않을 거야. 단일 저장소(org/.githuborg/ci)에 워크플로를 넣고 workflow_call로 노출해서, 모든 사용자가 그곳을 가리키게 만들어.

입력값, 출력값, 비밀값

  • inputs — 호출자의 타입이 있는 매개변수(string, boolean, number)야.
  • outputs — 호출된 워크플로가 생성하고, 호출자의 하위 작업이 읽어.
  • secrets — 명시적으로 상속하거나 하나씩 지정해. 간단한 경우엔 secrets: inherit을 써.

알아둘 제약

  • 중첩은 4단계까지 가능해(a가 b를 호출, b가 c를, c가 d를).
  • 재사용 워크플로는 단계에서 호출할 수 없어(작업에서만 가능).
  • 호출된 워크플로의 permissions: 블록이 적용되고, 호출자의 것은 아니야.

Code

재사용 워크플로 정의·yaml
# .github/workflows/reusable-test.yml
name: reusable-test
on:
  workflow_call:
    inputs:
      python-version:
        type: string
        default: '3.12'
      coverage-min:
        type: number
        default: 80
    outputs:
      coverage:
        description: Reported coverage percentage
        value: ${{ jobs.test.outputs.coverage }}

jobs:
  test:
    runs-on: ubuntu-latest
    outputs:
      coverage: ${{ steps.cov.outputs.value }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: { python-version: '${{ inputs.python-version }}' }
      - run: pip install -e '.[dev]' && pytest --cov=src --cov-fail-under=${{ inputs.coverage-min }}
      - id: cov
        run: echo "value=$(coverage report | tail -1 | awk '{print $4}')" >> $GITHUB_OUTPUT
호출자 워크플로·yaml
# .github/workflows/ci.yml
name: ci
on: { pull_request: {}, push: { branches: [main] } }

jobs:
  call-test:
    uses: ./.github/workflows/reusable-test.yml   # local repo
    # or: uses: my-org/ci-workflows/.github/workflows/reusable-test.yml@v1
    with:
      python-version: '3.12'
      coverage-min: 85
    secrets: inherit

  use-output:
    needs: call-test
    runs-on: ubuntu-latest
    steps:
      - run: echo "Coverage was ${{ needs.call-test.outputs.coverage }}%"

External links

Exercise

거의 동일한 CI 워크플로 파일을 가진 저장소 두 개를 찾아봐. 공통된 형태를 세 번째 저장소의 재사용 워크플로로 끌어올려. 사용자 둘 다 그 재사용 워크플로를 uses:로 참조하게 하고, 한 번의 업데이트로 동기화해.

Progress

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

댓글 0

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

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