GitHub:Actions:Python
lastmod 2025-02-14created 2022-12-063 edits
How to run a Python project on GitHub Actions.
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
Go to the Account settings > Add API token page on PyPI and issue a token for the desired project. It is shown as follows.
- Permissions: Upload packages
- Scope: Project "async-receiver"
Copy the token, then on the Settings > Security > Secrets > Actions page of the Github project, click the "New repository secret" button and paste the token added above.
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
- Building and testing Python - GitHub Docs
- Github - pypa/gh-action-pypi-publish - pypa/gh-action-pypi-publish: GitHub Action, for publishing distribution files to PyPI