Skip to content

Commit 04df94c

Browse files
authored
ci: 🎨🧪 Modularize GHA workflow through reuse (#618)
This change slightly refactors parts of the GitHub Actions CI/CD workflow definitions to make it possible to rely on the main check gate in branch protection and declutter the generic workflow. It reproduces the original efforts of skipping the unnecessary checks depending on the files changed via dynamically determined variables.
1 parent f5d5f15 commit 04df94c

File tree

6 files changed

+216
-130
lines changed

6 files changed

+216
-130
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: change detection
2+
on:
3+
workflow_call:
4+
outputs:
5+
run-docs:
6+
description: Whether or not build the docs
7+
value: ${{ jobs.change-detection.outputs.run-docs || false }}
8+
run-tests:
9+
description: Whether or not run the tests
10+
value: ${{ jobs.change-detection.outputs.run-tests || false }}
11+
12+
jobs:
13+
change-detection:
14+
name: Identify source changes
15+
runs-on: ubuntu-latest
16+
timeout-minutes: 1
17+
outputs:
18+
run-docs: ${{ steps.docs-changes.outputs.run-docs || false }}
19+
run-tests: ${{ steps.tests-changes.outputs.run-tests || false }}
20+
steps:
21+
- uses: actions/checkout@v3
22+
- name: Get a list of the changed runtime-related files
23+
if: github.event_name == 'pull_request'
24+
id: changed-testable-files
25+
uses: Ana06/[email protected]
26+
with:
27+
filter: |
28+
src/**
29+
tests/**
30+
tox.ini
31+
.github/workflows/test.yml
32+
.github/workflows/reusable-type.yml
33+
.github/workflows/reusable-pytest.yml
34+
- name: Set a flag for running the tests
35+
if: >-
36+
github.event_name != 'pull_request'
37+
|| steps.changed-testable-files.outputs.added_modified_renamed != ''
38+
id: tests-changes
39+
run: >-
40+
echo "run-tests=true" >> "${GITHUB_OUTPUT}"
41+
- name: Get a list of the changed documentation-related files
42+
if: github.event_name == 'pull_request'
43+
id: changed-docs-files
44+
uses: Ana06/[email protected]
45+
with:
46+
filter: |
47+
docs/**
48+
CHANGELOG.rst
49+
README.md
50+
.github/workflows/test.yml
51+
.github/workflows/reusable-check.yml
52+
- name: Set a flag for building the docs
53+
if: >-
54+
github.event_name != 'pull_request'
55+
|| steps.changed-docs-files.outputs.added_modified_renamed != ''
56+
id: docs-changes
57+
run: >-
58+
echo "run-docs=true" >> "${GITHUB_OUTPUT}"

‎.github/workflows/check.yml renamed to ‎.github/workflows/reusable-docs.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
name: check
22
on:
3-
push:
4-
branches:
5-
- main
6-
pull_request:
7-
branches:
8-
- main
9-
schedule:
10-
- cron: "0 8 * * *"
3+
workflow_call:
114

125
jobs:
136
docs:
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: pytest
2+
on:
3+
workflow_call:
4+
5+
jobs:
6+
pytest:
7+
runs-on: ${{ matrix.os }}-latest
8+
env:
9+
PYTEST_ADDOPTS: "--run-integration --showlocals -vv --durations=10 --reruns 5 --only-rerun subprocess.CalledProcessError"
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
os:
14+
- ubuntu
15+
- macos
16+
- windows
17+
py:
18+
- "pypy-3.7"
19+
- "pypy-3.8"
20+
- "pypy-3.9"
21+
- "3.11"
22+
- "3.10"
23+
- "3.9"
24+
- "3.8"
25+
- "3.7"
26+
tox-target:
27+
- "tox"
28+
- "min"
29+
30+
continue-on-error: >- # jobs not required in branch protection
31+
${{
32+
(
33+
startsWith(matrix.py, 'pypy-')
34+
&& (!endsWith(matrix.py, '-3.7') || matrix.os == 'windows')
35+
)
36+
&& true
37+
|| false
38+
}}
39+
40+
steps:
41+
- uses: actions/checkout@v3
42+
with:
43+
fetch-depth: 0
44+
45+
- name: Setup python for test ${{ matrix.py }}
46+
uses: actions/setup-python@v4
47+
with:
48+
python-version: ${{ matrix.py }}
49+
50+
- name: Pick environment to run
51+
run: |
52+
import platform
53+
import os
54+
import sys
55+
56+
if platform.python_implementation() == "PyPy":
57+
base = f"pypy{sys.version_info.major}{sys.version_info.minor}"
58+
else:
59+
base = f"py{sys.version_info.major}{sys.version_info.minor}"
60+
env = f"BASE={base}\n"
61+
print(f"Picked:\n{env}for {sys.version}")
62+
with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as file:
63+
file.write(env)
64+
shell: python
65+
66+
- name: Setup python for tox
67+
uses: actions/setup-python@v4
68+
with:
69+
python-version: 3.9
70+
71+
- name: Install tox
72+
run: python -m pip install tox
73+
74+
- name: Run test suite via tox
75+
if: matrix.tox-target == 'tox'
76+
run: |
77+
tox -vv --notest -e ${{env.BASE}}
78+
tox -e ${{env.BASE}} --skip-pkg-install
79+
80+
- name: Run minimum version test
81+
if: matrix.tox-target == 'min'
82+
run: tox -e ${{env.BASE}}-${{ matrix.tox-target }}
83+
84+
- name: Run path test
85+
if: matrix.tox-target == 'tox' && matrix.py == '3.10'
86+
run: tox -e path
87+
88+
- name: Combine coverage files
89+
if: always()
90+
run: tox -e coverage
91+
92+
- uses: codecov/codecov-action@v3
93+
if: always()
94+
env:
95+
PYTHON: ${{ matrix.python }}
96+
with:
97+
file: ./.tox/coverage.xml
98+
flags: tests
99+
env_vars: PYTHON
100+
name: ${{ matrix.py }} - ${{ matrix.os }}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: type
2+
on:
3+
workflow_call:
4+
5+
jobs:
6+
type:
7+
runs-on: ubuntu-latest
8+
env:
9+
PY_COLORS: 1
10+
TOX_PARALLEL_NO_SPINNER: 1
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- name: Setup Python 3.9
15+
uses: actions/setup-python@v4
16+
with:
17+
python-version: 3.9
18+
19+
- name: Install tox
20+
run: python -m pip install tox
21+
22+
- name: Setup run environment
23+
run: tox -vv --notest -e type
24+
25+
- name: Run check for type
26+
run: tox -e type --skip-pkg-install

‎.github/workflows/test.yml

Lines changed: 31 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@ on:
33
push:
44
branches:
55
- main
6-
paths-ignore:
7-
- "docs/**"
8-
- "*.md"
9-
- "*.rst"
106
pull_request:
117
branches:
128
- main
13-
paths-ignore:
14-
- "docs/**"
15-
- "*.md"
16-
- "*.rst"
179
schedule:
1810
- cron: "0 8 * * *"
1911
workflow_dispatch:
@@ -23,129 +15,31 @@ concurrency:
2315
cancel-in-progress: true
2416

2517
jobs:
26-
pytest:
27-
runs-on: ${{ matrix.os }}-latest
28-
env:
29-
PYTEST_ADDOPTS: "--run-integration --showlocals -vv --durations=10 --reruns 5 --only-rerun subprocess.CalledProcessError"
30-
strategy:
31-
fail-fast: false
32-
matrix:
33-
os:
34-
- ubuntu
35-
- macos
36-
- windows
37-
py:
38-
- "pypy-3.7"
39-
- "pypy-3.8"
40-
- "pypy-3.9"
41-
- "3.11"
42-
- "3.10"
43-
- "3.9"
44-
- "3.8"
45-
- "3.7"
46-
tox-target:
47-
- "tox"
48-
- "min"
49-
50-
continue-on-error: >- # jobs not required in branch protection
51-
${{
52-
(
53-
startsWith(matrix.py, 'pypy-')
54-
&& (!endsWith(matrix.py, '-3.7') || matrix.os == 'windows')
55-
)
56-
&& true
57-
|| false
58-
}}
59-
60-
steps:
61-
- uses: actions/checkout@v3
62-
with:
63-
fetch-depth: 0
64-
65-
- name: Setup python for test ${{ matrix.py }}
66-
uses: actions/setup-python@v4
67-
with:
68-
python-version: ${{ matrix.py }}
69-
70-
- name: Pick environment to run
71-
run: |
72-
import platform
73-
import os
74-
import sys
75-
76-
if platform.python_implementation() == "PyPy":
77-
base = f"pypy{sys.version_info.major}{sys.version_info.minor}"
78-
else:
79-
base = f"py{sys.version_info.major}{sys.version_info.minor}"
80-
env = f"BASE={base}\n"
81-
print(f"Picked:\n{env}for {sys.version}")
82-
with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as file:
83-
file.write(env)
84-
shell: python
85-
86-
- name: Setup python for tox
87-
uses: actions/setup-python@v4
88-
with:
89-
python-version: 3.9
90-
91-
- name: Install tox
92-
run: python -m pip install tox
18+
change-detection:
19+
uses: ./.github/workflows/reusable-change-detection.yml
9320

94-
- name: Run test suite via tox
95-
if: matrix.tox-target == 'tox'
96-
run: |
97-
tox -vv --notest -e ${{env.BASE}}
98-
tox -e ${{env.BASE}} --skip-pkg-install
21+
check-docs:
22+
needs: change-detection
23+
if: fromJSON(needs.change-detection.outputs.run-docs)
24+
uses: ./.github/workflows/reusable-docs.yml
9925

100-
- name: Run minimum version test
101-
if: matrix.tox-target == 'min'
102-
run: tox -e ${{env.BASE}}-${{ matrix.tox-target }}
103-
104-
- name: Run path test
105-
if: matrix.tox-target == 'tox' && matrix.py == '3.10'
106-
run: tox -e path
107-
108-
- name: Combine coverage files
109-
if: always()
110-
run: tox -e coverage
111-
112-
- uses: codecov/codecov-action@v3
113-
if: always()
114-
env:
115-
PYTHON: ${{ matrix.python }}
116-
with:
117-
file: ./.tox/coverage.xml
118-
flags: tests
119-
env_vars: PYTHON
120-
name: ${{ matrix.py }} - ${{ matrix.os }}
26+
pytest:
27+
needs: change-detection
28+
if: fromJSON(needs.change-detection.outputs.run-tests)
29+
uses: ./.github/workflows/reusable-pytest.yml
12130

12231
type:
123-
runs-on: ubuntu-latest
124-
env:
125-
PY_COLORS: 1
126-
TOX_PARALLEL_NO_SPINNER: 1
127-
steps:
128-
- uses: actions/checkout@v3
129-
130-
- name: Setup Python 3.9
131-
uses: actions/setup-python@v4
132-
with:
133-
python-version: 3.9
134-
135-
- name: Install tox
136-
run: python -m pip install tox
137-
138-
- name: Setup run environment
139-
run: tox -vv --notest -e type
140-
141-
- name: Run check for type
142-
run: tox -e type --skip-pkg-install
32+
needs: change-detection
33+
if: fromJSON(needs.change-detection.outputs.run-tests)
34+
uses: ./.github/workflows/reusable-type.yml
14335

14436
# https://github.com/marketplace/actions/alls-green#why
14537
required-checks-pass: # This job does nothing and is only used for the branch protection
14638
if: always()
14739

14840
needs:
41+
- change-detection # transitive
42+
- check-docs
14943
- pytest
15044
- type
15145

@@ -155,4 +49,20 @@ jobs:
15549
- name: Decide whether the needed jobs succeeded or failed
15650
uses: re-actors/alls-green@release/v1
15751
with:
52+
allowed-skips: >-
53+
${{
54+
fromJSON(needs.change-detection.outputs.run-docs)
55+
&& ''
56+
|| '
57+
check-docs,
58+
'
59+
}}
60+
${{
61+
fromJSON(needs.change-detection.outputs.run-tests)
62+
&& ''
63+
|| '
64+
pytest,
65+
type,
66+
'
67+
}}
15868
jobs: ${{ toJSON(needs) }}

‎README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# build
22

33
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/pypa/build/main.svg)](https://results.pre-commit.ci/latest/github/pypa/build/main)
4-
[![CI check](https://github.com/pypa/build/workflows/check/badge.svg)](https://github.com/pypa/build/actions)
54
[![CI test](https://github.com/pypa/build/actions/workflows/test.yml/badge.svg)](https://github.com/pypa/build/actions/workflows/test.yml)
65
[![codecov](https://codecov.io/gh/pypa/build/branch/main/graph/badge.svg)](https://codecov.io/gh/pypa/build)
76

0 commit comments

Comments
 (0)