Skip to content

GitHub:Actions:Python

GitHub Actions 에서 Python 프로젝트 돌리는 방법.

Test Workflow

name: Test

on:
  push:
    branches:
      - master

env:
  USE_SYSTEM_PYTHON: python3

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 1

      - name: setup python 3.9
        uses: actions/setup-python@v1
        with:
          python-version: 3.9

      - name: pip caching
        uses: actions/cache@v1
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.*.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Install dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.txt
      - name: Test with black
        run: |
          ./black.sh
      - name: Test with flake8
        run: |
          ./flake8.sh
      - name: Test with isort
        run: |
          ./isort.sh
      - name: Test with mypy
        run: |
          ./mypy.sh
      - name: Test with pytest
        run: |
          ./pytest.sh

Deploy Workflow

PyPI 의 Account settings > Add API token 페이지로 가서, 원하는 프로젝트의 토큰을 발급받는다. 다음과 같이 출력된다.

  • Permissions: Upload packages
  • Scope: Project "async-receiver"

토큰을 복사하고, Github 프로젝트의 Settings > Security > Secrets > Actions 페이지에서 "New repository secret" 버튼을 클릭하여 위에서 추가한 토큰을 붙여넣는다.

name: deploy

on:
  release:
    types: [published]

permissions:
  contents: read

env:
  USE_SYSTEM_PYTHON: python3

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        uses: actions/checkout@v1
        with:
          fetch-depth: 1

      - name: setup python 3.9
        uses: actions/setup-python@v1
        with:
          python-version: 3.9

      - name: pip caching
        uses: actions/cache@v1
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('requirements.deploy.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Install dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.deploy.txt
      - name: Build package
        run: |
          ./build.sh
      - name: Publish package
        uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
        with:
          user: __token__
          password: ${{ secrets.PYPI_API_TOKEN }}

See also

Favorite site