Repository navigation
449 lines (441 loc) · 18.9 KB
/
Copy pathpython-ci.yml
File metadata and controls
449 lines (441 loc) · 18.9 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
# Reusable: CI for a Python package linted and formatted with ruff and tested
# with pytest.
#
# What pytest measures under coverage lives in pyproject.toml
# ([tool.coverage.run]), not on the command line here, so a developer running
# `pytest --cov` locally measures what CI measures.
#
# A repo with a uv.lock is installed from it (`uv sync --locked`), so CI tests
# exactly the versions the lock pins and fails if the lock is stale. Its tools
# then run through `uv run --no-sync`, which uses that environment as synced
# rather than syncing again with different options. Without a lock, `install`
# runs with pip.
name: python-ci
on:
workflow_call:
inputs:
python-versions:
description: JSON array of Python versions to test
type: string
default: '["3.11", "3.12", "3.13", "3.14"]'
python-version:
description: The single version lint and coverage run on
type: string
default: "3.13"
install:
description: >-
Command that installs the package and its dev tools with pip. Ignored
when the repo has a uv.lock, which is installed with uv instead.
type: string
default: pip install -e ".[dev]"
uv-sync-args:
description: >-
Arguments added to `uv sync --locked` in a repo with a uv.lock. The
default takes every extra, so dev tools kept in an extra (like the
`.[dev]` pip installs) are there too; dependency groups such as `dev`
are installed by uv's default either way.
type: string
default: --all-extras
coverage:
description: Run pytest under coverage and upload coverage.xml to Codecov
type: boolean
default: true
audit:
description: Run pip-audit (runtime blocking, development advisory)
type: boolean
default: true
secrets:
CODECOV_TOKEN:
required: false
permissions:
contents: read
jobs:
lint:
name: ruff
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-python@v7
with:
python-version: ${{ inputs.python-version }}
- name: Use uv if the repo has a lockfile
id: tool
run: |
if [ -f uv.lock ]; then
echo "uv=true" >> "$GITHUB_OUTPUT"
echo "RUN=uv run --no-sync" >> "$GITHUB_ENV"
fi
- if: steps.tool.outputs.uv == 'true'
uses: astral-sh/[email protected]
with:
python-version: ${{ inputs.python-version }}
- name: Install
env:
INSTALL: ${{ inputs.install }}
UV: ${{ steps.tool.outputs.uv }}
UV_SYNC_ARGS: ${{ inputs.uv-sync-args }}
run: |
if [ "$UV" = true ]; then
# Unquoted on purpose: it is a list of arguments.
# shellcheck disable=SC2086
uv sync --locked $UV_SYNC_ARGS
else
eval "$INSTALL"
fi
- run: $RUN ruff check .
- run: $RUN ruff format --check .
test:
# Interpolated, not a fixed string: a matrix job with a static name produces
# several checks called the same thing.
name: pytest ${{ matrix.python-version }}
timeout-minutes: 10
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ${{ fromJSON(inputs.python-versions) }}
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python-version }}
- name: Use uv if the repo has a lockfile
id: tool
run: |
if [ -f uv.lock ]; then
echo "uv=true" >> "$GITHUB_OUTPUT"
echo "RUN=uv run --no-sync" >> "$GITHUB_ENV"
fi
- if: steps.tool.outputs.uv == 'true'
uses: astral-sh/[email protected]
with:
python-version: ${{ matrix.python-version }}
- name: Install
env:
INSTALL: ${{ inputs.install }}
UV: ${{ steps.tool.outputs.uv }}
UV_SYNC_ARGS: ${{ inputs.uv-sync-args }}
run: |
if [ "$UV" = true ]; then
# Unquoted on purpose: it is a list of arguments.
# shellcheck disable=SC2086
uv sync --locked $UV_SYNC_ARGS
else
eval "$INSTALL"
fi
- run: $RUN pytest -q
# Reporting, not a gate: the matrix above is what proves the suite passes.
# One interpreter, because coverage of the same code on several says the same
# thing several times.
coverage:
name: coverage
if: inputs.coverage
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-python@v7
with:
python-version: ${{ inputs.python-version }}
- name: Use uv if the repo has a lockfile
id: tool
run: |
if [ -f uv.lock ]; then
echo "uv=true" >> "$GITHUB_OUTPUT"
echo "RUN=uv run --no-sync" >> "$GITHUB_ENV"
fi
- if: steps.tool.outputs.uv == 'true'
uses: astral-sh/[email protected]
with:
python-version: ${{ inputs.python-version }}
- name: Install
env:
INSTALL: ${{ inputs.install }}
UV: ${{ steps.tool.outputs.uv }}
UV_SYNC_ARGS: ${{ inputs.uv-sync-args }}
run: |
if [ "$UV" = true ]; then
# Unquoted on purpose: it is a list of arguments.
# shellcheck disable=SC2086
uv sync --locked $UV_SYNC_ARGS
else
eval "$INSTALL"
fi
- run: $RUN pytest -q --cov --cov-report=xml
# Advisory only: a Codecov outage or a token missing from a fork says
# nothing about the change under review. The coverage run above stays
# blocking.
- uses: codecov/codecov-action@v7
continue-on-error: true
with:
files: ./coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
# Separate from the test matrix: an audit result reflects the advisory
# database at a point in time, not the diff under review, and advisories
# belong to a package version, not to the Python running the audit. One
# interpreter, the one lint runs on, except for the runtime tree of a repo
# without a uv.lock (runtime-audit, below).
#
# Runtime means [project].dependencies, without extras or dependency groups:
# what `pip install <package>` gives a user. The development tree is
# everything the repo can install. With a uv.lock both come from the lock
# (`uv export`), hashed, and scripts/audit-requirements.py turns each export
# into pip-audit's input:
# - Hashes are kept, so pip-audit checks the pinned versions as they are
# without resolving anything.
# - Direct URL requirements (Git dependencies, say) are dropped with a
# notice: no version to look up, and no hashes.
# - Environment markers are stripped. pip-audit would evaluate them against
# this one interpreter and skip a pin that only another tested Python
# installs.
# - A package locked at several versions for different markers is split
# across several files, audited one at a time, since one requirements file
# can list it once.
# Without a lock, the development tree is installed here with the `install`
# input into a venv of its own, and pip-audit reads what landed there. pip is
# uninstalled from it first, being the installer rather than a dependency.
# Each line of $RUNNER_TEMP/audit/<tree>.args holds the arguments of one
# pip-audit run.
# Whether the repo has a uv.lock, which decides whether runtime-audit runs.
audit-plan:
name: audit plan
if: inputs.audit
timeout-minutes: 5
runs-on: ubuntu-latest
outputs:
lock: ${{ steps.tool.outputs.uv }}
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Use uv if the repo has a lockfile
id: tool
run: |
if [ -f uv.lock ]; then
echo "uv=true" >> "$GITHUB_OUTPUT"
fi
audit:
name: dependency audit
if: inputs.audit
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Use uv if the repo has a lockfile
id: tool
run: |
if [ -f uv.lock ]; then
echo "uv=true" >> "$GITHUB_OUTPUT"
fi
- uses: actions/setup-python@v7
with:
python-version: ${{ inputs.python-version }}
- if: steps.tool.outputs.uv == 'true'
uses: astral-sh/[email protected]
with:
python-version: ${{ inputs.python-version }}
# The pip-audit pin lives in this repository, where Dependabot bumps it,
# so it is fetched at the commit the caller pinned, and so is the script
# that prepares a uv export for it.
- name: Fetch the pinned pip-audit requirements and script
env:
# job.workflow_sha is this workflow's own commit (the one the caller's
# @ref resolved to), not the caller's.
REF: ${{ job.workflow_sha }}
run: |
for file in requirements-audit.txt audit-requirements.py; do
curl --fail --silent --show-error --retry 3 -o "$RUNNER_TEMP/$file" \
"https://raw.githubusercontent.com/atdr/github-boilerplate/$REF/scripts/$file"
done
# Into the runner's interpreter, never into what is audited below.
- run: pip install -r "$RUNNER_TEMP/requirements-audit.txt"
- name: Resolve runtime and development dependencies
env:
INSTALL: ${{ inputs.install }}
UV: ${{ steps.tool.outputs.uv }}
run: |
out="$RUNNER_TEMP/audit"
mkdir -p "$out"
if [ "$UV" = true ]; then
# Checked once against the audit's Python, then exported --frozen:
# with --locked, every export looks for an interpreter meeting its
# selection's requires-python, and setup-uv's UV_PYTHON pins this
# one, so a group declaring a newer requires-python would fail.
uv lock --check --quiet
# --no-emit-local leaves out the project and any path dependency,
# which are not on PyPI to audit.
export_args="--frozen --quiet --format requirements-txt --no-emit-local --no-default-groups"
# Unquoted on purpose: it is a list of arguments.
# shellcheck disable=SC2086
uv export $export_args -o "$out/export-runtime.txt"
# The development tree is the runtime export plus one export per
# extra and per dependency group, and in a workspace per member and
# each of its extras and groups: uv refuses a single selection of
# them all when the project declares any of them conflicting. Each
# line the script prints is one selection's arguments.
python -I "$RUNNER_TEMP/audit-requirements.py" --selections pyproject.toml \
> "$out/selections"
n=0
while read -r -a selection <&3; do
n=$((n + 1))
# shellcheck disable=SC2086
uv export $export_args "${selection[@]}" -o "$out/export-development-$n.txt"
done 3< "$out/selections"
python -I "$RUNNER_TEMP/audit-requirements.py" "$out/runtime" "$out/export-runtime.txt"
python -I "$RUNNER_TEMP/audit-requirements.py" "$out/development" "$out"/export-*.txt
for tree in runtime development; do
# One pip-audit run per file it wrote, none if it wrote none:
# pip-audit refuses a requirements file with nothing in it.
for file in "$out/$tree"-*.txt; do
if [ -f "$file" ]; then
echo "--disable-pip -r $file"
fi
done > "$out/$tree.args"
done
elif [ -f pyproject.toml ] || [ -f setup.py ]; then
# The runtime tree is runtime-audit's, one job per Python.
# The development tree on the audit's own Python only: it is
# advisory, and never reaches a user.
python -m venv "$out/development"
(
export VIRTUAL_ENV="$out/development" PATH="$out/development/bin:$PATH"
eval "$INSTALL"
)
"$out/development/bin/python" -m pip uninstall --quiet --yes pip
site=$("$out/development/bin/python" -c 'import sysconfig; print(sysconfig.get_path("purelib"))')
echo "--skip-editable --path $site" > "$out/development.args"
else
echo "::error title=Nothing to audit::No uv.lock, pyproject.toml or setup.py, so there are no dependencies to audit. Set \`audit: false\` if this repo has none."
exit 1
fi
# Blocking: runtime dependencies are the ones that reach a user. pip-audit
# has no severity threshold, so any known advisory fails. Only with a
# lock: without one, runtime-audit covers the runtime tree.
- name: audit runtime dependencies
if: steps.tool.outputs.uv == 'true'
run: |
# An export with nothing left in it, which pip-audit would refuse.
if [ ! -s "$RUNNER_TEMP/audit/runtime.args" ]; then
echo "No runtime dependencies to audit."
exit 0
fi
status=0
set -f
while read -r args <&3; do
# Unquoted on purpose: it is a list of arguments.
# shellcheck disable=SC2086
pip-audit --progress-spinner off $args || status=1
done 3< "$RUNNER_TEMP/audit/runtime.args"
exit "$status"
# Advisory: development tooling never ships. The step swallows the exit
# code itself rather than using continue-on-error, which would leave the
# runner's bare "Process completed with exit code 1" as the only
# annotation. ::error rather than ::warning, so it is not scrolled past
# with the deprecation notices; a Dependabot bump clears it.
- name: audit development tree (advisory)
run: |
if [ ! -s "$RUNNER_TEMP/audit/development.args" ]; then
echo "No development dependencies to audit."
exit 0
fi
status=0
set -f
while read -r args <&3; do
# Unquoted on purpose: it is a list of arguments.
# shellcheck disable=SC2086
pip-audit --progress-spinner off $args || status=1
done 3< "$RUNNER_TEMP/audit/development.args"
if [ "$status" != 0 ]; then
echo "::error title=Development dependency advisory::pip-audit found an advisory in the development tree. This does not block: development dependencies never reach a user, and the job is green on purpose. Dependabot alerts are the signal of record; the report above says which package."
fi
# Without a uv.lock, the runtime tree is whatever pip installs, and an
# environment marker can make that differ by Python, so it is installed and
# audited once per version the test matrix runs. A job per version, so
# setup-python resolves each entry itself (3.x, a range, pypy3.10, 3.13t)
# and python-path names the interpreter it chose. A uv export already covers
# every marker, so with a lock this job is skipped. Blocking, like the
# runtime audit above: an entry setup-python cannot install, or a tree that
# cannot be installed or read, fails its job rather than passing unaudited.
runtime-audit:
# Interpolated, not a fixed string: a matrix job with a static name produces
# several checks called the same thing.
name: runtime audit ${{ matrix.python-version }}
needs: audit-plan
if: inputs.audit && needs.audit-plan.outputs.lock != 'true'
timeout-minutes: 10
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ${{ fromJSON(inputs.python-versions) }}
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-python@v7
id: python
with:
python-version: ${{ matrix.python-version }}
# pip-audit and the script run on the audit job's Python, set up second
# so it is the default `python`: the matrix may test a Python older than
# pip-audit supports. The matrix interpreter only installs the tree, and
# pip-audit reads that tree's metadata from outside it.
- uses: actions/setup-python@v7
with:
python-version: ${{ inputs.python-version }}
# As in the audit job: pinned in this repository, fetched at the commit
# the caller pinned.
- name: Fetch the pinned pip-audit requirements and script
env:
REF: ${{ job.workflow_sha }}
run: |
for file in requirements-audit.txt audit-requirements.py; do
curl --fail --silent --show-error --retry 3 -o "$RUNNER_TEMP/$file" \
"https://raw.githubusercontent.com/atdr/github-boilerplate/$REF/scripts/$file"
done
# Into the audit job's Python, never into the venv audited below.
- run: python -m pip install -r "$RUNNER_TEMP/requirements-audit.txt"
- name: Resolve runtime dependencies
env:
INSTALL: ${{ inputs.install }}
PYTHON: ${{ steps.python.outputs.python-path }}
run: |
if [ ! -f pyproject.toml ] && [ ! -f setup.py ]; then
echo "::error title=Nothing to audit::No uv.lock, pyproject.toml or setup.py, so there are no dependencies to audit. Set \`audit: false\` if this repo has none."
exit 1
fi
out="$RUNNER_TEMP/audit"
mkdir -p "$out"
# The runtime tree resolves from the same indexes as `install`, so
# the script picks its index options out of it, and nothing else:
# no extras, and no other part of it runs.
python -I "$RUNNER_TEMP/audit-requirements.py" --index-options "$INSTALL" \
> "$out/index-options"
index=()
while IFS= read -r -d '' option; do
index+=("$option")
done < "$out/index-options"
# Without pip, and installed into by the interpreter's own pip: a
# venv's pip, and the setuptools Python 3.11 seeds beside it, are
# not dependencies.
"$PYTHON" -m venv --without-pip "$out/runtime"
"$PYTHON" -m pip --python "$out/runtime/bin/python" install --quiet "${index[@]}" -e .
site=$("$out/runtime/bin/python" -c 'import sysconfig; print(sysconfig.get_path("purelib"))')
echo "SITE=$site" >> "$GITHUB_ENV"
- name: audit runtime dependencies
run: |
if [ -z "$SITE" ] || [ ! -d "$SITE" ]; then
echo "::error title=Runtime tree not audited::The runtime venv's site-packages is missing, so nothing was audited."
exit 1
fi
pip-audit --progress-spinner off --skip-editable --path "$SITE"