|
| 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: |
| 29 | + description: Config hash value for use in cache keys |
| 30 | + value: ${{ jobs.compute-changes.outputs.config-hash }} # str |
| 31 | + run-docs: |
| 32 | + description: Whether to build the docs |
| 33 | + value: ${{ jobs.compute-changes.outputs.run-docs || false }} # bool |
| 34 | + run_tests: |
| 35 | + description: Whether to run the regular tests |
| 36 | + value: ${{ jobs.compute-changes.outputs.run-tests || false }} # bool |
| 37 | + run-win-msi: |
| 38 | + description: Whether to run the MSI installer smoke tests |
| 39 | + value: >- # bool |
| 40 | + ${{ jobs.compute-changes.outputs.run-win-msi || false }} |
| 41 | + run_hypothesis: |
| 42 | + description: Whether to run the Hypothesis tests |
| 43 | + value: >- # bool |
| 44 | + ${{ jobs.compute-changes.outputs.run-hypothesis || false }} |
| 45 | + run_cifuzz: |
| 46 | + description: Whether to run the CIFuzz job |
| 47 | + value: >- # bool |
| 48 | + ${{ jobs.compute-changes.outputs.run-cifuzz || false }} |
| 49 | +
|
| 50 | +jobs: |
| 51 | + compute-changes: |
| 52 | + name: Compute changed files |
| 53 | + runs-on: ubuntu-latest |
| 54 | + timeout-minutes: 10 |
| 55 | + outputs: |
| 56 | + config-hash: ${{ steps.config-hash.outputs.hash }} |
| 57 | + run-cifuzz: ${{ steps.check.outputs.run-cifuzz }} |
| 58 | + run-docs: ${{ steps.docs-changes.outputs.run-docs }} |
| 59 | + run-hypothesis: ${{ steps.check.outputs.run-hypothesis }} |
| 60 | + run-tests: ${{ steps.check.outputs.run-tests }} |
| 61 | + run-win-msi: ${{ steps.win-msi-changes.outputs.run-win-msi }} |
| 62 | + steps: |
| 63 | + - run: >- |
| 64 | + echo '${{ github.event_name }}' |
| 65 | + - uses: actions/checkout@v4 |
| 66 | + - name: Check for source changes |
| 67 | + id: check |
| 68 | + run: | |
| 69 | + if [ -z "$GITHUB_BASE_REF" ]; then |
| 70 | + echo "run-tests=true" >> $GITHUB_OUTPUT |
| 71 | + else |
| 72 | + git fetch origin $GITHUB_BASE_REF --depth=1 |
| 73 | + # git diff "origin/$GITHUB_BASE_REF..." (3 dots) may be more |
| 74 | + # reliable than git diff "origin/$GITHUB_BASE_REF.." (2 dots), |
| 75 | + # but it requires to download more commits (this job uses |
| 76 | + # "git fetch --depth=1"). |
| 77 | + # |
| 78 | + # git diff "origin/$GITHUB_BASE_REF..." (3 dots) works with Git |
| 79 | + # 2.26, but Git 2.28 is stricter and fails with "no merge base". |
| 80 | + # |
| 81 | + # git diff "origin/$GITHUB_BASE_REF.." (2 dots) should be enough on |
| 82 | + # GitHub, since GitHub starts by merging origin/$GITHUB_BASE_REF |
| 83 | + # into the PR branch anyway. |
| 84 | + # |
| 85 | + # https://github.com/python/core-workflow/issues/373 |
| 86 | + 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 |
| 87 | + fi |
| 88 | +
|
| 89 | + # Check if we should run hypothesis tests |
| 90 | + GIT_BRANCH=${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}} |
| 91 | + echo $GIT_BRANCH |
| 92 | + if $(echo "$GIT_BRANCH" | grep -q -w '3\.\(8\|9\|10\|11\)'); then |
| 93 | + echo "Branch too old for hypothesis tests" |
| 94 | + echo "run-hypothesis=false" >> $GITHUB_OUTPUT |
| 95 | + else |
| 96 | + echo "Run hypothesis tests" |
| 97 | + echo "run-hypothesis=true" >> $GITHUB_OUTPUT |
| 98 | + fi |
| 99 | +
|
| 100 | + # oss-fuzz maintains a configuration for fuzzing the main branch of |
| 101 | + # CPython, so CIFuzz should be run only for code that is likely to be |
| 102 | + # merged into the main branch; compatibility with older branches may |
| 103 | + # be broken. |
| 104 | + FUZZ_RELEVANT_FILES='(\.c$|\.h$|\.cpp$|^configure$|^\.github/workflows/build\.yml$|^Modules/_xxtestfuzz)' |
| 105 | + if [ "$GITHUB_BASE_REF" = "main" ] && [ "$(git diff --name-only origin/$GITHUB_BASE_REF.. | grep -qE $FUZZ_RELEVANT_FILES; echo $?)" -eq 0 ]; then |
| 106 | + # The tests are pretty slow so they are executed only for PRs |
| 107 | + # changing relevant files. |
| 108 | + echo "Run CIFuzz tests" |
| 109 | + echo "run-cifuzz=true" >> $GITHUB_OUTPUT |
| 110 | + else |
| 111 | + echo "Branch too old for CIFuzz tests; or no C files were changed" |
| 112 | + echo "run-cifuzz=false" >> $GITHUB_OUTPUT |
| 113 | + fi |
| 114 | + - name: Compute hash for config cache key |
| 115 | + id: config-hash |
| 116 | + run: | |
| 117 | + echo "hash=${{ hashFiles('configure', 'configure.ac', '.github/workflows/build.yml') }}" >> $GITHUB_OUTPUT |
| 118 | + - name: Get a list of the changed documentation-related files |
| 119 | + if: github.event_name == 'pull_request' |
| 120 | + id: changed-docs-files |
| 121 | + uses: Ana06/get-changed-files@v2.3.0 |
| 122 | + with: |
| 123 | + filter: | |
| 124 | + Doc/** |
| 125 | + Misc/** |
| 126 | + .github/workflows/reusable-docs.yml |
| 127 | + format: csv # works for paths with spaces |
| 128 | + - name: Check for docs changes |
| 129 | + if: >- |
| 130 | + github.event_name == 'pull_request' |
| 131 | + && steps.changed-docs-files.outputs.added_modified_renamed != '' |
| 132 | + id: docs-changes |
| 133 | + run: | |
| 134 | + echo "run-docs=true" >> "${GITHUB_OUTPUT}" |
| 135 | + - name: Get a list of the MSI installer-related files |
| 136 | + id: changed-win-msi-files |
| 137 | + uses: Ana06/get-changed-files@v2.3.0 |
| 138 | + with: |
| 139 | + filter: | |
| 140 | + Tools/msi/** |
| 141 | + .github/workflows/reusable-windows-msi.yml |
| 142 | + format: csv # works for paths with spaces |
| 143 | + - name: Check for changes in MSI installer-related files |
| 144 | + if: >- |
| 145 | + steps.changed-win-msi-files.outputs.added_modified_renamed != '' |
| 146 | + id: win-msi-changes |
| 147 | + run: | |
| 148 | + echo "run-win-msi=true" >> "${GITHUB_OUTPUT}" |
| 149 | +
|
| 150 | +... |
0 commit comments