-
Notifications
You must be signed in to change notification settings - Fork 0
595 lines (536 loc) · 22 KB
/
Copy pathpython.yml
File metadata and controls
595 lines (536 loc) · 22 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
name: Python CI/CD
on:
push:
branches:
- main
paths:
- 'python/**'
- '.github/workflows/python.yml'
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'python/**'
- '.github/workflows/python.yml'
workflow_dispatch:
inputs:
bump_type:
description: 'Version bump type'
required: true
type: choice
options:
- patch
- minor
- major
description:
description: 'Release description (optional)'
required: false
type: string
# Least privilege by default (best practice #10 / template parity): individual
# release jobs raise this to `contents: write` where they need it.
permissions:
contents: read
jobs:
# REQUIRED CI CHECKS - All must pass before release
# These jobs ensure code quality and tests pass before any release
# === CHANGE DETECTION ===
# Template parity: the JavaScript and Rust pipelines gated their expensive
# jobs on what actually changed, while Python and C# ran everything for a
# documentation-only pull request. The workflow-level `paths:` filter only
# decides whether this workflow runs at all; this job decides which jobs
# inside it are worth running.
detect-changes:
timeout-minutes: 10
# Read-only check: a newer run supersedes this one.
concurrency:
group: check-${{ github.workflow }}-${{ github.ref }}-detect-changes
cancel-in-progress: true
name: Detect Changes
runs-on: ubuntu-latest
if: github.event_name != 'workflow_dispatch'
outputs:
py-changed: ${{ steps.changes.outputs.py-changed }}
toml-changed: ${{ steps.changes.outputs.toml-changed }}
docs-changed: ${{ steps.changes.outputs.docs-changed }}
workflow-changed: ${{ steps.changes.outputs.workflow-changed }}
any-code-changed: ${{ steps.changes.outputs.any-code-changed }}
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: '3.13'
- name: Detect changes
id: changes
working-directory: ./python
env:
GITHUB_EVENT_NAME: ${{ github.event_name }}
GITHUB_BASE_SHA: ${{ github.event.pull_request.base.sha }}
GITHUB_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: python scripts/detect_code_changes.py
# Linting and formatting
lint:
timeout-minutes: 15
# Read-only check: a newer run supersedes this one.
concurrency:
group: check-${{ github.workflow }}-${{ github.ref }}-lint
cancel-in-progress: true
name: Lint and Format Check
runs-on: ubuntu-latest
needs: [detect-changes]
# `!cancelled()` is required because detect-changes is skipped on
# workflow_dispatch; it still propagates a real workflow cancellation.
if: >-
${{
!cancelled() && (
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
needs.detect-changes.outputs.py-changed == 'true' ||
needs.detect-changes.outputs.toml-changed == 'true' ||
needs.detect-changes.outputs.docs-changed == 'true' ||
needs.detect-changes.outputs.workflow-changed == 'true'
)
}}
steps:
- uses: actions/checkout@v7
with:
# Full history: the merge simulation below needs the base branch.
fetch-depth: 0
# Best practice #7: the `refs/pull/N/merge` preview goes stale as soon as
# the base branch moves, so checks would validate code that is not what
# will actually land. Merge the latest base branch first.
- name: Simulate fresh merge with base branch (PR only)
if: github.event_name == 'pull_request'
env:
BASE_REF: ${{ github.base_ref }}
# Explicit shell: some of these jobs run on Windows runners, where
# the default shell is PowerShell.
shell: bash
run: bash scripts/simulate-fresh-merge.sh
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: '3.13'
- name: Install dependencies
working-directory: ./python
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
- name: Run Ruff linting
working-directory: ./python
run: ruff check src tests scripts
- name: Check Ruff formatting
working-directory: ./python
run: ruff format --check src tests scripts
- name: Run mypy
working-directory: ./python
run: mypy src
- name: Check file size limit
working-directory: ./python
run: python scripts/check_file_size.py
# Test matrix: Python on multiple OS.
# Until issue #41 this job ran on ubuntu-latest only, while the JavaScript,
# Rust and C# suites all ran on three operating systems; a Windows-only
# failure (line endings, path separators, locale-dependent encoding) would
# have reached PyPI unnoticed.
test:
timeout-minutes: 30
# Read-only check: a newer run supersedes this one.
concurrency:
group: check-${{ github.workflow }}-${{ github.ref }}-test-${{ matrix.os }}
cancel-in-progress: true
name: Test (Python 3.13 on ${{ matrix.os }})
runs-on: ${{ matrix.os }}
needs: [detect-changes]
# `!cancelled()` is required because detect-changes is skipped on
# workflow_dispatch; it still propagates a real workflow cancellation.
if: >-
${{
!cancelled() && (
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
needs.detect-changes.outputs.py-changed == 'true' ||
needs.detect-changes.outputs.toml-changed == 'true' ||
needs.detect-changes.outputs.docs-changed == 'true' ||
needs.detect-changes.outputs.workflow-changed == 'true'
)
}}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v7
with:
# Full history: the merge simulation below needs the base branch.
fetch-depth: 0
# Best practice #7: the `refs/pull/N/merge` preview goes stale as soon as
# the base branch moves, so checks would validate code that is not what
# will actually land. Merge the latest base branch first.
- name: Simulate fresh merge with base branch (PR only)
if: github.event_name == 'pull_request'
env:
BASE_REF: ${{ github.base_ref }}
# Explicit shell: some of these jobs run on Windows runners, where
# the default shell is PowerShell.
shell: bash
run: bash scripts/simulate-fresh-merge.sh
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: '3.13'
- name: Install dependencies
working-directory: ./python
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
- name: Run tests
working-directory: ./python
run: pytest tests/ -v --cov=src --cov-report=xml --cov-report=term
# One upload per commit: the matrix legs produce identical coverage for a
# pure-Python package, and uploading all three only adds Codecov noise.
- name: Upload coverage to Codecov
if: matrix.os == 'ubuntu-latest'
uses: codecov/codecov-action@v7
with:
files: ./python/coverage.xml
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}
# Build package - only runs if lint and test pass
build:
timeout-minutes: 20
# Read-only check: a newer run supersedes this one.
concurrency:
group: check-${{ github.workflow }}-${{ github.ref }}-build
cancel-in-progress: true
name: Build Package
runs-on: ubuntu-latest
needs: [lint, test]
# Skipped together with lint and test when nothing relevant changed.
if: >-
${{
!cancelled() &&
needs.lint.result == 'success' &&
needs.test.result == 'success'
}}
steps:
- uses: actions/checkout@v7
with:
# Full history: the merge simulation below needs the base branch.
fetch-depth: 0
# Best practice #7: the `refs/pull/N/merge` preview goes stale as soon as
# the base branch moves, so checks would validate code that is not what
# will actually land. Merge the latest base branch first.
- name: Simulate fresh merge with base branch (PR only)
if: github.event_name == 'pull_request'
env:
BASE_REF: ${{ github.base_ref }}
# Explicit shell: some of these jobs run on Windows runners, where
# the default shell is PowerShell.
shell: bash
run: bash scripts/simulate-fresh-merge.sh
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: '3.13'
- name: Install build dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Build package
working-directory: ./python
run: python -m build
- name: Check package
working-directory: ./python
run: twine check dist/*
- name: Upload artifacts
uses: actions/upload-artifact@v7
with:
name: dist
path: python/dist/
# Check for changelog fragments in PRs (similar to changesets check)
changelog:
timeout-minutes: 10
# Read-only check: a newer run supersedes this one.
concurrency:
group: check-${{ github.workflow }}-${{ github.ref }}-changelog
cancel-in-progress: true
name: Changelog Fragment Check
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
# Best practice #7: the `refs/pull/N/merge` preview goes stale as soon as
# the base branch moves, so checks would validate code that is not what
# will actually land. Merge the latest base branch first.
- name: Simulate fresh merge with base branch (PR only)
if: github.event_name == 'pull_request'
env:
BASE_REF: ${{ github.base_ref }}
# Explicit shell: some of these jobs run on Windows runners, where
# the default shell is PowerShell.
shell: bash
run: bash scripts/simulate-fresh-merge.sh
- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.13"
- name: Check for changelog fragments
working-directory: ./python
env:
GITHUB_BASE_REF: ${{ github.base_ref }}
# Passed through the environment: a branch name is attacker-controlled
# and must never be interpolated straight into a shell script.
HEAD_REF: ${{ github.head_ref }}
run: python scripts/validate_changeset.py
# RELEASE JOBS - Only run after all CI checks pass
# Automatic release on push to main (if version changed)
auto-release:
timeout-minutes: 45
# Writer: every job in the repository that pushes commits, tags or
# packages shares this group, so releases queue instead of racing, and a
# started release is never cancelled mid-publish.
concurrency:
group: main-writer-${{ github.repository }}
cancel-in-progress: false
queue: max
name: Auto Release
needs: [lint, test, build]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: '3.13'
- name: Setup Node.js
# The post-publish registry readback is a shared Node helper, so the
# release jobs need Node even though the package itself is Python.
uses: actions/setup-node@v7
with:
node-version: '22'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Check if version changed
id: version_check
working-directory: ./python
run: |
# Get current version from pyproject.toml
CURRENT_VERSION=$(grep -Po '(?<=^version = ")[^"]*' pyproject.toml)
PACKAGE_NAME=$(grep -Po '(?<=^name = ")[^"]*' pyproject.toml | head -1)
echo "current_version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
# Decide based on the registry, not on the local git tag. PyPI returns 200 for an
# existing version's JSON metadata and 404 otherwise. See
# docs/case-studies/issue-25/README.md for why the tag is not a reliable proxy.
STATUS=$(curl -sS -o /dev/null -w '%{http_code}' "https://pypi.org/pypi/${PACKAGE_NAME}/${CURRENT_VERSION}/json")
echo "PyPI HTTP status for ${PACKAGE_NAME}@${CURRENT_VERSION}: ${STATUS}"
if [ "$STATUS" = "200" ]; then
echo "Version ${PACKAGE_NAME}@${CURRENT_VERSION} already on PyPI, skipping publish"
echo "should_release=false" >> "$GITHUB_OUTPUT"
else
echo "Version ${PACKAGE_NAME}@${CURRENT_VERSION} is NOT on PyPI, will publish"
echo "should_release=true" >> "$GITHUB_OUTPUT"
fi
- name: Download artifacts
if: steps.version_check.outputs.should_release == 'true'
uses: actions/download-artifact@v8
with:
name: dist
path: python/dist/
- name: PyPI publish preflight
# Issue #41: the 0.2.0 release died inside the publish action with
# `invalid-publisher: valid token, but no corresponding publisher`,
# which names none of the values an operator has to enter on PyPI and
# does not say the missing piece lives outside this repository. This
# step prints the four claims and the registration steps up front.
# It reports without gating: a correctly registered *pending* publisher
# is indistinguishable from none at all until the first upload lands.
if: steps.version_check.outputs.should_release == 'true'
working-directory: ./python
env:
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
# Switch to '1' to dump the resolved OIDC claims; quiet by default.
PYPI_PREFLIGHT_VERBOSE: '0'
run: |
python scripts/pypi_publish_preflight.py \
--package "${{ steps.version_check.outputs.package_name }}"
- name: Publish to PyPI
id: pypi_publish
if: steps.version_check.outputs.should_release == 'true'
# Uses PyPI's OIDC Trusted Publisher flow (id-token: write below). If you see "Trusted
# Publisher … not configured" in the logs, configure it for this repo + workflow on PyPI.
# See docs/case-studies/issue-25/README.md for the broader context.
# A PYPI_API_TOKEN secret, when present, takes over so a first release
# can happen before trusted publishing is registered; without the
# secret the `password` input is empty and OIDC is used as before.
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: python/dist/
password: ${{ secrets.PYPI_API_TOKEN }}
verbose: true
skip-existing: true
- name: Explain a failed publish
# Turns the raw OIDC exchange error into the runbook. Without this the
# only guidance in the log is a list of claims and a 'not found'.
if: failure() && steps.pypi_publish.outcome == 'failure'
working-directory: ./python
env:
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
PYPI_PREFLIGHT_VERBOSE: '1'
run: |
python scripts/pypi_publish_preflight.py \
--package "${{ steps.version_check.outputs.package_name }}"
- name: Verify package on PyPI
# Issue #41: this readback allowed twenty-five seconds, the same class
# of too-short budget that turned a successful NuGet publish into a red
# run. It now uses the shared, per-registry-tuned readback.
if: steps.version_check.outputs.should_release == 'true' && steps.pypi_publish.outcome == 'success'
env:
REGISTRY_WAIT_VERBOSE: '0'
run: |
node scripts/wait-for-registry.mjs --registry pypi \
--name "${{ steps.version_check.outputs.package_name }}" \
--version "${{ steps.version_check.outputs.current_version }}"
- name: Create GitHub Release
if: steps.version_check.outputs.should_release == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
working-directory: ./python
run: |
python scripts/create_github_release.py \
--version "${{ steps.version_check.outputs.current_version }}" \
--repository "${{ github.repository }}" \
--tag-prefix "python_v" \
--language "Python"
# Manual release via workflow_dispatch - only after CI passes
manual-release:
timeout-minutes: 45
# Writer: every job in the repository that pushes commits, tags or
# packages shares this group, so releases queue instead of racing, and a
# started release is never cancelled mid-publish.
concurrency:
group: main-writer-${{ github.repository }}
cancel-in-progress: false
queue: max
name: Manual Release
needs: [lint, test, build]
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Python
uses: actions/setup-python@v7
with:
python-version: '3.13'
- name: Install dependencies
working-directory: ./python
run: |
python -m pip install --upgrade pip
pip install build twine "scriv[toml]"
- name: Setup Node.js
# The post-publish registry readback is a shared Node helper.
uses: actions/setup-node@v7
with:
node-version: '22'
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Collect changelog fragments
working-directory: ./python
run: |
# Check if there are any fragments to collect
FRAGMENTS=$(find changelog.d -name "*.md" ! -name "README.md" ! -name "*.j2" 2>/dev/null | wc -l)
if [ "$FRAGMENTS" -gt 0 ]; then
echo "Found $FRAGMENTS changelog fragment(s), collecting..."
scriv collect --version "${{ github.event.inputs.bump_type }}"
else
echo "No changelog fragments found, skipping collection"
fi
- name: Version and commit
id: version
working-directory: ./python
run: |
python scripts/version_and_commit.py \
--bump-type "${{ github.event.inputs.bump_type }}" \
--description "${{ github.event.inputs.description }}"
- name: Build package
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
working-directory: ./python
run: python -m build
- name: Check package
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
working-directory: ./python
run: twine check dist/*
- name: Read package name from pyproject.toml
id: pkg
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
working-directory: ./python
run: |
PACKAGE_NAME=$(grep -Po '(?<=^name = ")[^"]*' pyproject.toml | head -1)
echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
- name: PyPI publish preflight
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
working-directory: ./python
env:
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
PYPI_PREFLIGHT_VERBOSE: '0'
run: |
python scripts/pypi_publish_preflight.py \
--package "${{ steps.pkg.outputs.package_name }}"
- name: Publish to PyPI
id: pypi_publish
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
# See note above: relies on PyPI Trusted Publisher; see docs/case-studies/issue-25/README.md.
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: python/dist/
password: ${{ secrets.PYPI_API_TOKEN }}
verbose: true
skip-existing: true
- name: Explain a failed publish
if: failure() && steps.pypi_publish.outcome == 'failure'
working-directory: ./python
env:
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
PYPI_PREFLIGHT_VERBOSE: '1'
run: |
python scripts/pypi_publish_preflight.py \
--package "${{ steps.pkg.outputs.package_name }}"
- name: Verify package on PyPI
if: (steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true') && steps.pypi_publish.outcome == 'success'
env:
REGISTRY_WAIT_VERBOSE: '0'
run: |
node scripts/wait-for-registry.mjs --registry pypi \
--name "${{ steps.pkg.outputs.package_name }}" \
--version "${{ steps.version.outputs.new_version }}"
- name: Create GitHub Release
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
working-directory: ./python
run: |
python scripts/create_github_release.py \
--version "${{ steps.version.outputs.new_version }}" \
--repository "${{ github.repository }}" \
--tag-prefix "python_v" \
--language "Python"