Skip to content

Commit 0a19f08

Browse files
committed
update release workflow to read version from pyproject
1 parent 31d4433 commit 0a19f08

File tree

1 file changed

+124
-81
lines changed

1 file changed

+124
-81
lines changed

.github/workflows/release.yml

Lines changed: 124 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,97 @@
1-
name: Publish Python Package
1+
name: Release
22

33
on:
4+
# Fires when the *Lint and Format Code* workflow that was
5+
# started by a push to main has finished (any conclusion)
46
workflow_run:
57
workflows: ["Lint and Format Code"]
6-
types:
7-
- completed
8-
branches:
9-
- main
10-
11-
release:
12-
types: [published]
8+
branches: [main]
9+
types: [completed]
1310

1411
jobs:
12+
# ────────────────────────────────────────────────────────────────────
13+
# 1) Pre-check — decide whether we really need to release
14+
# ────────────────────────────────────────────────────────────────────
15+
pre-check:
16+
name: Detect new version
17+
if: ${{ github.event.workflow_run.conclusion == 'success' }} # gate #1
18+
runs-on: ubuntu-latest
19+
outputs:
20+
release_needed: ${{ steps.version_diff.outputs.release_needed }}
21+
version: ${{ steps.version_diff.outputs.version }}
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
with:
27+
ref: ${{ github.event.workflow_run.head_sha }}
28+
fetch-depth: 0 # we need history to read the previous commit
29+
30+
- name: Compare versions
31+
id: version_diff
32+
shell: bash
33+
# gate #2 happens here → sets release_needed=true/false
34+
run: |
35+
# version in current pyproject.toml
36+
NEW_VERSION=$(grep -Po '(?<=^version = ")[^"]+' pyproject.toml)
37+
# version in the previous commit's pyproject.toml (if the file existed)
38+
BASE_COMMIT=$(git rev-parse "$GITHUB_SHA"^)
39+
if git cat-file -e "$BASE_COMMIT":pyproject.toml 2>/dev/null; then
40+
OLD_VERSION=$(git show "$BASE_COMMIT":pyproject.toml \
41+
| grep -Po '(?<=^version = ")[^"]+' || true)
42+
else
43+
OLD_VERSION=""
44+
fi
45+
46+
echo "Previous version: $OLD_VERSION"
47+
echo "Current version: $NEW_VERSION"
48+
49+
if [[ "$NEW_VERSION" != "$OLD_VERSION" ]]; then
50+
echo "release_needed=true" >>"$GITHUB_OUTPUT"
51+
else
52+
echo "release_needed=false" >>"$GITHUB_OUTPUT"
53+
fi
54+
echo "version=$NEW_VERSION" >>"$GITHUB_OUTPUT"
55+
56+
# ────────────────────────────────────────────────────────────────────
57+
# 2) Build
58+
# ────────────────────────────────────────────────────────────────────
1559
build:
1660
name: Build distribution 📦
61+
needs: pre-check
62+
if: ${{ needs.pre-check.outputs.release_needed == 'true' }} # gate #3
1763
runs-on: ubuntu-latest
1864

1965
steps:
20-
- name: Checkout code
21-
uses: actions/checkout@v4
22-
with:
23-
ref: ${{ github.head_ref }}
24-
25-
- name: Set up Python
26-
uses: actions/setup-python@v5
27-
with:
28-
python-version: "3.12"
29-
30-
- name: Install build dependencies
31-
run: >-
32-
python3 -m pip install build --user
33-
34-
- name: Build a source distribution and a wheel
35-
run: python3 -m build
36-
37-
- name: Store the distribution packages
38-
uses: actions/upload-artifact@v4
39-
with:
40-
name: python-package-distributions
41-
path: dist/
42-
66+
- name: Checkout code
67+
uses: actions/checkout@v4
68+
with:
69+
ref: ${{ github.event.workflow_run.head_sha }}
70+
71+
- name: Set up Python
72+
uses: actions/setup-python@v5
73+
with:
74+
python-version: "3.12"
75+
76+
- name: Install build dependencies
77+
run: python3 -m pip install --user build
78+
79+
- name: Build sdist & wheel
80+
run: python3 -m build
81+
82+
- name: Store the distribution packages
83+
uses: actions/upload-artifact@v4
84+
with:
85+
name: python-package-distributions
86+
path: dist/
87+
88+
# ────────────────────────────────────────────────────────────────────
89+
# 3) Publish to PyPI
90+
# ────────────────────────────────────────────────────────────────────
4391
publish-to-pypi:
44-
name: >-
45-
Publish Python 🐍 distribution PyPI
46-
needs:
47-
- build
92+
name: Publish Python 🐍 distribution to PyPI
93+
needs: [pre-check, build]
94+
if: ${{ needs.pre-check.outputs.release_needed == 'true' }}
4895
runs-on: ubuntu-latest
4996
environment:
5097
name: release
@@ -53,56 +100,52 @@ jobs:
53100
id-token: write
54101

55102
steps:
56-
- name: Download the dists
57-
uses: actions/download-artifact@v4
58-
with:
59-
name: python-package-distributions
60-
path: dist/
61-
62-
- name: Publish to PyPI
63-
uses: pypa/gh-action-pypi-publish@release/v1
64-
103+
- name: Download the dists
104+
uses: actions/download-artifact@v4
105+
with:
106+
name: python-package-distributions
107+
path: dist/
108+
109+
- name: Publish to PyPI
110+
uses: pypa/gh-action-pypi-publish@release/v1
111+
112+
# ────────────────────────────────────────────────────────────────────
113+
# 4) GitHub Release
114+
# ────────────────────────────────────────────────────────────────────
65115
github-release:
66-
name: >-
67-
Create GitHub Release
68-
needs:
69-
- publish-to-pypi
116+
name: Create GitHub Release
117+
needs: [pre-check, publish-to-pypi]
118+
if: ${{ needs.pre-check.outputs.release_needed == 'true' }}
70119
runs-on: ubuntu-latest
71-
72120
permissions:
73121
contents: write
74122
id-token: write
75123

76124
steps:
77-
- name: Download dists
78-
uses: actions/download-artifact@v4
79-
with:
80-
name: python-package-distributions
81-
path: dist/
82-
83-
- name: Sign dists with Sigstore
84-
uses: sigstore/gh-action-sigstore-python@v3.0.0
85-
with:
86-
inputs: >-
87-
./dist/*.tar.gz
88-
./dist/*.whl
89-
90-
- name: Create GitHub Release
91-
env:
92-
GITHUB_TOKEN: ${{ github.token }}
93-
run: >-
94-
gh release create
95-
'v0.2.12'
96-
--repo '${{ github.repository }}'
97-
--notes ""
98-
99-
- name: Upload artifact signatures to GitHub Release
100-
env:
101-
GITHUB_TOKEN: ${{ github.token }}
102-
# Upload to GitHub Release using the `gh` CLI.
103-
# `dist/` contains the built packages, and the
104-
# sigstore-produced signatures and certificates.
105-
run: >-
106-
gh release upload
107-
'v0.2.12' dist/**
108-
--repo '${{ github.repository }}'
125+
- name: Download the dists
126+
uses: actions/download-artifact@v4
127+
with:
128+
name: python-package-distributions
129+
path: dist/
130+
131+
- name: Sign dists with Sigstore
132+
uses: sigstore/gh-action-sigstore-python@v3.0.0
133+
with:
134+
inputs: |
135+
./dist/*.tar.gz
136+
./dist/*.whl
137+
138+
- name: Create GitHub Release
139+
env:
140+
GITHUB_TOKEN: ${{ github.token }}
141+
run: |
142+
gh release create "v${{ needs.pre-check.outputs.version }}" \
143+
--repo "${{ github.repository }}" \
144+
--notes ""
145+
146+
- name: Upload artefacts to Release
147+
env:
148+
GITHUB_TOKEN: ${{ github.token }}
149+
run: |
150+
gh release upload "v${{ needs.pre-check.outputs.version }}" dist/** \
151+
--repo "${{ github.repository }}"

0 commit comments

Comments
 (0)