⚙️ Github Action 설정 및 캐싱 가이드
🚀 Github Action으로 자동 테스트 실행
📄 .github/workflows/ci.yml 생성
name: CI
on:
push:
jobs:
static-analysis: # mypy, black, ruff등 정적 분석
runs-on: ubuntu-22.04 # 실제 프로덕션에서는 모든버전을 고정하는 것이좋다. 예기치못한 장애방지
steps:
- name: Check out the codes
uses: actions/checkout@v2
- name: Setup python environment
id: setup-python
uses: actions/setup-python@v2
with:
python-version: "3.13"
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.5
- name: Register Poetry bin
run: echo "${HOME}/.poetry/bin" >> $GITHUB_PATH
- name: Install dependencies
run: poetry install --no-root
- name: Run Black
run: poetry run black . --check
- name: Run Ruff
run: |
poetry run ruff check --select I
poetry run ruff check
- name: Run Mypy
run: poetry run mypy .
test: # 전체 테스트 실행
runs-on: ubuntu-22.04
steps:
- name: Check out the codes
uses: actions/checkout@v2
- name: Setup python environment
id: setup-python
uses: actions/setup-python@v2
with:
python-version: "3.13"
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.5
- name: Register Poetry bin
run: echo "${HOME}/.poetry/bin" >> $GITHUB_PATH
- name: Install dependencies
run: poetry install --no-root
- name: Run tests
run: |
poetry run coverage run -m pytest .
poetry run coverage report -m
🔍 주요 항목 설명
- on: push: 코드가 push될 때마다 자동 실행
- https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#on
- jobs: 병렬로 실행할 job 정의 (static-analysis, test) / 병렬실행을통해 시각 감소
- https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobs
- runs-on: job이 실행되는 machine을 의미합니다.
- https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idruns-on
- steps : 하나의 job 은 여러개의 step 으로 구성됩니다. step 은 명령을 실행하거나 다른 action 을 실행합니다.
- run: shell 명령 실행
- with : action 에 전달할 parameter 변수입니다.
- uses: GitHub 제공 액션 사용
- run: |: 여러 줄 명령 실행
💡 CI 성공 여부는 PR과 작업 화면에서 확인 가능
⚡ 고급 기능: 캐싱 설정으로 속도 개선
캐시 설정을 통해 Poetry 설치 및 종속성 설치 시간 절감 가능
📄 캐싱 포함 ci.yml 예시
name: CI
on:
push:
pull_request:
jobs:
static-analysis:
runs-on: ubuntu-22.04
steps:
- name: Check out the codes
uses: actions/checkout@v3
- name: Setup Python environment
id: setup-python
uses: actions/setup-python@v4
with:
python-version: "3.13"
- name: Cache Poetry
id: cache-poetry
uses: actions/cache@v3
with:
path: ~/.local/
key: poetry-1.8.5
- name: Install Poetry
if: steps.cache-poetry.outputs.cache-hit != 'true'
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.5
- name: Register Poetry bin
run: echo "${HOME}/.poetry/bin" >> $GITHUB_PATH
- name: Cache dependencies
id: cache-venv
uses: actions/cache@v3
with:
path: /home/runner/.cache/pypoetry/virtualenvs/
key: python-${{ steps.setup-python.outputs.python-version }}-poetry-${{ hashFiles('poetry.lock') }}-${{ hashFiles('pyproject.toml') }}-v1
- name: Install dependencies
if: steps.cache-venv.outputs.cache-hit != 'true'
run: poetry install --no-root
- name: Run Black
run: poetry run black . --check
- name: Run Ruff
run: |
poetry run ruff check --select I --fix
poetry run ruff check --fix
- name: Run Mypy
run: poetry run mypy .
test:
runs-on: ubuntu-22.04
steps:
- name: Check out the codes
uses: actions/checkout@v3
- name: Setup Python environment
id: setup-python
uses: actions/setup-python@v4
with:
python-version: "3.13"
- name: Cache Poetry
id: cache-poetry
uses: actions/cache@v3
with:
path: ~/.local/
key: poetry-1.8.5
- name: Install Poetry
if: steps.cache-poetry.outputs.cache-hit != 'true'
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.5
- name: Register Poetry bin
run: echo "${HOME}/.poetry/bin" >> $GITHUB_PATH
- name: Cache dependencies
id: cache-venv
uses: actions/cache@v3
with:
path: /home/runner/.cache/pypoetry/virtualenvs/
key: python-${{ steps.setup-python.outputs.python-version }}-poetry-${{ hashFiles('poetry.lock') }}-${{ hashFiles('pyproject.toml') }}-v1
- name: Install dependencies
if: steps.cache-venv.outputs.cache-hit != 'true'
run: poetry install --no-root
- name: Run tests
run: |
poetry run coverage run -m pytest .
poetry run coverage report -m
📌 캐싱 효과:
- CI 속도 개선 (특히 poetry install 단계)
- 종속성이 변하지 않는 한 재설치 없이 실행
- 성공적으로 캐시가 적용된 스텝은 "캐시에서 불러옴" 상태로 표시
✅ Recap
- ✅ GitHub Action으로 CI 자동화 구성
- ✅ push 할 때마다 코드 품질 검사 및 테스트 실행
- ✅ jobs 분리로 병렬 처리 및 속도 향상
- ✅ 캐싱 설정으로 CI 속도 최적화
CI/CD는 협업을 위한 '자동화된 안전망'입니다.
프로젝트 품질 유지와 배포 안정성을 위해 꼭 활용하세요.
'Fastapi' 카테고리의 다른 글
Chapter 3-4 Base62 디버깅과 디버깅 스킬의 중요성 (0) | 2025.05.27 |
---|---|
Chapter 3-1 미팅 생성 API 스펙 만들기 (0) | 2025.05.27 |
Chapter 2-5 테스트 스크립트 작성 가이드 (0) | 2025.05.23 |
Chapter 2-4 자동화 테스트가 필요한 이유 (0) | 2025.05.23 |
Chapter 2-3 Dev Dependency 개념 및 활용 가이드(의존성 관리) (0) | 2025.05.23 |