GitHub Actions 완벽 가이드 | CI/CD·자동화·Workflow·배포·실전 활용
이 글의 핵심
GitHub Actions로 CI/CD를 구축하는 완벽 가이드입니다. Workflow 작성, 테스트 자동화, 배포 파이프라인, Secrets 관리까지 실전 예제로 정리했습니다.
실무 경험 공유: Jenkins에서 GitHub Actions로 전환하면서, 설정이 간소화되고 배포 속도가 3배 빨라진 경험을 공유합니다.
들어가며: “CI/CD가 복잡해요”
실무 문제 시나리오
시나리오 1: Jenkins 설정이 어려워요
서버 관리가 필요합니다. GitHub Actions는 클라우드 기반입니다.
시나리오 2: 배포가 느려요
순차 실행이 느립니다. GitHub Actions는 병렬 실행을 지원합니다.
시나리오 3: 비용이 높아요
서버 비용이 듭니다. GitHub Actions는 Public 저장소는 무료입니다.
1. GitHub Actions란?
핵심 특징
GitHub Actions는 CI/CD 자동화 플랫폼입니다.
주요 장점:
- Git 통합: GitHub 네이티브
- 무료: Public 저장소 무료
- Marketplace: 수천 개의 Action
- 병렬 실행: 빠른 빌드
- 간단한 설정: YAML 파일
2. 기본 Workflow
Hello World
# .github/workflows/hello.yml
name: Hello World
on:
push:
branches: [main]
jobs:
greet:
runs-on: ubuntu-latest
steps:
- name: Say hello
run: echo "Hello, GitHub Actions!"
3. CI Workflow
Node.js 테스트
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20]
steps:
- uses: actions/checkout@v4
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run tests
run: npm test
- name: Build
run: npm run build
4. 배포 Workflow
Vercel 배포
# .github/workflows/deploy.yml
name: Deploy to Vercel
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Vercel CLI
run: npm install -g vercel
- name: Deploy to Vercel
run: vercel --prod --token=${{ secrets.VERCEL_TOKEN }}
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
Docker 배포
name: Build and Push Docker
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: user/app:latest
5. Secrets 관리
설정
# GitHub → Settings → Secrets and variables → Actions
# New repository secret
사용
steps:
- name: Deploy
run: ./deploy.sh
env:
API_KEY: ${{ secrets.API_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
6. 조건부 실행
if 조건
jobs:
deploy:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy
run: ./deploy.sh
notify:
runs-on: ubuntu-latest
if: failure()
steps:
- name: Send notification
run: ./notify.sh
7. 캐싱
의존성 캐싱
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm ci
커스텀 캐싱
steps:
- uses: actions/cache@v3
with:
path: |
~/.npm
.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-nextjs-
8. Marketplace Actions
인기 Actions
# Checkout
- uses: actions/checkout@v4
# Node.js 설정
- uses: actions/setup-node@v4
# 캐싱
- uses: actions/cache@v3
# 아티팩트 업로드
- uses: actions/upload-artifact@v3
with:
name: build
path: dist/
# 슬랙 알림
- uses: slackapi/slack-github-action@v1
with:
payload: |
{
"text": "Deployment successful!"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
9. 실전 예제
Monorepo CI
name: Monorepo CI
on: [push, pull_request]
jobs:
changes:
runs-on: ubuntu-latest
outputs:
web: ${{ steps.filter.outputs.web }}
api: ${{ steps.filter.outputs.api }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
web:
- 'apps/web/**'
api:
- 'apps/api/**'
test-web:
needs: changes
if: needs.changes.outputs.web == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm test --workspace=web
test-api:
needs: changes
if: needs.changes.outputs.api == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm test --workspace=api
정리 및 체크리스트
핵심 요약
- GitHub Actions: CI/CD 자동화
- Git 통합: GitHub 네이티브
- 무료: Public 저장소 무료
- Marketplace: 수천 개의 Action
- 병렬 실행: 빠른 빌드
- 간단한 설정: YAML 파일
구현 체크리스트
- Workflow 파일 작성
- CI 구현
- 배포 파이프라인 구현
- Secrets 설정
- 캐싱 추가
- 조건부 실행 설정
- Marketplace Actions 활용
같이 보면 좋은 글
- Docker Compose 완벽 가이드
- Terraform 실전 가이드
- Kubernetes 실전 가이드
이 글에서 다루는 키워드
GitHub Actions, CI/CD, Automation, Workflow, DevOps, Deployment, Testing
자주 묻는 질문 (FAQ)
Q. Jenkins와 비교하면 어떤가요?
A. GitHub Actions가 훨씬 간단하고 GitHub 통합이 완벽합니다. Jenkins는 더 많은 커스터마이징을 제공합니다.
Q. 무료로 사용할 수 있나요?
A. 네, Public 저장소는 무료입니다. Private 저장소는 2,000분/월 무료입니다.
Q. Self-hosted Runner를 사용할 수 있나요?
A. 네, 자체 서버에서 실행할 수 있습니다.
Q. 프로덕션에서 사용해도 되나요?
A. 네, 수많은 오픈소스와 기업에서 사용하고 있습니다.