-
Notifications
You must be signed in to change notification settings - Fork 3
333 lines (269 loc) · 12.2 KB
/
publish.yml
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
name: Publish
on:
release:
types: [released]
workflow_dispatch:
inputs:
requested_release_tag:
description: "The tag to use for this developmental release (without `.dev` suffix) (e.g., `v2.0.2`)"
required: true
jobs:
# Responsible for validating inputs and generating release values for the rest of the workflow
# Takes in the tag from the GitHub release, or a manually provided one for developmental releases (i.e., tests of the CI pipeline)
pre_build_sanity_check:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v5
name: Install Python
with:
python-version: "3.12"
- run: |
pip install packaging
- name: Capture the ref
run: |
echo "event_ref=`echo '${{ github.event.release.tag_name }}${{ github.event.ref }}'`" >> $GITHUB_ENV
- name: Capture the release tag
run: |
echo "release_tag=`echo '${{ github.event.release.tag_name }}${{ github.event.inputs.requested_release_tag }}'`" >> $GITHUB_ENV
- name: Normalize the release tag into a version
run: |
echo "version_from_release_tag=`echo '${{ env.release_tag }}' | sed 's/^v//'`" >> $GITHUB_ENV
- name: Log all the things
run: |
echo 'Ref `${{ env.event_ref }}`'
echo 'release event's tag `${{ env.release_tag }}`'
echo 'release event's version `${{ env.version_from_release_tag }}`'
- name: Verify that the release's tag matches the format we expect ("v" + Python version number)
# https://peps.python.org/pep-0440/
run: |
echo "${{ env.release_tag }}" | sed '/^v\([1-9][0-9]*!\)\?\(0\|[1-9][0-9]*\)\(\.\(0\|[1-9][0-9]*\)\)*\(\(a\|b\|rc\)\(0\|[1-9][0-9]*\)\)\?\(\.post\(0\|[1-9][0-9]*\)\)\?\(\.dev\(0\|[1-9][0-9]*\)\)\?$/!{q1}'
- uses: actions/checkout@v4
with:
ref: ${{ env.event_ref }}
- name: Get the version from pyproject.toml
run: |
echo "backports_version=`grep -Po 'version = "\K[^"]*' pyproject.toml`" >> $GITHUB_ENV
- name: Generate the next developmental release version
# If there is a developmental release in Test PyPI for the requested version, increment the number. Else 1. Save in $GITHUB_ENV
run: |
curl https://test.pypi.org/pypi/backports-datetime-fromisoformat/json | python release/developmental_release.py ${{ env.version_from_release_tag }} | tail -n 1 | sed 's/^/developmental_release_version=/' >> $GITHUB_ENV
- name: Determine which version to use
run: echo "version_to_use=`if [ '${{ github.event_name }}' == 'workflow_dispatch' ]; then echo '${{ env.developmental_release_version }}'; else echo '${{ env.version_from_release_tag }}'; fi`" >> $GITHUB_ENV
- name: Log all the things
run: |
echo 'Ref `${{ env.event_ref }}`'
echo 'release event tag `${{ env.release_tag }}`'
echo 'release event version `${{ env.version_from_release_tag }}`'
echo 'Version in pyproject.toml `${{ env.backports_version }}`'
echo 'New developmental version `${{ env.developmental_release_version }}`'
echo 'Version to use `${{ env.version_to_use }}`'
- name: Verify that the version string we produced looks like a Python version string
# https://peps.python.org/pep-0440/
run: |
echo "${{ env.version_to_use }}" | sed '/^\([1-9][0-9]*!\)\?\(0\|[1-9][0-9]*\)\(\.\(0\|[1-9][0-9]*\)\)*\(\(a\|b\|rc\)\(0\|[1-9][0-9]*\)\)\?\(\.post\(0\|[1-9][0-9]*\)\)\?\(\.dev\(0\|[1-9][0-9]*\)\)\?$/!{q1}'
- name: Serialize normalized release values
run: |
echo -e "event_ref=${{ env.event_ref }}\nversion_from_release_tag=${{env.version_from_release_tag}}\nversion_to_use=${{ env.version_to_use }}" > release_values.txt
- name: Share normalized release values
uses: actions/upload-artifact@v4
with:
name: release_values
path: release_values.txt
build_wheels:
name: Build wheel on ${{ matrix.os }}
needs: [pre_build_sanity_check]
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
steps:
- uses: actions/download-artifact@v4
with:
name: release_values
- name: Load normalized release values
run: |
cat release_values.txt | xargs -I{} bash -c 'echo {} >> $GITHUB_ENV'
- uses: actions/checkout@v4
with:
ref: ${{ env.event_ref }}
- name: Replace version in pyproject.toml
# Required for developmental releases
run: |
awk '{sub(/^version = ".*?"$/,"version = \"${{ env.version_to_use }}\""); print}' pyproject.toml > temp && mv temp pyproject.toml
- name: Set up QEMU # Needed to build aarch64 wheels, https://cibuildwheel.pypa.io/en/stable/faq/#emulation
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v3
with:
platforms: all
- name: Build wheels
uses: pypa/[email protected]
- uses: actions/upload-artifact@v4
with:
name: dist_${{ matrix.os }}
path: |
./wheelhouse/*.whl
!./wheelhouse/UNKNOWN*.whl
build_wheels_windows:
name: Build wheel on windows-latest
needs: [pre_build_sanity_check]
runs-on: windows-latest
steps:
- uses: actions/download-artifact@v4
with:
name: release_values
- name: Load normalized release values
run: foreach($line in [System.IO.File]::ReadLines("release_values.txt")){ echo $line >> $env:GITHUB_ENV }
- uses: actions/checkout@v4
with:
ref: ${{ env.event_ref }}
- name: Replace version in pyproject.toml
# Required for developmental releases
run: (Get-Content -path "pyproject.toml") | % { $_ -Replace '^version = ".*?"$', "version = `"$env:version_to_use`"" } | Out-File "pyproject.toml"
- name: Build wheels
uses: pypa/[email protected]
- uses: actions/upload-artifact@v4
with:
name: dist_windows
path: |
./wheelhouse/*.whl
!./wheelhouse/UNKNOWN*.whl
build_sdist:
name: Build source distribution
needs: [pre_build_sanity_check]
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v5
name: Install Python
with:
python-version: "3.12"
- uses: actions/download-artifact@v4
with:
name: release_values
- name: Load normalized release values
run: |
cat release_values.txt | xargs -l -I{} bash -c 'echo {} >> $GITHUB_ENV'
- uses: actions/checkout@v4
with:
ref: ${{ env.event_ref }}
- name: Replace version in pyproject.toml
# Required for developmental releases
run: sed -i -e 's/^version = ".*\?"$/version = "${{ env.version_to_use }}"/g' pyproject.toml
- name: Get build tool
run: pip install --upgrade build
- name: Build sdist
run: python -m build
- uses: actions/upload-artifact@v4
with:
name: dist_source
path: dist/*.tar.gz
publish_to_test_pypi:
needs:
[pre_build_sanity_check, build_wheels, build_wheels_windows, build_sdist]
runs-on: ubuntu-latest
environment: test_pypi
permissions:
id-token: write # Required for "trusted publishing"
steps:
- uses: actions/download-artifact@v4
with:
name: release_values
- name: Load normalized release values
run: |
cat release_values.txt | xargs -I{} bash -c 'echo {} >> $GITHUB_ENV'
- uses: actions/checkout@v4
with:
ref: ${{ env.event_ref }}
- uses: actions/download-artifact@v4
id: download
with:
pattern: dist_*
merge-multiple: true
path: dist/
- name: Publish package to TestPyPI
uses: pypa/[email protected]
with:
repository-url: https://test.pypi.org/legacy/
attestations: true
pre_publish_sanity_check:
# needs: [publish_to_test_pypi]
needs:
[pre_build_sanity_check, build_wheels, build_wheels_windows, build_sdist]
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: release_values
- name: Load normalized release values
run: |
cat release_values.txt | xargs -l -I{} bash -c 'echo {} >> $GITHUB_ENV'
- name: Verify that the `version_from_release_tag` is not a "developmental release"
run: |
python -c 'import sys; from packaging import version; code = 1 if version.parse("${{ env.version_from_release_tag }}").is_devrelease else 0; sys.exit(code)'
- uses: actions/checkout@v4
with:
ref: ${{ env.event_ref }}
- name: Get the latest developmental release for this version from Test PyPI
run: |
curl https://test.pypi.org/pypi/backports-datetime-fromisoformat/json | python release/developmental_release.py ${{ env.version_from_release_tag }} | head -n 1 | sed 's/^/test_pypi_developmental_release_version=/' >> $GITHUB_ENV
- name: Get the latest version from PyPI
run: |
curl https://pypi.org/pypi/backports-datetime-fromisoformat/json | python -c 'import json, sys; contents=sys.stdin.read(); parsed = json.loads(contents); print("pypi_version=" + parsed["info"]["version"])' >> $GITHUB_ENV
- name: Log all the things
run: |
echo 'Ref: `${{ env.event_ref }}`'
echo 'Version to use: `${{ env.version_to_use }}`'
echo 'Developmental release version in Test PyPI `${{ env.test_pypi_developmental_release_version }}`'
echo 'Version in PyPI `${{ env.pypi_version }}`'
- name: Verify that there exists a developmental release for this version in Test PyPI
# https://peps.python.org/pep-0440
# Meant to make sure that we aren't somehow skipping the "developmental release" phase of the release
# (e.g., Publishing the GitHub release without first saving the draft)
run: |
[[ ${{ env.test_pypi_developmental_release_version }} != 0.0.0 ]]
- name: Get the version from pyproject.toml
run: |
echo "backports_version=`grep -Po 'version = "\K[^"]*' pyproject.toml`" >> $GITHUB_ENV
- name: Verify that the release version matches the version in pyproject.toml
run: |
[[ ${{ env.version_from_release_tag }} == ${{ env.backports_version }} ]]
- name: Verify that the `version_from_release_tag` is present in the CHANGELOG
# TODO: Use something like `changelog-cli` to extract the correct version number
run: |
grep ${{ env.version_from_release_tag }} CHANGELOG.md
- name: Verify that the `version_from_release_tag` is larger/newer than the existing release in PyPI
# Note: This precludes making releases that are for old versions
run: |
python -c 'import sys; from packaging import version; code = 0 if version.parse("${{ env.pypi_version }}") < version.parse("${{ env.version_from_release_tag }}") else 1; sys.exit(code)'
publish:
if: github.event_name == 'release'
needs: [
pre_build_sanity_check,
build_wheels,
build_wheels_windows,
build_sdist,
# publish_to_test_pypi,
pre_publish_sanity_check,
]
runs-on: ubuntu-latest
environment: production_pypi
permissions:
id-token: write # Required for "trusted publishing"
steps:
- uses: actions/download-artifact@v4
with:
name: release_values
- name: Load normalized release values
run: |
cat release_values.txt | xargs -l -I{} bash -c 'echo {} >> $GITHUB_ENV'
- uses: actions/checkout@v4
with:
ref: ${{ env.event_ref }}
- uses: actions/download-artifact@v4
id: download
with:
name: dist
path: dist/
- name: Publish package to PyPI
uses: pypa/[email protected]
with:
attestations: true