-
Notifications
You must be signed in to change notification settings - Fork 8
151 lines (131 loc) · 5.85 KB
/
release.yml
File metadata and controls
151 lines (131 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
name: Release
on:
# Fires when the *Lint and Format Code* workflow that was
# started by a push to main has finished (any conclusion)
workflow_run:
workflows: ["Lint and Format Code"]
branches: [main]
types: [completed]
jobs:
# ────────────────────────────────────────────────────────────────────
# 1) Pre-check — decide whether we really need to release
# ────────────────────────────────────────────────────────────────────
pre-check:
name: Detect new version
if: ${{ github.event.workflow_run.conclusion == 'success' }} # gate #1
runs-on: ubuntu-latest
outputs:
release_needed: ${{ steps.version_diff.outputs.release_needed }}
version: ${{ steps.version_diff.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0 # we need history to read the previous commit
- name: Compare versions
id: version_diff
shell: bash
# gate #2 happens here → sets release_needed=true/false
run: |
# version in current pyproject.toml
NEW_VERSION=$(grep -Po '(?<=^version = ")[^"]+' pyproject.toml)
# version in the previous commit's pyproject.toml (if the file existed)
BASE_COMMIT=$(git rev-parse "$GITHUB_SHA"^)
if git cat-file -e "$BASE_COMMIT":pyproject.toml 2>/dev/null; then
OLD_VERSION=$(git show "$BASE_COMMIT":pyproject.toml \
| grep -Po '(?<=^version = ")[^"]+' || true)
else
OLD_VERSION=""
fi
echo "Previous version: $OLD_VERSION"
echo "Current version: $NEW_VERSION"
if [[ "$NEW_VERSION" != "$OLD_VERSION" ]]; then
echo "release_needed=true" >>"$GITHUB_OUTPUT"
else
echo "release_needed=false" >>"$GITHUB_OUTPUT"
fi
echo "version=$NEW_VERSION" >>"$GITHUB_OUTPUT"
# ────────────────────────────────────────────────────────────────────
# 2) Build
# ────────────────────────────────────────────────────────────────────
build:
name: Build distribution 📦
needs: pre-check
if: ${{ needs.pre-check.outputs.release_needed == 'true' }} # gate #3
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install build dependencies
run: python3 -m pip install --user build
- name: Build sdist & wheel
run: python3 -m build
- name: Store the distribution packages
uses: actions/upload-artifact@v4
with:
name: python-package-distributions
path: dist/
# ────────────────────────────────────────────────────────────────────
# 3) Publish to PyPI
# ────────────────────────────────────────────────────────────────────
publish-to-pypi:
name: Publish Python 🐍 distribution to PyPI
needs: [pre-check, build]
if: ${{ needs.pre-check.outputs.release_needed == 'true' }}
runs-on: ubuntu-latest
environment:
name: release
url: https://pypi.org/p/weco
permissions:
id-token: write
steps:
- name: Download the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
# ────────────────────────────────────────────────────────────────────
# 4) GitHub Release
# ────────────────────────────────────────────────────────────────────
github-release:
name: Create GitHub Release
needs: [pre-check, publish-to-pypi]
if: ${{ needs.pre-check.outputs.release_needed == 'true' }}
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Download the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Sign dists with Sigstore
uses: sigstore/gh-action-sigstore-python@v3.0.0
with:
inputs: |
./dist/*.tar.gz
./dist/*.whl
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
gh release create "v${{ needs.pre-check.outputs.version }}" \
--repo "${{ github.repository }}" \
--notes ""
- name: Upload artefacts to Release
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
gh release upload "v${{ needs.pre-check.outputs.version }}" dist/** \
--repo "${{ github.repository }}"