Repository navigation
285 lines (284 loc) · 13.2 KB
/
Copy pathcontinuous-integration.yml
File metadata and controls
285 lines (284 loc) · 13.2 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
# Run opinionated checks with project-owned configuration.
#
# ### Quick start
#
# ```yaml
# name: CI
# on: [push, pull_request]
# permissions: {}
# jobs:
# python:
# uses: hoverkraft-tech/ci-github-python/.github/workflows/continuous-integration.yml@main
# permissions:
# contents: read
# id-token: write
# with:
# working-directory: .
# python-versions: '["3.13", "3.15"]'
# ```
#
# An empty versions input uses project detection once. The last matrix entry is
# the primary version: lint, audit and package checks run there, and only
# that version publishes coverage. Tests run on every entry with fail-fast disabled.
# Duplicate, malformed and non-string versions are rejected. `runs-on` is a JSON
# array of Linux labels and applies to every job, including configuration.
#
# `checks: false` disables every check. Set `lint` or `test` to the string
# `"false"`, `"null"` or an empty string to disable that check. A command string or
# JSON object with `command` replaces its default. Commands are trusted Bash
# programs and run unchanged with failure propagation; do not populate them from
# untrusted pull request text. Unknown options fail before any project execution.
#
# Dependency review runs only on pull requests. Disable it with
# `dependency-review: false` when unavailable for the caller repository.
# `audit: false` disables vulnerability scanning. Audit results depend on the
# current vulnerability database and require network access.
#
# ### Python projects
#
# Use uv projects with committed `pyproject.toml`, `uv.lock` and preferably
# `.python-version`. Explicit `python-version` takes precedence; otherwise
# uv selects from `.python-version` and `requires-python`. Setup runs
# `uv sync --locked`: missing or stale locks fail instead of being rewritten.
# It installs uv's default dependency groups; optional extras require caller configuration.
# Configure `[tool.uv].default-groups` when additional groups are needed.
#
# Declare Ruff, Black, mypy, pytest and pytest-cov in those default groups.
# Ruff reports SARIF, Black checks formatting, and mypy uses the project's
# configuration. All three lint checks execute even after a failure.
# Configure coverage sources under `[tool.coverage.run]` to avoid measuring
# unrelated code. The [fixture](../../tests/python-project/pyproject.toml)
# shows a typed `src/` package and project-owned tool configuration.
#
# The audit action exports the locked production environment with hashes,
# then runs pip-audit. Development groups and the project itself are excluded.
# Registry dependencies with hashes are the supported audit input; direct
# VCS or editable dependencies may need a project-specific audit workflow.
#
# Distribution checks are opt-in:
#
# ```yaml
# with:
# package: '{"tests":"python -I -c \"import my_package\""}'
# ```
#
# The action builds with `uv build --no-sources`, installs the wheel and
# sdist independently into temporary venvs, checks dependencies, and runs
# the supplied commands outside the source checkout. `PACKAGE_NAME`,
# `PACKAGE_VERSION` and the consumer venv's `PATH` are available. Pin the
# build backend in `build-system.requires` for reproducible builds.
# Build backends execute code; Python has no general equivalent to npm's
# `ignore-scripts` switch. The action does not publish to PyPI.
#
# ### Reports and permissions
#
# Tests retain JUnit and supported coverage files for seven days, including after
# command failures. Lint retains parsed SARIF/Checkstyle reports. Only report files
# are uploaded, with unique names for parallel calls; a job producing no reports
# returns an empty `report-artifact-id`. Summaries and annotations work without PR
# write permissions. Individual PR comments are not created.
#
# Use the common
# [ci-reports workflow](https://github.com/hoverkraft-tech/ci-github-common/blob/main/.github/workflows/ci-reports.yml)
# for consolidated comments after standalone producer jobs; set `report-group` to
# the outer calling job ID when required. Python JUnit and Cobertura reports use the common
# parser.
#
# `test: '{"coverage":"codecov"}'` enables OIDC upload. The expected coverage file is
# `cobertura.xml`. A custom command must produce
# the applicable file. `coverage` controls publication, not collection, and an
# empty value still retains JUnit results. The reusable workflow declares
# `id-token: write` for Codecov and callers must grant it, including for default
# GitHub reports. Standalone GitHub reporting requires only read permissions.
# No repository-specific bot secrets are required by the public actions or workflow.
#
# See [setup-python](../../actions/setup-python/README.md), [lint](../../actions/lint/README.md),
# [test](../../actions/test/README.md) and [audit](../../actions/audit/README.md)
# for standalone action usage. Pin the workflow to a reviewed commit SHA in production.
---
name: Python Continuous Integration
"on":
workflow_call:
inputs:
runs-on:
description: JSON array of Linux runner labels for all jobs.
type: string
default: '["ubuntu-latest"]'
checks:
description: Enable all checks, including distribution checks.
type: boolean
default: true
working-directory:
description: One project directory inside the checkout. Use a caller matrix for multiple projects.
type: string
default: .
python-versions:
description: JSON array of Python versions. The last entry runs lint, audit and package checks and publishes coverage. Empty runs project detection once. An empty entry uses the project version.
type: string
default: ""
lint:
description: true for default checks, a trusted Bash command, or a JSON object with command. false, null or empty disables lint.
type: string
default: "true"
test:
description: true for default tests, a trusted Bash command, or a JSON object with command, coverage (`github`, `codecov` or empty). false, null or empty disables tests. Coverage publication runs only on the last version; the command controls collection.
type: string
default: "true"
audit:
description: Check production dependencies for known vulnerabilities.
type: boolean
default: true
dependency-review:
description: Review dependency changes on pull requests. Requires GitHub dependency review availability.
type: boolean
default: true
package:
description: false disables distribution tests. true builds and installs wheel and sdist. A JSON object with tests supplies consumer smoke commands. Does not publish packages.
type: string
default: "false"
uv-version:
description: Pinned uv release.
type: string
default: 0.13.9
permissions: {}
jobs:
configuration:
runs-on: ${{ fromJSON(inputs.runs-on) }}
permissions: {}
outputs:
versions: ${{ steps.config.outputs.versions }}
primary-version: ${{ steps.config.outputs.primary-version }}
lint: ${{ steps.config.outputs.lint }}
test: ${{ steps.config.outputs.test }}
package-enabled: ${{ steps.config.outputs.package-enabled }}
package-tests: ${{ steps.config.outputs.package-tests }}
steps:
- id: config
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
VERSIONS: ${{ inputs.python-versions }}
LINT: ${{ inputs.lint }}
TEST: ${{ inputs.test }}
PACKAGE: ${{ inputs.package }}
with:
script: |
const versions = process.env.VERSIONS.trim() ? JSON.parse(process.env.VERSIONS) : [''];
if (!Array.isArray(versions) || !versions.length || versions.length > 20 ||
versions.some(version => typeof version !== 'string' || (version && !/^\d+\.\d+(?:\.\d+)?$/.test(version))) ||
new Set(versions).size !== versions.length) {
throw new Error('versions must be a nonempty JSON array of unique numeric version strings.');
}
function options(name, input, defaults) {
const value = input.trim();
if (['', 'false', 'null'].includes(value)) return null;
let config;
if (value === 'true') config = {};
else if (value.startsWith('{') || value.startsWith('[')) config = JSON.parse(value);
else config = {command: value};
if (!config || typeof config !== 'object' || Array.isArray(config)) throw new Error(name + ' must be a command or JSON object.');
const unknown = Object.keys(config).filter(key => !Object.hasOwn(defaults, key));
if (unknown.length) throw new Error('Unknown ' + name + ' options: ' + unknown.join(', '));
config = {...defaults, ...config};
for (const [key, value] of Object.entries(config)) {
if (typeof value !== typeof defaults[key]) throw new Error(name + '.' + key + ' has the wrong type.');
}
return config;
}
const lint = options('lint', process.env.LINT, {command: ''});
const test = options('test', process.env.TEST, {"command": "uv run --locked pytest --junitxml=junit.xml --cov --cov-report=term-missing --cov-report=xml:cobertura.xml", "coverage": "github"});
if (test && !['', 'github', 'codecov'].includes(test.coverage)) throw new Error('test.coverage must be github, codecov or empty.');
let pkg;
try { pkg = JSON.parse(process.env.PACKAGE.trim() || 'false'); }
catch { throw new Error('package must be true, false or an object with tests.'); }
if (typeof pkg !== 'boolean' && (!pkg || typeof pkg !== 'object' || Array.isArray(pkg))) throw new Error('package must be true, false or an object.');
const enabled = pkg !== false;
if (typeof pkg === 'boolean') pkg = {};
if (Object.keys(pkg).some(key => key !== 'tests')) throw new Error('Unknown package option.');
if (typeof (pkg.tests ?? '') !== 'string' || pkg.tests === null) throw new Error('package.tests must be a string.');
core.setOutput('package-enabled', enabled);
core.setOutput('package-tests', pkg.tests || '');
core.setOutput('versions', versions);
core.setOutput('primary-version', versions.at(-1));
core.setOutput('lint', JSON.stringify(lint));
core.setOutput('test', JSON.stringify(test));
dependency-review:
if: inputs.checks && inputs.dependency-review && github.event_name == 'pull_request'
runs-on: ${{ fromJSON(inputs.runs-on) }}
permissions:
contents: read
steps:
- &checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/dependency-review-action@bbc44d14a204704ccc493733616cd1e93a0321ec # v5.0.1
lint:
if: inputs.checks && needs.configuration.outputs.lint != 'null'
needs: configuration
runs-on: ${{ fromJSON(inputs.runs-on) }}
timeout-minutes: 20
permissions:
contents: read
steps:
- *checkout
- &setup-primary
uses: $/actions/setup-python
with:
working-directory: ${{ inputs.working-directory }}
python-version: ${{ needs.configuration.outputs.primary-version }}
uv-version: ${{ inputs.uv-version }}
- uses: $/actions/lint
with:
working-directory: ${{ inputs.working-directory }}
command: ${{ fromJSON(needs.configuration.outputs.lint).command }}
test:
if: inputs.checks && needs.configuration.outputs.test != 'null'
needs: configuration
runs-on: ${{ fromJSON(inputs.runs-on) }}
timeout-minutes: 20
permissions:
contents: read
id-token: write
steps:
- *checkout
- uses: $/actions/setup-python
with:
working-directory: ${{ inputs.working-directory }}
python-version: ${{ matrix.version }}
uv-version: ${{ inputs.uv-version }}
- uses: $/actions/test
with:
working-directory: ${{ inputs.working-directory }}
command: ${{ fromJSON(needs.configuration.outputs.test).command }}
coverage: ${{ matrix.version == needs.configuration.outputs.primary-version && fromJSON(needs.configuration.outputs.test).coverage || '' }}
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.configuration.outputs.versions) }}
audit:
if: inputs.checks && inputs.audit
needs: configuration
runs-on: ${{ fromJSON(inputs.runs-on) }}
timeout-minutes: 20
permissions:
contents: read
steps:
- *checkout
- *setup-primary
- uses: $/actions/audit
with:
working-directory: ${{ inputs.working-directory }}
package:
if: inputs.checks && needs.configuration.outputs.package-enabled == 'true'
needs: configuration
runs-on: ${{ fromJSON(inputs.runs-on) }}
timeout-minutes: 20
permissions:
contents: read
steps:
- *checkout
- *setup-primary
- uses: $/actions/package/test
with:
working-directory: ${{ inputs.working-directory }}
commands: ${{ needs.configuration.outputs.package-tests }}