Chapter 2-6 Github Action 설정 및 캐싱 가이드

2025. 5. 23. 10:36·Fastapi

⚙️ 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
'Fastapi' 카테고리의 다른 글
  • Chapter 3-4 Base62 디버깅과 디버깅 스킬의 중요성
  • Chapter 3-1 미팅 생성 API 스펙 만들기
  • Chapter 2-5 테스트 스크립트 작성 가이드
  • Chapter 2-4 자동화 테스트가 필요한 이유
Chansman
Chansman
안녕하세요! 코딩을 시작한 지 얼마 되지 않은 초보 개발자 찬스맨입니다. 이 블로그는 제 학습 기록을 남기고, 다양한 코딩 실습을 통해 성장하는 과정을 공유하려고 합니다. 초보자의 눈높이에 맞춘 실습과 팁, 그리고 개발하면서 겪은 어려움과 해결 과정을 솔직하게 풀어내려 합니다. 함께 성장하는 개발자 커뮤니티가 되기를 바랍니다.
  • Chansman
    찬스맨의 프로그래밍 스토리
    Chansman
  • 전체
    오늘
    어제
    • 분류 전체보기 (777) N
      • Python (32)
      • 프로젝트 (101) N
      • 과제 (25)
      • Database (40)
      • 멘토링 (11)
      • 특강 (37)
      • 기술블로그 (41)
      • 기술블로그-Fastapi편 (33)
      • 기술블로그-Django편 (153)
      • 기술블로그-Flask편 (36)
      • AI 분석 (5)
      • HTML & CSS (31)
      • JavaScript (17)
      • AWS_Cloud (21)
      • 웹스크래핑과 데이터 수집 (14)
      • Flask (42)
      • Django (77)
      • Fastapi (16)
      • 연예 (14)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    btsjungkook
    self-growth
    homebartrend
    btsreunion
    remittance
    RM
    chatgpterror
    newpoliticalparty
    뷔
    global politics
    lawsuitculture
    youngprofessionals
    trumpmuskclash
    college reunions
    urbantrends
    hotcoffeecase
    basalcellcarcinoma
    life reflection
    gpterror
    btscomeback
    travel ban
    livebroadcast
    americanlaw
    titaniumcase
    btsdischarge
    chinanightlife
    bts
    classaction
    smartphonedurability
    americaparty
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Chansman
Chapter 2-6 Github Action 설정 및 캐싱 가이드
상단으로

티스토리툴바