|
| 1 | +name: CI workflow for python projects |
| 2 | + |
| 3 | +# Run the CI test workflow of jobs which includes: |
| 4 | +# - `test`: Run tests (including code checks) using `tox`. |
| 5 | +# - `build`: Build the python package. |
| 6 | +# - `publish-test`: Publish the package to test.pypi.org. |
| 7 | +# - `publish`: Publish the package to pypi.org (also runs `publish-test`). |
| 8 | +# - `release`: Create a GitHub release. |
| 9 | + |
| 10 | +# Configure the workflows here. Each environment variable name should be a |
| 11 | +# wildcard matching the |
| 12 | +# `on-<github.event_name>-<github.ref_type>-<github.ref_name>` format. For |
| 13 | +# example, if the event is a push to a tag `v1.0.0`, the environment variable |
| 14 | +# `on-push-tag-v*` will match. The value of the matching variable will be |
| 15 | +# written to $GITHUB_OUTPUT to set the jobs, python versions, and operating |
| 16 | +# systems to run the workflow on. The first match found is used. |
| 17 | +env: |
| 18 | + on-push-tag-*: | # Push version tag matching "v*", eg. "v1.0.0" |
| 19 | + jobs=["test", "build", "publish", "release"] |
| 20 | + python-version=["3.9", "3.10", "3.11", "3.12", "3.13"] |
| 21 | + os=["ubuntu-latest"] |
| 22 | + on-push-branch-main: | # Push commits to main branch |
| 23 | + jobs=["test", "build", "publish-test"] |
| 24 | + python-version=["3.9", "3.10", "3.11", "3.12", "3.13"] |
| 25 | + os=["ubuntu-latest"] |
| 26 | + on-push-branch-*: | # Push commits to other branches |
| 27 | + jobs=["test", "build"] |
| 28 | + python-version=["3.9", "3.13"] |
| 29 | + os=["ubuntu-latest"] |
| 30 | + on-workflow_dispatch-branch-*: | # Manual trigger of the workflow |
| 31 | + jobs=["test", "build"] |
| 32 | + python-version=["3.9", "3.13"] |
| 33 | + os=["ubuntu-latest"] |
| 34 | +
|
| 35 | +on: |
| 36 | + push: |
| 37 | + branches: ["**"] # Push commits to any branch |
| 38 | + tags: ["v[0-9]*"] # Publish on tags matching "v*", eg. "v1.0.0" |
| 39 | + |
| 40 | +####### Edit above this line - leave the rest of the workflow intact. |
| 41 | + |
| 42 | + workflow_dispatch: # Allow manual triggering of the workflow |
| 43 | + |
| 44 | +jobs: |
| 45 | + config: # Select the workflow config based on the event trigger. |
| 46 | + name: Configure workflow |
| 47 | + outputs: |
| 48 | + jobs: ${{ steps.config.outputs.jobs }} |
| 49 | + os: ${{ steps.config.outputs.os }} |
| 50 | + python-version: ${{ steps.config.outputs.python-version }} |
| 51 | + runs-on: ubuntu-latest |
| 52 | + steps: |
| 53 | + - id: config |
| 54 | + uses: glenn20/python-ci/actions/config@v2 |
| 55 | + with: |
| 56 | + config: ${{ toJson(env) }} |
| 57 | + |
| 58 | + ci-workflow: # Run the CI workflow based on the config. |
| 59 | + name: CI workflow |
| 60 | + needs: config |
| 61 | + uses: glenn20/python-ci/.github/workflows/ci-workflow-tox.yaml@v2 |
| 62 | + with: |
| 63 | + jobs: ${{ needs.config.outputs.jobs }} |
| 64 | + os: ${{ needs.config.outputs.os }} |
| 65 | + python-version: ${{ needs.config.outputs.python-version }} |
| 66 | + |
| 67 | + # We can't use trusted publishing from a reusable workflow in another |
| 68 | + # repository, so the publish workflows must be run from here. |
| 69 | + publish: |
| 70 | + name: Publish to pypi.org |
| 71 | + needs: [config, ci-workflow] |
| 72 | + # Will match "publish-test" or "publish" in the `jobs` json string. |
| 73 | + if: ${{ contains(needs.config.outputs.jobs, 'publish') }} |
| 74 | + environment: |
| 75 | + name: publish-pypi |
| 76 | + url: ${{ steps.publish.outputs.url }} |
| 77 | + permissions: |
| 78 | + id-token: write # Required for trusted publishing |
| 79 | + runs-on: ubuntu-latest |
| 80 | + steps: |
| 81 | + - id: publish |
| 82 | + uses: glenn20/python-ci/actions/publish@v2 |
| 83 | + with: |
| 84 | + test-only: ${{ contains(fromJson(needs.config.outputs.jobs), 'publish') && 'false' || 'true' }} |
| 85 | + |
| 86 | + # We run the github release job here instead of in ci-workflow, as it requires |
| 87 | + # permissions to sign the release and to simplify the workflow dependency |
| 88 | + # graph on the github UI. |
| 89 | + release: |
| 90 | + needs: [config, ci-workflow] |
| 91 | + uses: glenn20/python-ci/.github/workflows/github-release.yaml@v2 |
| 92 | + permissions: |
| 93 | + id-token: write # Required for signing the release |
| 94 | + contents: write # Required for github release |
| 95 | + if: ${{ contains(fromJson(needs.config.outputs.jobs), 'release') }} |
0 commit comments