CI/CD 파이프라인
GitHub Actions로 테스트 자동화, Docker 이미지 빌드, 배포까지 자동화합니다.
기본 CI 워크플로우
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: testdb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:7-alpine
ports:
- 6379:6379
steps:
- name: 소스 체크아웃
uses: actions/checkout@v4
- name: Python 설정
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"
- name: 의존성 설치
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
- name: 린트 (Ruff)
run: ruff check .
- name: 타입 체크 (mypy)
run: mypy app/
- name: 테스트 실행
env:
DATABASE_URL: postgresql://postgres:password@localhost:5432/testdb
REDIS_URL: redis://localhost:6379/0
SECRET_KEY: test-secret-key-32-chars-minimum!
ENVIRONMENT: testing
run: |
pytest --cov=app --cov-report=xml --cov-report=term-missing -v
- name: 커버리지 업로드
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
CD — Docker 빌드 + 배포
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
tags: ["v*.*.*"]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image-tag: ${{ steps.meta.outputs.tags }}
steps:
- uses: actions/checkout@v4
- name: Docker Buildx 설정
uses: docker/setup-buildx-action@v3
- name: 레지스트리 로그인
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: 메타데이터 추출
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=sha,prefix=sha-
- name: 빌드 및 푸시
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
needs: build-and-push
runs-on: ubuntu-latest
environment: production
steps:
- name: 서버에 배포
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
script: |
cd /opt/myapp
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
docker compose pull app
docker compose up -d --no-deps app
docker image prune -f
전체 파이프라인 (CI + CD 통합)
# .github/workflows/pipeline.yml
name: Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
# ── 1단계: 코드 품질 ─────────────────────────────────
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- run: pip install ruff mypy
- run: ruff check . && ruff format --check .
- run: mypy app/ --ignore-missing-imports
# ── 2단계: 테스트 ────────────────────────────────────
test:
needs: quality
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
- run: pip install -r requirements.txt -r requirements-dev.txt
- run: pytest -x --tb=short
# ── 3단계: 보안 스캔 ─────────────────────────────────
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install bandit safety
- run: bandit -r app/ -ll # 코드 보안 취약점
- run: safety check # 의존성 CVE 검사
# ── 4단계: 배포 (main 브랜치만) ──────────────────────
deploy:
needs: [test, security]
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- name: 프로덕션 배포
run: echo "배포 스크립트 실행"
pyproject.toml — 도구 설정 통합
# pyproject.toml
[tool.ruff]
line-length = 88
target-version = "py312"
select = ["E", "F", "I", "N", "W", "UP"]
ignore = ["E501"]
[tool.ruff.isort]
known-first-party = ["app"]
[tool.mypy]
python_version = "3.12"
strict = true
ignore_missing_imports = true
[tool.pytest.ini_options]
testpaths = ["tests"]
asyncio_mode = "auto"
addopts = "-v --tb=short"
[tool.coverage.run]
source = ["app"]
omit = ["tests/*", "*/migrations/*"]
[tool.coverage.report]
fail_under = 80
정리
| 단계 | 도구 | 목적 |
|---|---|---|
| 린트/포매팅 | Ruff | 코드 스타일 통일 |
| 타입 체크 | mypy | 런타임 오류 사전 방지 |
| 테스트 | pytest + coverage | 기능 검증, 회귀 방지 |
| 보안 스캔 | bandit + safety | 취약점 검출 |
| 이미지 빌드 | Docker Buildx | 컨테이너화 |
| 배포 | SSH/Kubernetes | 프로덕션 적용 |