-
Notifications
You must be signed in to change notification settings - Fork 0
475 lines (421 loc) · 16.5 KB
/
Copy pathpython.yml
File metadata and controls
475 lines (421 loc) · 16.5 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
name: Python CI/CD Pipeline
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
defaults:
run:
working-directory: python
env:
GIT_CONFIG_COUNT: '1'
GIT_CONFIG_KEY_0: init.defaultBranch
GIT_CONFIG_VALUE_0: main
jobs:
# === DETECT CHANGES - determines which jobs should run ===
detect-changes:
name: Detect Changes
runs-on: ubuntu-latest
timeout-minutes: 5
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-detect-changes
cancel-in-progress: true
if: github.event_name != 'workflow_dispatch'
outputs:
py-changed: ${{ steps.changes.outputs.py-changed }}
tests-changed: ${{ steps.changes.outputs.tests-changed }}
package-changed: ${{ steps.changes.outputs.package-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@v6
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Detect changes
id: changes
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
# === CHANGELOG CHECK - only runs on PRs with code changes ===
# Docs-only PRs (./docs folder, markdown files) don't require changelog fragments
changelog:
name: Changelog Fragment Check
runs-on: ubuntu-latest
timeout-minutes: 10
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-changelog
cancel-in-progress: true
needs: [detect-changes]
if: github.event_name == 'pull_request' && needs.detect-changes.outputs.any-code-changed == 'true'
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: "3.13"
- name: Install scriv
run: pip install "scriv[toml]"
- name: Check for changelog fragments
# GITHUB_BASE_REF is passed as an environment variable rather than
# interpolated into the script body: a branch name is attacker-controlled
# data on pull_request events.
env:
GITHUB_BASE_REF: ${{ github.base_ref }}
run: |
set -euo pipefail
# Get list of fragment files (excluding README and template)
FRAGMENTS=$(find changelog.d -name "*.md" ! -name "README.md" ! -name "*.j2" 2>/dev/null | wc -l)
# Get changed files in PR
CHANGED_FILES=$(git diff --name-only "origin/${GITHUB_BASE_REF}...HEAD")
# Check if any source files changed (excluding docs and config)
SOURCE_CHANGED=$(printf '%s\n' "$CHANGED_FILES" | grep -cE "^python/(src/|tests/|scripts/)" || true)
if [ "$SOURCE_CHANGED" -gt 0 ] && [ "$FRAGMENTS" -eq 0 ]; then
echo "::error::No changelog fragment found. Please run 'scriv create' and document your changes."
echo ""
echo "To create a changelog fragment:"
echo " pip install 'scriv[toml]'"
echo " scriv create"
echo ""
echo "This is similar to adding a changeset in JavaScript projects."
echo "See changelog.d/README.md for more information."
exit 1
fi
echo "Changelog check passed"
# === LINT AND FORMAT CHECK ===
lint:
name: Lint and Format Check
runs-on: ubuntu-latest
timeout-minutes: 10
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-lint
cancel-in-progress: true
needs: [detect-changes]
# !cancelled() is required because detect-changes is skipped on
# workflow_dispatch. Without it, a skipped dependency skips this job too,
# which in turn skipped manual-release (it needs lint), making the manual
# release path unreachable.
# See: https://github.com/actions/runner/issues/491
if: |
!cancelled() && (
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
needs.detect-changes.outputs.py-changed == 'true' ||
needs.detect-changes.outputs.tests-changed == 'true' ||
needs.detect-changes.outputs.docs-changed == 'true' ||
needs.detect-changes.outputs.package-changed == 'true' ||
needs.detect-changes.outputs.workflow-changed == 'true'
)
steps:
- uses: actions/checkout@v6
with:
# Full history: simulate-fresh-merge.sh needs a merge base.
fetch-depth: 0
# Lint the state that will exist after the merge, not the stale merge
# preview GitHub built when the pull request was last synced.
- name: Simulate fresh merge with base branch (PR only)
if: github.event_name == 'pull_request'
env:
BASE_REF: ${{ github.base_ref }}
# The job default working-directory is the language subdirectory, and
# the shared script lives at the repository root. shell: bash is
# explicit because the Windows runners default to pwsh.
working-directory: .
shell: bash
run: bash scripts/simulate-fresh-merge.sh
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
- name: Run Ruff linting
run: ruff check .
- name: Check Ruff formatting
run: ruff format --check .
- name: Run mypy
run: mypy src
- name: Check file size limit
run: python scripts/check_file_size.py
# === TEST ===
test:
name: Test (Python ${{ matrix.python-version }} on ${{ matrix.os }})
runs-on: ${{ matrix.os }}
timeout-minutes: 20
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-test-${{ matrix.python-version }}-${{ matrix.os }}
cancel-in-progress: true
needs: [detect-changes, changelog]
# Run if: push event, OR changelog succeeded, OR changelog was skipped (docs-only PR)
if: >-
!cancelled() && (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || needs.changelog.result == 'success' || needs.changelog.result == 'skipped')
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ['3.13']
steps:
- uses: actions/checkout@v6
with:
# Full history: simulate-fresh-merge.sh needs a merge base.
fetch-depth: 0
# Test the state that will exist after the merge. This is the check that
# catches a semantic conflict: two pull requests that each pass alone but
# break the suite once both are on the base branch.
- name: Simulate fresh merge with base branch (PR only)
if: github.event_name == 'pull_request'
env:
BASE_REF: ${{ github.base_ref }}
# The job default working-directory is the language subdirectory, and
# the shared script lives at the repository root. shell: bash is
# explicit because the Windows runners default to pwsh.
working-directory: .
shell: bash
run: bash scripts/simulate-fresh-merge.sh
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
- name: Run tests
run: pytest tests/ -v --cov=src --cov-report=xml --cov-report=term
# Gate on the token rather than uploading unconditionally: without it the
# action fails on forks, which have no secrets. With the token present an
# upload error is a real failure, so fail_ci_if_error is true. It was
# false, which meant a genuinely failed upload still reported success --
# the coverage report silently stopped updating and nothing said so.
- name: Upload coverage to Codecov
if: >-
matrix.os == 'ubuntu-latest' && matrix.python-version == '3.13' &&
env.CODECOV_TOKEN != ''
uses: codecov/codecov-action@v7
with:
token: ${{ env.CODECOV_TOKEN }}
files: ./python/coverage.xml
disable_search: true
fail_ci_if_error: true
flags: python
- name: Report skipped Codecov upload
if: >-
matrix.os == 'ubuntu-latest' && matrix.python-version == '3.13' &&
env.CODECOV_TOKEN == ''
run: echo "::notice::Skipping Codecov upload because CODECOV_TOKEN is not configured"
# === BUILD PACKAGE ===
build:
name: Build Package
runs-on: ubuntu-latest
timeout-minutes: 10
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-build
cancel-in-progress: true
needs: [detect-changes, lint, test]
# Build only after the quality gates pass. 'skipped' is accepted because a
# docs-only pull request legitimately skips lint/test; 'failure' never is.
# The previous condition short-circuited on `github.event_name == 'push'`,
# so a failing lint on main still produced a build artifact.
if: |
!cancelled() &&
(needs.lint.result == 'success' || needs.lint.result == 'skipped') &&
(needs.test.result == 'success' || needs.test.result == 'skipped')
steps:
- uses: actions/checkout@v6
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install build dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Build package
run: python -m build
- name: Check package
run: twine check dist/*
- name: Upload artifacts
uses: actions/upload-artifact@v7
with:
name: python-dist
path: python/dist/
# === AUTO RELEASE - triggers on push to main if version changed ===
auto-release:
name: Auto Release
needs: [lint, test, build]
timeout-minutes: 30
concurrency:
group: main-writer-${{ github.repository }}-main
cancel-in-progress: false
# !cancelled() plus explicit success checks: a cancelled or failed gate must
# never publish, and a gate that was skipped for an unrelated reason must
# not be treated as a pass.
if: |
!cancelled() &&
github.event_name == 'push' &&
github.ref == 'refs/heads/main' &&
needs.lint.result == 'success' &&
needs.test.result == 'success' &&
needs.build.result == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Check if version changed
id: version_check
run: |
# Get current version from pyproject.toml
CURRENT_VERSION=$(grep -Po '(?<=^version = ")[^"]*' pyproject.toml)
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
# Check if tag exists (with python prefix to differentiate from JS/Rust)
if git rev-parse "python-v$CURRENT_VERSION" >/dev/null 2>&1; then
echo "Tag python-v$CURRENT_VERSION already exists, skipping release"
echo "should_release=false" >> $GITHUB_OUTPUT
else
echo "New version detected: $CURRENT_VERSION"
echo "should_release=true" >> $GITHUB_OUTPUT
fi
- name: Download artifacts
if: steps.version_check.outputs.should_release == 'true'
uses: actions/download-artifact@v7
with:
name: python-dist
path: python/dist/
- name: Publish to PyPI
if: steps.version_check.outputs.should_release == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: python/dist/
- name: Create GitHub Release
if: steps.version_check.outputs.should_release == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python scripts/create_github_release.py \
--version "${{ steps.version_check.outputs.current_version }}" \
--repository "${{ github.repository }}" \
--tag-prefix "python-v"
# === MANUAL RELEASE - triggered via workflow_dispatch ===
manual-release:
name: Manual Release
needs: [lint, test, build]
timeout-minutes: 30
concurrency:
group: main-writer-${{ github.repository }}-main
cancel-in-progress: false
# Previously this job could never run: detect-changes is skipped on
# workflow_dispatch, which skipped lint, which skipped this job because a
# skipped dependency skips dependents unless the condition survives it.
if: |
!cancelled() &&
github.event_name == 'workflow_dispatch' &&
needs.lint.result == 'success' &&
needs.test.result == 'success' &&
needs.build.result == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine "scriv[toml]"
- 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
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
# RELEASE_DESCRIPTION is a free-form workflow_dispatch input, so it is
# passed through the environment instead of being interpolated into the
# shell command.
env:
RELEASE_DESCRIPTION: ${{ github.event.inputs.description }}
run: |
python scripts/version_and_commit.py \
--bump-type "${{ github.event.inputs.bump_type }}" \
--description "$RELEASE_DESCRIPTION"
- name: Build package
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
run: python -m build
- name: Check package
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
run: twine check dist/*
- name: Publish to PyPI
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: python/dist/
- name: Create GitHub Release
if: steps.version.outputs.version_committed == 'true' || steps.version.outputs.already_released == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python scripts/create_github_release.py \
--version "${{ steps.version.outputs.new_version }}" \
--repository "${{ github.repository }}" \
--tag-prefix "python-v"