-
-
Notifications
You must be signed in to change notification settings - Fork 541
418 lines (395 loc) · 12.5 KB
/
reusable-tox.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
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
---
name: >-
❌
[DO NOT CLICK]
Reusable Tox
on:
workflow_call:
inputs:
built-wheel-names:
description: >-
A glob for the built distributions in the artifact
to test (is installed into tox env if passed)
required: false
type: string
cache-key-files:
description: Dependency files cache
required: true
type: string
check-name:
description: A custom name for the Checks API-reported status
required: false
type: string
dists-artifact-name:
description: Workflow artifact name containing dists
required: true
type: string
environment-variables:
description: >-
A newline-delimited blob of text with environment variables
to be set using `${GITHUB_ENV}`
required: false
type: string
python-version:
description: Python version to provision in the VM
required: true
type: string
release-requested:
description: Flag whether this is CI run is a release request
default: 'false'
required: false
type: string
runner-vm-os:
description: VM OS to use
default: ubuntu
required: false
type: string
source-tarball-name:
description: Sdist filename wildcard
required: true
type: string
timeout-minutes:
description: Deadline for the job to complete
required: true
type: string
toxenv:
description: Name of the tox environment to use
required: true
type: string
tox-run-posargs:
description: Positional arguments to pass to the regular tox run
required: false
type: string
tox-rerun-posargs:
description: Positional arguments to pass to the re-attempted tox run
required: false
type: string
upstream-repository-id:
description: ID of the upstream GitHub Repository
required: true
type: string
xfail:
description: >-
Whether this job is expected to fail. Controls if the run outcomes
contribute to the failing CI status or not. The job status will be
treated as successful if this is set to `true`. Setting `false`
should be preferred typically.
required: true
type: string
secrets:
codecov-token:
description: Mandatory token for uploading to Codecov
required: true
env:
COLOR: >- # Supposedly, pytest or coveragepy use this
yes
FORCE_COLOR: 1 # Request colored output from CLI tools supporting it
MYPY_FORCE_COLOR: 1 # MyPy's color enforcement
PIP_DISABLE_PIP_VERSION_CHECK: 1
PIP_NO_PYTHON_VERSION_WARNING: 1
PIP_NO_WARN_SCRIPT_LOCATION: 1
PRE_COMMIT_COLOR: always
PY_COLORS: 1 # Recognized by the `py` package, dependency of `pytest`
PYTHONIOENCODING: utf-8
PYTHONUTF8: 1
TOX_PARALLEL_NO_SPINNER: 1
TOX_TESTENV_PASSENV: >- # Make tox-wrapped tools see color requests
COLOR
FORCE_COLOR
MYPY_FORCE_COLOR
NO_COLOR
PIP_DISABLE_PIP_VERSION_CHECK
PIP_NO_PYTHON_VERSION_WARNING
PIP_NO_WARN_SCRIPT_LOCATION
PRE_COMMIT_COLOR
PY_COLORS
PYTEST_THEME
PYTEST_THEME_MODE
PYTHONIOENCODING
PYTHONLEGACYWINDOWSSTDIO
PYTHONUTF8
jobs:
tox:
name: >-
${{
inputs.check-name
&& inputs.check-name
|| format(
'{0}@🐍{1}@{2}',
inputs.toxenv,
inputs.python-version,
inputs.runner-vm-os
)
}}
runs-on: ${{ inputs.runner-vm-os }}
timeout-minutes: ${{ fromJSON(inputs.timeout-minutes) }}
continue-on-error: >-
${{
(
fromJSON(inputs.xfail) ||
(
startsWith(inputs.python-version, '~')
) ||
contains(inputs.python-version, 'alpha')
) && true || false
}}
env:
TOXENV: ${{ inputs.toxenv }}
steps:
- name: Export requested job-global environment variables
if: inputs.environment-variables != ''
run: >-
echo '${{ inputs.environment-variables }}'
>> "${GITHUB_ENV}"
- name: >-
Switch to using Python v${{ inputs.python-version }}
by default
id: python-install
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
# NOTE: `pre-commit --show-diff-on-failure` and `sphinxcontrib-spellcheck`
# NOTE: with Git authors allowlist enabled both depend on the presence of a
# NOTE: Git repository.
- name: Grab the source from Git
if: >-
contains(fromJSON('["pre-commit", "spellcheck-docs"]'), inputs.toxenv)
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.release-committish }}
- name: Retrieve the project source from an sdist inside the GHA artifact
if: >-
!contains(fromJSON('["pre-commit", "spellcheck-docs"]'), inputs.toxenv)
uses: re-actors/checkout-python-sdist@release/v2
with:
source-tarball-name: ${{ inputs.source-tarball-name }}
workflow-artifact-name: ${{ inputs.dists-artifact-name }}
- name: Cache pre-commit.com virtualenvs
if: inputs.toxenv == 'pre-commit'
uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: >-
${{
runner.os
}}-pre-commit-${{
hashFiles('.pre-commit-config.yaml')
}}
- name: Figure out if the interpreter ABI is stable
id: py-abi
run: |
from os import environ
from pathlib import Path
from sys import version_info
FILE_APPEND_MODE = 'a'
is_stable_abi = version_info.releaselevel == 'final'
with Path(environ['GITHUB_OUTPUT']).open(
mode=FILE_APPEND_MODE,
) as outputs_file:
print(
'is-stable-abi={is_stable_abi}'.
format(is_stable_abi=str(is_stable_abi).lower()),
file=outputs_file,
)
shell: python
- name: >-
Calculate Python interpreter version hash value
for use in the cache key
if: fromJSON(steps.py-abi.outputs.is-stable-abi)
id: calc-cache-key-py
run: |
from hashlib import sha512
from os import environ
from pathlib import Path
from sys import version
FILE_APPEND_MODE = 'a'
hash = sha512(version.encode()).hexdigest()
with Path(environ['GITHUB_OUTPUT']).open(
mode=FILE_APPEND_MODE,
) as outputs_file:
print(f'py-hash-key={hash}', file=outputs_file)
shell: python
- name: Get pip cache dir
if: fromJSON(steps.py-abi.outputs.is-stable-abi)
id: pip-cache-dir
run: >-
echo "dir=$(python -Im pip cache dir)" >> "${GITHUB_OUTPUT}"
shell: bash
- name: Set up pip cache
if: fromJSON(steps.py-abi.outputs.is-stable-abi)
uses: actions/cache@v4
with:
path: ${{ steps.pip-cache-dir.outputs.dir }}
key: >-
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key }}-${{
inputs.cache-key-files }}
restore-keys: |
${{ runner.os }}-pip-${{
steps.calc-cache-key-py.outputs.py-hash-key
}}-
${{ runner.os }}-pip-
- name: Install tox
run: >-
python -Im pip install tox
shell: bash # windows compat
- name: Make the env clean of non-test files
if: inputs.toxenv == 'metadata-validation'
run: |
shopt -s extglob
rm -rf !tox.ini
shell: bash
- name: Download all the dists
if: >-
contains(fromJSON('["metadata-validation", "py"]'), inputs.toxenv)
uses: actions/download-artifact@v4
with:
name: ${{ inputs.dists-artifact-name }}
path: dist/
- name: >-
Pre-populate tox envs: `${{ env.TOXENV }}`
run: >-
python -Im
tox
--parallel auto
--parallel-live
--skip-missing-interpreters false
${{
inputs.built-wheel-names != ''
&& format('--installpkg dist/{0}', inputs.built-wheel-names)
|| ''
}}
--notest
- name: Initialize pre-commit envs if needed
if: inputs.toxenv == 'pre-commit'
run: >-
python -Im
tox
exec
--skip-pkg-install
--quiet
--
python -Im pre_commit install-hooks
- name: >-
Run tox envs: `${{ env.TOXENV }}`
id: tox-run
run: >-
python -Im
tox
--parallel auto
--parallel-live
--skip-missing-interpreters false
--skip-pkg-install
--quiet
${{
inputs.tox-run-posargs != ''
&& format('-- {0}', inputs.tox-run-posargs)
|| ''
}}
- name: Produce markdown test summary from JUnit
if: >-
!cancelled()
&& steps.tox-run.outputs.test-result-files != ''
uses: test-summary/[email protected]
with:
paths: >-
${{ steps.tox-run.outputs.test-result-files }}
- name: Produce markdown test summary from Cobertura XML
# NOTE: MyPy is temporarily excluded because it produces incomplete XML
# NOTE: files that `irongut/CodeCoverageSummary` can't stomach.
# Refs:
# * https://github.com/irongut/CodeCoverageSummary/issues/324
# * https://github.com/python/mypy/issues/17689
# FIXME: Revert the exclusion once upstream fixes the bug.
if: >-
!cancelled()
&& runner.os == 'Linux'
&& steps.tox-run.outputs.cov-report-files != ''
&& steps.tox-run.outputs.test-result-files == ''
&& steps.tox-run.outputs.codecov-flags != 'MyPy'
uses: irongut/[email protected]
with:
badge: true
filename: >-
${{ steps.tox-run.outputs.cov-report-files }}
format: markdown
output: both
# Ref: https://github.com/irongut/CodeCoverageSummary/issues/66
- name: Append coverage results to Job Summary
if: >-
!cancelled()
&& runner.os == 'Linux'
&& steps.tox-run.outputs.cov-report-files != ''
&& steps.tox-run.outputs.test-result-files == ''
&& steps.tox-run.outputs.codecov-flags != 'MyPy'
run: >-
cat code-coverage-results.md >> "${GITHUB_STEP_SUMMARY}"
- name: Re-run the failing tests with maximum verbosity
if: >-
!cancelled()
&& failure()
&& inputs.tox-rerun-posargs != ''
run: >- # `exit 1` makes sure that the job remains red with flaky runs
python -Im
tox
--parallel auto
--parallel-live
--skip-missing-interpreters false
-vvvvv
--skip-pkg-install
--
${{ inputs.tox-rerun-posargs }}
&& exit 1
shell: bash
- name: Send coverage data to Codecov
if: >-
!cancelled()
&& steps.tox-run.outputs.cov-report-files != ''
uses: codecov/codecov-action@v4
with:
disable_search: true
fail_ci_if_error: >-
${{ toJSON(inputs.upstream-repository-id == github.repository_id) }}
files: >-
${{ steps.tox-run.outputs.cov-report-files }}
flags: >-
CI-GHA,
${{ steps.tox-run.outputs.codecov-flags }},
OS-${{
runner.os
}},
VM-${{
inputs.runner-vm-os
}},
Py-${{
steps.python-install.outputs.python-version
}}
token: ${{ secrets.codecov-token }}
- name: Upload test results to Codecov
if: >-
!cancelled()
&& steps.tox-run.outputs.test-result-files != ''
# FIXME: revert to v1 once Codecov releases v1.0.2 of their action.
# Ref: https://github.com/codecov/test-results-action/issues/108.
# uses: codecov/test-results-action@v1
uses: codecov/test-results-action@4e79e65778be1cecd5df25e14af1eafb6df80ea9
with:
disable_search: true
fail_ci_if_error: >-
${{ toJSON(inputs.upstream-repository-id == github.repository_id) }}
files: >-
${{ steps.tox-run.outputs.test-result-files }}
flags: >-
CI-GHA,
${{ steps.tox-run.outputs.codecov-flags }},
OS-${{
runner.os
}},
VM-${{
inputs.runner-vm-os
}},
Py-${{
steps.python-install.outputs.python-version
}}
token: ${{ secrets.codecov-token }}
...