조건부 실행
다양한 if 조건을 활용한 브랜치별/이벤트별 분기 파이프라인
Job-level if 조건을 활용하여 브랜치, 이벤트, 이전 Job 상태에 따라 분기하는 파이프라인입니다. 9개 Job이 각각 다른 조건으로 실행됩니다.
전체 코드
name: conditional-pipeline
triggers:
push:
branches: [main, develop]
change_request:
branches: [main]
jobs:
# ─────────────────────────────────────────
# 1. 린트 — 항상 실행 (조건 없음)
# ─────────────────────────────────────────
lint:
steps:
- name: eslint
image: node:18
run: npm run lint
# ─────────────────────────────────────────
# 2. 단위 테스트 — lint 성공 시에만
# ─────────────────────────────────────────
unit-test:
needs: [lint]
if: "success()" # lint가 성공했을 때만 실행
steps:
- name: run-tests
image: node:18
run: npm test
env:
CI: "true"
# ─────────────────────────────────────────
# 3. 통합 테스트 — push 이벤트에서만
# (CR에서는 단위 테스트만 실행)
# ─────────────────────────────────────────
integration-test:
needs: [lint]
if: "collabops.event_name == 'push'"
steps:
- name: setup-db
image: postgres:15
run: echo "Setting up test database..."
- name: run-integration
image: node:18
run: npm run test:integration
# ─────────────────────────────────────────
# 4. 빌드 — 단위 테스트 성공 시
# ─────────────────────────────────────────
build:
needs: [unit-test]
if: "success()"
steps:
- name: build-app
image: node:18
run: npm run build
# ─────────────────────────────────────────
# 5. 스테이징 배포 — develop 브랜치 + 성공 시
# ─────────────────────────────────────────
deploy-staging:
needs: [build, integration-test]
if: "collabops.ref == 'refs/heads/develop' && success()"
steps:
- name: deploy
run: |
echo "Deploying to staging..."
kubectl apply -f k8s/staging/
# ─────────────────────────────────────────
# 6. 프로덕션 배포 — main 브랜치 push만
# ─────────────────────────────────────────
deploy-production:
needs: [build]
if: "collabops.ref == 'refs/heads/main' && collabops.event_name == 'push'"
steps:
- name: approval-check
run: echo "Checking deployment approval..."
- name: deploy
run: |
echo "Deploying to production..."
kubectl apply -f k8s/production/
# ─────────────────────────────────────────
# 7. 성공 알림 — 배포 성공 시
# ─────────────────────────────────────────
notify-success:
needs: [deploy-staging, deploy-production]
if: "success()"
steps:
- name: notify
uses: "collabops/slack-notify@v1"
with:
webhook-url: ${{ secrets.SLACK_WEBHOOK }}
message: "배포 성공: ${{ collabops.ref_name }}"
color: good
# ─────────────────────────────────────────
# 8. 실패 알림 — 빌드 파이프라인 실패 시
# ─────────────────────────────────────────
notify-failure:
needs: [lint, unit-test, build]
if: "failure()"
steps:
- name: notify
uses: "collabops/slack-notify@v1"
with:
webhook-url: ${{ secrets.SLACK_WEBHOOK }}
message: "빌드 실패: ${{ collabops.ref_name }} by ${{ collabops.actor }}"
color: danger
# ─────────────────────────────────────────
# 9. 정리 — 성공/실패 관계없이 항상 실행
# ─────────────────────────────────────────
cleanup:
needs: [lint, unit-test, integration-test, build, deploy-staging, deploy-production]
if: "always()" # 어떤 상황에서든 항상 실행
steps:
- name: cleanup-artifacts
run: echo "Cleaning up build artifacts..."조건별 실행 요약
| Job | 조건 | 설명 |
|---|---|---|
lint | (없음) | 항상 실행 |
unit-test | success() | lint 성공 시 |
integration-test | collabops.event_name == 'push' | push 이벤트에서만 |
build | success() | 테스트 성공 시 |
deploy-staging | ref == develop && success() | develop 브랜치 + 성공 |
deploy-production | ref == main && event == push | main push만 |
notify-success | success() | 배포 성공 시 |
notify-failure | failure() | 파이프라인 실패 시 |
cleanup | always() | 항상 실행 |
이벤트별 실행 범위
| 이벤트 | 실행되는 Jobs |
|---|---|
push (main) | lint → test → integration → build → deploy-production → notify → cleanup |
push (develop) | lint → test → integration → build → deploy-staging → notify → cleanup |
change_request | lint → test → build → cleanup (통합 테스트/배포 건너뜀) |