Test, Docs & Publish #6
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This workflow handles testing, documentation, and publishing for the project. | |
# It runs on pull requests to main and when releases are published. | |
# The workflow ensures that: | |
# 1. Tests pass before any other operations | |
# 2. Documentation is built and deployed (deployment only on release) | |
# 3. Package is published to PyPI (only on release and after docs are deployed) | |
name: Test, Docs & Publish | |
on: | |
# Run on pull requests to main branch to verify changes | |
pull_request: | |
branches: | |
- main | |
# Run when a new release is published to deploy docs and package | |
release: | |
types: [published] | |
# Default permissions are read-only | |
permissions: | |
contents: read | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: "3.x" # tox manages specific versions | |
cache: "pip" | |
# Cache test data | |
- name: Cache test data | |
uses: actions/cache@v4 | |
with: | |
path: data/ | |
key: test-data-${{ hashFiles('tests/conftest.py') }} | |
restore-keys: test-data- | |
# Cache tox environments | |
- name: Cache tox environments | |
uses: actions/cache@v4 | |
with: | |
path: .tox | |
key: tox-${{ runner.os }}-${{ hashFiles('pyproject.toml') }} | |
restore-keys: | | |
tox-${{ runner.os }}- | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
python -m pip install tox tox-gh-actions | |
- name: Run tests | |
run: python -m tox | |
build-docs: | |
# Only build documentation after tests have passed | |
needs: test | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.x' | |
cache: 'pip' | |
- name: Install package with docs dependencies | |
run: | | |
python -m pip install --upgrade pip | |
python -m pip install '.[docs]' | |
# DOC_ALLOW_EXEC=1 allows executing code examples in docstrings | |
- name: Build documentation | |
run: DOC_ALLOW_EXEC=1 pdoc --docformat google -o docs/ snputils | |
# Upload documentation as an artifact for deployment | |
- name: Upload documentation | |
uses: actions/upload-pages-artifact@v3 | |
with: | |
path: docs/ | |
deploy-docs: | |
# Deploy documentation to GitHub Pages only on release | |
# This job requires additional permissions for GitHub Pages | |
if: github.event_name == 'release' && github.event.action == 'published' | |
needs: build-docs | |
runs-on: ubuntu-latest | |
permissions: | |
pages: write # Required for deploying to GitHub Pages | |
id-token: write # Required for authentication | |
environment: | |
name: github-pages | |
url: ${{ steps.deployment.outputs.page_url }} | |
steps: | |
- name: Deploy to GitHub Pages | |
id: deployment | |
uses: actions/deploy-pages@v4 | |
deploy-pypi: | |
# Deploy to PyPI only on release and after both tests pass and docs are deployed | |
# This ensures the documentation is available when the package is published | |
needs: [test, deploy-docs] | |
if: github.event_name == 'release' && github.event.action == 'published' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.x' | |
cache: 'pip' | |
- name: Install build dependencies | |
run: | | |
python -m pip install --upgrade pip | |
python -m pip install build | |
- name: Build package | |
run: python -m build | |
# Publish to PyPI using API token | |
# Token must be configured in repository secrets | |
- name: Publish package | |
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 | |
with: | |
user: __token__ | |
password: ${{ secrets.PYPI_API_TOKEN }} |