홈 › CI/CD › 02 / 7

GitHub Actions 기초

섹션 7진행 0 / 7

4. 응용 변형 예제

4.1 PR 에서는 검증만, push 는 배포까지

yaml
on: { pull_request: {}, push: { branches: [main] } }
jobs:
  test:
    runs-on: ubuntu-latest
  deploy:
    if: github.event_name == 'push'
    needs: test
    runs-on: ubuntu-latest

deploy 잡에 if: github.event_name == 'push' 를 걸면 PR 에서는 테스트만 돌고, main 병합으로 push 이벤트가 될 때만 배포 잡이 실행됩니다.

4.2 수동 실행에 입력값 받기

yaml
on:
  workflow_dispatch:
    inputs:
      env:
        description: 배포 대상 환경
        default: stage
        type: choice
        options: [stage, prod]

inputs 로 받은 값은 ${{ github.event.inputs.env }} 로 스텝 안에서 씁니다. 운영 배포처럼 사람이 값을 고르고 눌러야 실행되는 작업에 알맞습니다.

4.3 환경별로 시크릿 나누기

yaml
jobs:
  deploy:
    environment: production
    steps:
      - run: echo "token=${{ secrets.PROD_DEPLOY_TOKEN }}"

environment: 를 지정하면 저장소 전체 시크릿이 아니라 그 환경에만 등록한 시크릿을 쓰고, 필요하면 배포 전 승인 단계도 걸 수 있습니다. stage 와 prod 의 토큰을 분리할 때 씁니다.

4.4 매트릭스 조합 일부 제외

yaml
strategy:
  matrix:
    os: [ubuntu-latest, windows-latest]
    java: [17, 21]
    exclude:
      - os: windows-latest
        java: 17

os 와 java 를 곱하면 조합이 네 개지만, exclude 로 한 조합을 빼 세 개만 실행합니다. 특정 조합에서 지원이 끝났거나 의미가 없을 때 씁니다.

4.5 실패한 스텝 뒤에도 정리 작업 실행

yaml
steps:
  - run: ./run-tests.sh
  - name: 테스트 리포트 업로드
    if: always()
    uses: actions/upload-artifact@v4
    with:
      name: test-report
      path: report

기본값은 이전 스텝이 실패하면 뒤 스텝을 건너뜁니다. if: always() 를 붙이면 실패해도 리포트 업로드처럼 꼭 필요한 정리 스텝은 실행됩니다.