100일 챌린지 Day 82 - 첫 GitHub Actions Workflow를 만들어봅니다.
배울 내용
- Workflow 파일 생성
- 기본 문법 이해
- 첫 Actions 실행
Workflow 파일 만들기
파일 위치
1
2
3
4
| 프로젝트/
└── .github/
└── workflows/
└── main.yml # Workflow 파일
|
규칙:
.github/workflows/ 디렉토리에 생성 - 파일명: 자유 (예:
main.yml, deploy.yml) - 확장자:
.yml 또는 .yaml
첫 Workflow 만들기
1
2
3
4
5
6
7
8
9
10
11
| # .github/workflows/hello.yml
name: Hello World
on: push
jobs:
greet:
runs-on: ubuntu-latest
steps:
- name: 인사하기
run: echo "Hello, GitHub Actions!"
|
설명:
1
2
3
4
5
| - name: Workflow 이름
- on: 언제 실행할지
- jobs: 어떤 작업을 할지
- runs-on: 어떤 환경에서 실행할지
- steps: 작업의 세부 단계
|
YAML 기본 문법
들여쓰기 (중요!)
1
2
3
4
5
6
7
8
9
10
11
12
| # ✅ 올바른 들여쓰기 (스페이스 2칸)
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 테스트
run: npm test
# ❌ 잘못된 들여쓰기
jobs:
build:
runs-on: ubuntu-latest
|
리스트
1
2
3
4
5
6
7
| # 방법 1: - 사용
steps:
- name: Step 1
- name: Step 2
# 방법 2: [] 사용
branches: [main, develop]
|
문자열
1
2
3
4
5
6
| # 따옴표 없이
name: My Workflow
# 따옴표 사용 (특수문자 포함 시)
name: 'My: Workflow'
name: "My Workflow: Test"
|
Workflow 주요 구성
1. name (이름)
1
2
3
| name: CI
# GitHub Actions 탭에서 보이는 이름
|
2. on (트리거)
1
2
3
4
5
6
7
8
9
10
11
12
13
| # Push 시
on: push
# Pull Request 시
on: pull_request
# 여러 이벤트
on: [push, pull_request]
# 특정 브랜치만
on:
push:
branches: [main]
|
3. jobs (작업)
1
2
3
4
5
6
7
8
9
10
| jobs:
test: # Job 이름
runs-on: ubuntu-latest
steps:
- run: npm test
build: # 다른 Job
runs-on: ubuntu-latest
steps:
- run: npm run build
|
4. steps (단계)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| steps:
# 이름만
- name: 체크아웃
uses: actions/checkout@v3
# 명령 실행
- name: 테스트
run: npm test
# 여러 명령
- name: 빌드 및 테스트
run: |
npm install
npm run build
npm test
|
실습: 간단한 테스트 Workflow
1. 프로젝트 준비
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # 프로젝트 초기화
mkdir my-actions-test
cd my-actions-test
git init
# package.json 생성
npm init -y
# 간단한 테스트 스크립트 추가
# package.json의 scripts에 추가
{
"scripts": {
"test": "echo '테스트 통과!'"
}
}
|
2. Workflow 파일 생성
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
| # 디렉토리 생성
mkdir -p .github/workflows
# Workflow 파일 생성
cat > .github/workflows/test.yml << 'YAML'
name: Run Tests
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: 코드 체크아웃
uses: actions/checkout@v3
- name: Node.js 설정
uses: actions/setup-node@v3
with:
node-version: '18'
- name: 의존성 설치
run: npm install
- name: 테스트 실행
run: npm test
YAML
|
3. Git에 커밋 및 푸시
1
2
3
4
5
| git add .
git commit -m "Add GitHub Actions workflow"
git branch -M main
git remote add origin https://github.com/사용자명/저장소.git
git push -u origin main
|
Actions 실행 확인
GitHub에서 확인
1
2
3
4
| 1. GitHub 저장소 접속
2. "Actions" 탭 클릭
3. 실행 중인 Workflow 확인
4. Job 클릭 → 각 Step 결과 확인
|
실행 상태
1
2
3
| 🟡 노란색 점: 실행 중
✅ 초록 체크: 성공
❌ 빨간 X: 실패
|
자주 사용하는 Actions
actions/checkout
코드 가져오기
1
2
| steps:
- uses: actions/checkout@v3
|
actions/setup-node
Node.js 설정
1
2
3
4
| steps:
- uses: actions/setup-node@v3
with:
node-version: '18'
|
actions/setup-python
Python 설정
1
2
3
4
| steps:
- uses: actions/setup-python@v4
with:
python-version: '3.11'
|
환경 변수 사용
기본 사용법
1
2
3
4
5
6
7
8
| steps:
- name: 환경 변수 사용
run: echo "Branch는 $"
- name: 커스텀 환경 변수
env:
MY_VAR: "Hello"
run: echo $MY_VAR
|
GitHub 제공 변수
1
2
3
4
5
6
7
8
9
10
11
| # 저장소 이름
$
# 브랜치 이름
$
# 커밋 SHA
$
# 이벤트 이름 (push, pull_request 등)
$
|
정리
완료 체크:
- Workflow 파일 생성
- 기본 YAML 문법 이해
- Actions 실행 및 확인
핵심 요약:
1
2
3
4
5
6
7
8
9
10
| Workflow 파일:
- 위치: .github/workflows/
- 형식: YAML
- 구성: name, on, jobs, steps
기본 흐름:
1. 파일 생성
2. Git Push
3. 자동 실행
4. 결과 확인
|
다음: Day 83 - Workflow 문법 심화 →
← Day 82 | 전체 커리큘럼 | Day 83 →