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

Reusable workflow

~13 min · reusable, workflow_call, centralization

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

한 workflow, 여러 caller

Reusable workflowon: workflow_call 가진 workflow 파일. 다른 workflow 가 uses: 로 참조. 호출된 workflow 는 자기 job 으로 자기 runner 에서 돌지만, caller 에서 secret 과 변수 상속.

왜 이게 중앙화 메커니즘인가

12 repo 가 다 같은 'build, test, GHCR push, smoke deploy' 시퀀스 필요하면 12 카피 유지하기 싫어. Single repo (org/.githuborg/ci) 에 workflow 넣고 workflow_call 로 노출, 모든 consumer 가 거기를 가리키게.

Input, output, secret

  • inputs — caller 의 typed 매개변수 (string, boolean, number).
  • outputs — 호출된 workflow 가 생산, caller 의 downstream job 이 읽음.
  • secrets — 명시적으로 상속 또는 하나씩 명명. 단순한 경우 secrets: inherit 사용.

알아둘 제약

  • 중첩 4 레벨까지 (a 가 b 호출, b 가 c, c 가 d).
  • Reusable workflow 를 step 에서 호출 불가 (job 에서만).
  • 호출된 workflow 의 permissions: 블록 적용, caller 의 게 아님.

Code

Reusable workflow 정의·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
Caller workflow·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 workflow 파일 가진 repo 두 개 식별. 공유 shape 을 세 번째 repo 의 reusable workflow 로 끌어올려. Consumer 둘 다 그 reusable 한 거 uses: 하고 한 번의 업데이트로 동기화.

Progress

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

댓글 0

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

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