포스트

[GitHub 100일 챌린지] Day 83 - Workflow 문법 완전정복

[GitHub 100일 챌린지] Day 83 - Workflow 문법 완전정복

100일 챌린지 Day 83 - Workflow의 다양한 트리거와 조건을 배웁니다.

배울 내용

  1. 다양한 트리거 유형
  2. 조건부 실행
  3. Job 의존성 설정

트리거 (on) 완전 정복

기본 이벤트

1
2
3
4
5
6
7
8
9
10
11
# Push
on: push

# Pull Request
on: pull_request

# 수동 실행
on: workflow_dispatch

# 여러 이벤트
on: [push, pull_request]

브랜치 필터

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 특정 브랜치만
on:
  push:
    branches:
      - main
      - develop

# 제외
on:
  push:
    branches-ignore:
      - temp-*

# 패턴 매칭
on:
  push:
    branches:
      - 'feature/**'  # feature/로 시작하는 모든 브랜치

경로 필터

1
2
3
4
5
6
7
8
9
10
11
12
13
# 특정 파일 변경 시만
on:
  push:
    paths:
      - 'src/**'
      - '**.js'

# 특정 파일 제외
on:
  push:
    paths-ignore:
      - 'docs/**'
      - '**.md'

스케줄 실행

1
2
3
4
5
6
7
8
9
10
on:
  schedule:
    # 매일 오전 9시 (UTC)
    - cron: '0 9 * * *'
    
    # 매주 월요일 오전 10시
    - cron: '0 10 * * 1'
    
    # 매월 1일 자정
    - cron: '0 0 1 * *'

Cron 표현식:

1
2
3
4
5
6
7
8
9
10
11
12
┌───────── 분 (0-59)
│ ┌─────── 시 (0-23)
│ │ ┌───── 일 (1-31)
│ │ │ ┌─── 월 (1-12)
│ │ │ │ ┌─ 요일 (0-6, 0=일요일)
│ │ │ │ │
* * * * *

예시:
0 9 * * *      # 매일 오전 9시
0 */6 * * *    # 6시간마다
0 0 * * 1      # 매주 월요일 자정

조건부 실행 (if)

기본 조건

1
2
3
4
5
6
7
jobs:
  deploy:
    runs-on: ubuntu-latest
    # main 브랜치일 때만 실행
    if: github.ref == 'refs/heads/main'
    steps:
      - run: echo "배포 시작"

Step 조건

1
2
3
4
5
6
7
8
9
10
11
12
13
steps:
  - name: 테스트
    run: npm test
  
  # 테스트 성공 시에만 실행
  - name: 배포
    if: success()
    run: npm run deploy
  
  # 실패 시에만 실행
  - name: 알림
    if: failure()
    run: echo "테스트 실패!"

조건 함수

1
2
3
4
5
6
7
8
9
10
11
# 항상 실행
if: always()

# 성공 시
if: success()

# 실패 시
if: failure()

# 취소 시
if: cancelled()

복합 조건

1
2
3
4
5
6
7
8
# AND 조건
if: github.ref == 'refs/heads/main' && success()

# OR 조건
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'

# NOT 조건
if: "!cancelled()"

Job 의존성 (needs)

순차 실행

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: npm test
  
  build:
    needs: test  # test 완료 후 실행
    runs-on: ubuntu-latest
    steps:
      - run: npm run build
  
  deploy:
    needs: build  # build 완료 후 실행
    runs-on: ubuntu-latest
    steps:
      - run: npm run deploy

흐름:

1
test → build → deploy

여러 Job 대기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
jobs:
  test-frontend:
    runs-on: ubuntu-latest
    steps:
      - run: npm test
  
  test-backend:
    runs-on: ubuntu-latest
    steps:
      - run: pytest
  
  deploy:
    # 두 테스트 모두 성공 후 실행
    needs: [test-frontend, test-backend]
    runs-on: ubuntu-latest
    steps:
      - run: deploy.sh

흐름:

1
2
3
test-frontend ┐
              ├─→ deploy
test-backend  ┘

조건부 의존성

1
2
3
4
5
6
7
8
9
10
11
12
13
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: npm test
  
  deploy:
    needs: test
    # test가 실패해도 실행
    if: always()
    runs-on: ubuntu-latest
    steps:
      - run: echo "항상 실행"

Job 출력 (outputs)

출력 정의

1
2
3
4
5
6
7
8
jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      version: $
    steps:
      - id: get-version
        run: echo "version=1.2.3" >> $GITHUB_OUTPUT

출력 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      version: $
    steps:
      - id: get-version
        run: echo "version=1.2.3" >> $GITHUB_OUTPUT
  
  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: 버전 사용
        run: echo "배포 버전: $"

매트릭스 전략 (matrix)

여러 버전 테스트

1
2
3
4
5
6
7
8
9
10
11
12
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16, 18, 20]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: $
      - run: npm test

결과: 3개 Job 실행 (Node 16, 18, 20)

다차원 매트릭스

1
2
3
4
5
6
7
8
9
10
11
12
13
jobs:
  test:
    runs-on: $
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        node-version: [16, 18]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: $
      - run: npm test

결과: 6개 Job 실행 (3 OS × 2 Node 버전)

특정 조합 제외

1
2
3
4
5
6
7
strategy:
  matrix:
    os: [ubuntu-latest, windows-latest]
    node-version: [16, 18]
    exclude:
      - os: windows-latest
        node-version: 16

환경 변수 (env)

Workflow 레벨

1
2
3
4
5
6
7
8
9
10
11
name: Deploy

env:
  APP_NAME: my-app
  REGION: us-east-1

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - run: echo "앱: $APP_NAME, 지역: $REGION"

Job 레벨

1
2
3
4
5
6
7
jobs:
  deploy:
    runs-on: ubuntu-latest
    env:
      ENVIRONMENT: production
    steps:
      - run: echo "환경: $ENVIRONMENT"

Step 레벨

1
2
3
4
5
steps:
  - name: 배포
    env:
      API_KEY: $
    run: deploy.sh

실전 예제

PR 자동 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
name: PR Test

on:
  pull_request:
    branches: [main]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
      - run: npm install
      - run: npm run lint
  
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16, 18, 20]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: $
      - run: npm install
      - run: npm test
  
  comment:
    needs: [lint, test]
    runs-on: ubuntu-latest
    if: success()
    steps:
      - name: PR 댓글
        run: echo "모든 테스트 통과!"

정리

완료 체크:

  • 다양한 트리거 이해
  • 조건부 실행 활용
  • Job 의존성 설정
  • 매트릭스 전략 사용

핵심 요약:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
트리거:
- on: push, pull_request, schedule
- branches, paths 필터
- cron 스케줄

조건:
- if: 조건식
- success(), failure(), always()

의존성:
- needs: [job1, job2]
- outputs로 데이터 전달

매트릭스:
- strategy.matrix
- 여러 버전/환경 자동 테스트

다음: Day 84 - Secrets와 환경 관리 →


← Day 83 | 전체 커리큘럼 | Day 84 →

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.