| 1 |
# Release the package on GitHub and publish it to PyPI. |
| 2 |
|
| 3 |
name: Release and Publish |
| 4 |
|
| 5 |
on: |
| 6 |
workflow_dispatch: |
| 7 |
inputs: |
| 8 |
ref: |
| 9 |
description: Branch or commit SHA to publish and tag |
| 10 |
type: string |
| 11 |
required: true |
| 12 |
tag: |
| 13 |
description: Tag to apply |
| 14 |
type: string |
| 15 |
required: true |
| 16 |
|
| 17 |
jobs: |
| 18 |
release: |
| 19 |
runs-on: ubuntu-latest |
| 20 |
permissions: |
| 21 |
contents: write |
| 22 |
steps: |
| 23 |
- name: Checkout code |
| 24 |
uses: actions/[email protected] |
| 25 |
with: |
| 26 |
ref: ${{ inputs.ref }} |
| 27 |
|
| 28 |
- name: Install uv |
| 29 |
uses: astral-sh/[email protected] |
| 30 |
|
| 31 |
- name: Install python |
| 32 |
uses: actions/[email protected] |
| 33 |
with: |
| 34 |
# Installs the latest version compatible with the requires-python field |
| 35 |
python-version-file: pyproject.toml |
| 36 |
|
| 37 |
- name: Install the project |
| 38 |
run: uv sync --all-extras --dev |
| 39 |
|
| 40 |
- name: Get package version |
| 41 |
id: version |
| 42 |
run: | |
| 43 |
VERSION=$(uvx --from=toml-cli toml get --toml-path=pyproject.toml project.version) |
| 44 |
echo "package_version=$VERSION" >> $GITHUB_OUTPUT |
| 45 |
|
| 46 |
- name: Determine if version is a candidate (prerelease) |
| 47 |
id: candidate |
| 48 |
run: | |
| 49 |
VERSION="${{ steps.version.outputs.package_version }}" |
| 50 |
# Test against the regex |
| 51 |
if [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 52 |
echo "is_candidate=false" >> $GITHUB_OUTPUT |
| 53 |
else |
| 54 |
echo "is_candidate=true" >> $GITHUB_OUTPUT |
| 55 |
fi |
| 56 |
|
| 57 |
- name: Validate candidate branch |
| 58 |
id: candidate-branch |
| 59 |
run: | |
| 60 |
if [ "${{ steps.candidate.outputs.is_candidate }}" = "true" ] && [ "${{ github.ref_name }}" = "main" ]; then |
| 61 |
echo "Error: Candidate versions must NOT be released from 'main' branch" |
| 62 |
exit 1 |
| 63 |
fi |
| 64 |
if [ "${{ steps.candidate.outputs.is_candidate }}" != "true" ] && [ "${{ github.ref_name }}" != "main" ]; then |
| 65 |
echo "Error: Non-candidate versions must be released from 'main' branch only" |
| 66 |
exit 1 |
| 67 |
fi |
| 68 |
|
| 69 |
- name: Verify given tag matches package version |
| 70 |
# Since we trigger releases manually, this cross-checks the user's intention |
| 71 |
id: verify-tag |
| 72 |
run: | |
| 73 |
if [ "${{ steps.version.outputs.package_version }}" != "${{ inputs.tag }}" ]; then |
| 74 |
echo "::error ::Provided tag ('${{ inputs.tag }}') does not match the package version ('${{ steps.version.outputs.package_version }}')." |
| 75 |
exit 1 |
| 76 |
fi |
| 77 |
|
| 78 |
- uses: ncipollo/release-action@v1 |
| 79 |
with: |
| 80 |
draft: false |
| 81 |
generateReleaseNotes: true |
| 82 |
commit: ${{ inputs.ref }} |
| 83 |
tag: ${{ inputs.tag || steps.version.outputs.package_version }} |
| 84 |
prerelease: ${{ steps.candidate.outputs.is_candidate }} |
| 85 |
skipIfReleaseExists: true |
| 86 |
|
| 87 |
publish: |
| 88 |
runs-on: ubuntu-latest |
| 89 |
permissions: |
| 90 |
id-token: write |
| 91 |
needs: release |
| 92 |
steps: |
| 93 |
- name: Checkout code |
| 94 |
uses: actions/[email protected] |
| 95 |
with: |
| 96 |
ref: ${{ inputs.ref }} |
| 97 |
|
| 98 |
- name: Install uv |
| 99 |
uses: astral-sh/[email protected] |
| 100 |
|
| 101 |
- name: Install python |
| 102 |
uses: actions/[email protected] |
| 103 |
with: |
| 104 |
# Installs the latest version compatible with the requires-python field |
| 105 |
python-version-file: pyproject.toml |
| 106 |
|
| 107 |
- name: Install the project |
| 108 |
run: uv build |
| 109 |
|
| 110 |
- name: Publish package distributions to PyPI |
| 111 |
uses: pypa/[email protected] |