Skip to content

Commit c994611

Browse files
committed
Move change detection to separate job in CI
1 parent d52726c commit c994611

File tree

2 files changed

+151
-118
lines changed

2 files changed

+151
-118
lines changed

.github/workflows/build.yml

+13-118
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
name: Tests
22

3-
# gh-84728: "paths-ignore" is not used to skip documentation-only PRs, because
4-
# it prevents to mark a job as mandatory. A PR cannot be merged if a job is
5-
# mandatory but not scheduled because of "paths-ignore".
63
on:
74
workflow_dispatch:
85
push:
@@ -23,121 +20,19 @@ concurrency:
2320

2421
jobs:
2522
check_source:
26-
name: 'Check for source changes'
27-
runs-on: ubuntu-latest
28-
timeout-minutes: 10
29-
outputs:
30-
# Some of the referenced steps set outputs conditionally and there may be
31-
# cases when referencing them evaluates to empty strings. It is nice to
32-
# work with proper booleans so they have to be evaluated through JSON
33-
# conversion in the expressions. However, empty strings used like that
34-
# may trigger all sorts of undefined and hard-to-debug behaviors in
35-
# GitHub Actions CI/CD. To help with this, all of the outputs set here
36-
# that are meant to be used as boolean flags (and not arbitrary strings),
37-
# MUST have fallbacks with default values set. A common pattern would be
38-
# to add ` || false` to all such expressions here, in the output
39-
# definitions. They can then later be safely used through the following
40-
# idiom in job conditionals and other expressions. Here's some examples:
41-
#
42-
# if: fromJSON(needs.check_source.outputs.run-docs)
43-
#
44-
# ${{
45-
# fromJSON(needs.check_source.outputs.run_tests)
46-
# && 'truthy-branch'
47-
# || 'falsy-branch'
48-
# }}
49-
#
50-
run-docs: ${{ steps.docs-changes.outputs.run-docs || false }}
51-
run-win-msi: ${{ steps.win-msi-changes.outputs.run-win-msi || false }}
52-
run_tests: ${{ steps.check.outputs.run_tests || false }}
53-
run_hypothesis: ${{ steps.check.outputs.run_hypothesis || false }}
54-
run_cifuzz: ${{ steps.check.outputs.run_cifuzz || false }}
55-
config_hash: ${{ steps.config_hash.outputs.hash }} # str
56-
steps:
57-
- uses: actions/checkout@v4
58-
- name: Check for source changes
59-
id: check
60-
run: |
61-
if [ -z "$GITHUB_BASE_REF" ]; then
62-
echo "run_tests=true" >> $GITHUB_OUTPUT
63-
else
64-
git fetch origin $GITHUB_BASE_REF --depth=1
65-
# git diff "origin/$GITHUB_BASE_REF..." (3 dots) may be more
66-
# reliable than git diff "origin/$GITHUB_BASE_REF.." (2 dots),
67-
# but it requires to download more commits (this job uses
68-
# "git fetch --depth=1").
69-
#
70-
# git diff "origin/$GITHUB_BASE_REF..." (3 dots) works with Git
71-
# 2.26, but Git 2.28 is stricter and fails with "no merge base".
72-
#
73-
# git diff "origin/$GITHUB_BASE_REF.." (2 dots) should be enough on
74-
# GitHub, since GitHub starts by merging origin/$GITHUB_BASE_REF
75-
# into the PR branch anyway.
76-
#
77-
# https://github.com/python/core-workflow/issues/373
78-
git diff --name-only origin/$GITHUB_BASE_REF.. | grep -qvE '(\.rst$|^Doc|^Misc|^\.pre-commit-config\.yaml$|\.ruff\.toml$|\.md$|mypy\.ini$)' && echo "run_tests=true" >> $GITHUB_OUTPUT || true
79-
fi
80-
81-
# Check if we should run hypothesis tests
82-
GIT_BRANCH=${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}}
83-
echo $GIT_BRANCH
84-
if $(echo "$GIT_BRANCH" | grep -q -w '3\.\(8\|9\|10\|11\)'); then
85-
echo "Branch too old for hypothesis tests"
86-
echo "run_hypothesis=false" >> $GITHUB_OUTPUT
87-
else
88-
echo "Run hypothesis tests"
89-
echo "run_hypothesis=true" >> $GITHUB_OUTPUT
90-
fi
91-
92-
# oss-fuzz maintains a configuration for fuzzing the main branch of
93-
# CPython, so CIFuzz should be run only for code that is likely to be
94-
# merged into the main branch; compatibility with older branches may
95-
# be broken.
96-
FUZZ_RELEVANT_FILES='(\.c$|\.h$|\.cpp$|^configure$|^\.github/workflows/build\.yml$|^Modules/_xxtestfuzz)'
97-
if [ "$GITHUB_BASE_REF" = "main" ] && [ "$(git diff --name-only origin/$GITHUB_BASE_REF.. | grep -qE $FUZZ_RELEVANT_FILES; echo $?)" -eq 0 ]; then
98-
# The tests are pretty slow so they are executed only for PRs
99-
# changing relevant files.
100-
echo "Run CIFuzz tests"
101-
echo "run_cifuzz=true" >> $GITHUB_OUTPUT
102-
else
103-
echo "Branch too old for CIFuzz tests; or no C files were changed"
104-
echo "run_cifuzz=false" >> $GITHUB_OUTPUT
105-
fi
106-
- name: Compute hash for config cache key
107-
id: config_hash
108-
run: |
109-
echo "hash=${{ hashFiles('configure', 'configure.ac', '.github/workflows/build.yml') }}" >> $GITHUB_OUTPUT
110-
- name: Get a list of the changed documentation-related files
111-
if: github.event_name == 'pull_request'
112-
id: changed-docs-files
113-
uses: Ana06/[email protected]
114-
with:
115-
filter: |
116-
Doc/**
117-
Misc/**
118-
.github/workflows/reusable-docs.yml
119-
format: csv # works for paths with spaces
120-
- name: Check for docs changes
121-
if: >-
122-
github.event_name == 'pull_request'
123-
&& steps.changed-docs-files.outputs.added_modified_renamed != ''
124-
id: docs-changes
125-
run: |
126-
echo "run-docs=true" >> "${GITHUB_OUTPUT}"
127-
- name: Get a list of the MSI installer-related files
128-
id: changed-win-msi-files
129-
uses: Ana06/[email protected]
130-
with:
131-
filter: |
132-
Tools/msi/**
133-
.github/workflows/reusable-windows-msi.yml
134-
format: csv # works for paths with spaces
135-
- name: Check for changes in MSI installer-related files
136-
if: >-
137-
steps.changed-win-msi-files.outputs.added_modified_renamed != ''
138-
id: win-msi-changes
139-
run: |
140-
echo "run-win-msi=true" >> "${GITHUB_OUTPUT}"
23+
name: Change detection
24+
# To use boolean outputs from this job, parse them as JSON.
25+
# Here's some examples:
26+
#
27+
# if: fromJSON(needs.check_source.outputs.run-docs)
28+
#
29+
# ${{
30+
# fromJSON(needs.check_source.outputs.run_tests)
31+
# && 'truthy-branch'
32+
# || 'falsy-branch'
33+
# }}
34+
#
35+
uses: ./.github/workflows/reusable-change-detection.yml
14136

14237
check-docs:
14338
name: Docs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
3+
name: Change detection
4+
5+
on: # yamllint disable-line rule:truthy
6+
workflow_call:
7+
outputs:
8+
# Some of the referenced steps set outputs conditionally and there may be
9+
# cases when referencing them evaluates to empty strings. It is nice to
10+
# work with proper booleans so they have to be evaluated through JSON
11+
# conversion in the expressions. However, empty strings used like that
12+
# may trigger all sorts of undefined and hard-to-debug behaviors in
13+
# GitHub Actions CI/CD. To help with this, all of the outputs set here
14+
# that are meant to be used as boolean flags (and not arbitrary strings),
15+
# MUST have fallbacks with default values set. A common pattern would be
16+
# to add ` || false` to all such expressions here, in the output
17+
# definitions. They can then later be safely used through the following
18+
# idiom in job conditionals and other expressions. Here's some examples:
19+
#
20+
# if: fromJSON(needs.change-detection.outputs.run-docs)
21+
#
22+
# ${{
23+
# fromJSON(needs.change-detection.outputs.run-tests)
24+
# && 'truthy-branch'
25+
# || 'falsy-branch'
26+
# }}
27+
#
28+
config_hash: ${{ jobs.compute-changes.outputs.config-hash }} # str
29+
run-docs: ${{ jobs.compute-changes.outputs.run-docs || false }} # bool
30+
run_tests: ${{ jobs.compute-changes.outputs.run-tests || false }} # bool
31+
run-win-msi: >- # bool
32+
${{ jobs.compute-changes.outputs.run-win-msi || false }}
33+
run_hypothesis: >- # bool
34+
${{ jobs.compute-changes.outputs.run-hypothesis || false }}
35+
run_cifuzz: >- # bool
36+
${{ jobs.compute-changes.outputs.run-cifuzz || false }}
37+
38+
jobs:
39+
compute-changes:
40+
name: Compute changed files
41+
runs-on: ubuntu-latest
42+
timeout-minutes: 10
43+
outputs:
44+
config-hash: ${{ steps.config-hash.outputs.hash }}
45+
run-cifuzz: ${{ steps.check.outputs.run-cifuzz }}
46+
run-docs: ${{ steps.docs-changes.outputs.run-docs }}
47+
run-hypothesis: ${{ steps.check.outputs.run-hypothesis }}
48+
run-tests: ${{ steps.check.outputs.run-tests }}
49+
run-win-msi: ${{ steps.win-msi-changes.outputs.run-win-msi }}
50+
steps:
51+
- run: >-
52+
echo '${{ github.event_name }}'
53+
- uses: actions/checkout@v4
54+
- name: Check for source changes
55+
id: check
56+
run: |
57+
if [ -z "$GITHUB_BASE_REF" ]; then
58+
echo "run-tests=true" >> $GITHUB_OUTPUT
59+
else
60+
git fetch origin $GITHUB_BASE_REF --depth=1
61+
# git diff "origin/$GITHUB_BASE_REF..." (3 dots) may be more
62+
# reliable than git diff "origin/$GITHUB_BASE_REF.." (2 dots),
63+
# but it requires to download more commits (this job uses
64+
# "git fetch --depth=1").
65+
#
66+
# git diff "origin/$GITHUB_BASE_REF..." (3 dots) works with Git
67+
# 2.26, but Git 2.28 is stricter and fails with "no merge base".
68+
#
69+
# git diff "origin/$GITHUB_BASE_REF.." (2 dots) should be enough on
70+
# GitHub, since GitHub starts by merging origin/$GITHUB_BASE_REF
71+
# into the PR branch anyway.
72+
#
73+
# https://github.com/python/core-workflow/issues/373
74+
git diff --name-only origin/$GITHUB_BASE_REF.. | grep -qvE '(\.rst$|^Doc|^Misc|^\.pre-commit-config\.yaml$|\.ruff\.toml$|\.md$|mypy\.ini$)' && echo "run-tests=true" >> $GITHUB_OUTPUT || true
75+
fi
76+
77+
# Check if we should run hypothesis tests
78+
GIT_BRANCH=${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}}
79+
echo $GIT_BRANCH
80+
if $(echo "$GIT_BRANCH" | grep -q -w '3\.\(8\|9\|10\|11\)'); then
81+
echo "Branch too old for hypothesis tests"
82+
echo "run-hypothesis=false" >> $GITHUB_OUTPUT
83+
else
84+
echo "Run hypothesis tests"
85+
echo "run-hypothesis=true" >> $GITHUB_OUTPUT
86+
fi
87+
88+
# oss-fuzz maintains a configuration for fuzzing the main branch of
89+
# CPython, so CIFuzz should be run only for code that is likely to be
90+
# merged into the main branch; compatibility with older branches may
91+
# be broken.
92+
FUZZ_RELEVANT_FILES='(\.c$|\.h$|\.cpp$|^configure$|^\.github/workflows/build\.yml$|^Modules/_xxtestfuzz)'
93+
if [ "$GITHUB_BASE_REF" = "main" ] && [ "$(git diff --name-only origin/$GITHUB_BASE_REF.. | grep -qE $FUZZ_RELEVANT_FILES; echo $?)" -eq 0 ]; then
94+
# The tests are pretty slow so they are executed only for PRs
95+
# changing relevant files.
96+
echo "Run CIFuzz tests"
97+
echo "run-cifuzz=true" >> $GITHUB_OUTPUT
98+
else
99+
echo "Branch too old for CIFuzz tests; or no C files were changed"
100+
echo "run-cifuzz=false" >> $GITHUB_OUTPUT
101+
fi
102+
- name: Compute hash for config cache key
103+
id: config-hash
104+
run: |
105+
echo "hash=${{ hashFiles('configure', 'configure.ac', '.github/workflows/build.yml') }}" >> $GITHUB_OUTPUT
106+
- name: Get a list of the changed documentation-related files
107+
if: github.event_name == 'pull_request'
108+
id: changed-docs-files
109+
uses: Ana06/[email protected]
110+
with:
111+
filter: |
112+
Doc/**
113+
Misc/**
114+
.github/workflows/reusable-docs.yml
115+
format: csv # works for paths with spaces
116+
- name: Check for docs changes
117+
if: >-
118+
github.event_name == 'pull_request'
119+
&& steps.changed-docs-files.outputs.added_modified_renamed != ''
120+
id: docs-changes
121+
run: |
122+
echo "run-docs=true" >> "${GITHUB_OUTPUT}"
123+
- name: Get a list of the MSI installer-related files
124+
id: changed-win-msi-files
125+
uses: Ana06/[email protected]
126+
with:
127+
filter: |
128+
Tools/msi/**
129+
.github/workflows/reusable-windows-msi.yml
130+
format: csv # works for paths with spaces
131+
- name: Check for changes in MSI installer-related files
132+
if: >-
133+
steps.changed-win-msi-files.outputs.added_modified_renamed != ''
134+
id: win-msi-changes
135+
run: |
136+
echo "run-win-msi=true" >> "${GITHUB_OUTPUT}"
137+
138+
...

0 commit comments

Comments
 (0)