Understand, optimize, and collaborate on your CI/CD pipelines
Tags matching v*.*.*
When a release is published
python -m pip install --upgrade pip if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pytest --maxfail=1 --disable-warnings -q
python -m pip install --upgrade build python -m build --sdist --wheel
python -m pip install --upgrade twine python -m twine upload dist/*
rm -rf dist build *.egg-info
name: Publish Python Package
on:
push:
tags:
- 'v*.*.*'
release:
types: [published]
permissions:
contents: read
id-token: write
jobs:
test-and-build:
name: Test & Build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Cache pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run pytest
run: pytest --maxfail=1 --disable-warnings -q
- name: Build distributions
run: |
python -m pip install --upgrade build
python -m build --sdist --wheel
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
publish-to-pypi:
name: Publish to PyPI
needs: test-and-build
runs-on: ubuntu-latest
environment:
name: pypi
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- name: Publish with twine
if: startsWith(github.ref, 'refs/tags/')
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: python -m pip install --upgrade twine \
&& python -m twine upload dist/*
- name: Publish with pypa/gh-action-pypi-publish
if: github.event_name == 'release'
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
- name: Cleanup
if: always()
run: rm -rf dist build *.egg-info
Consider caching node_modules to speed up npm/yarn installs.
Use pytest-xdist to run tests in parallel and reduce execution time.
Test against multiple Python versions using a matrix strategy.