|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +# Single source of truth for which PR diffs should skip code CI. |
| 19 | +# |
| 20 | +# Workflows that gate jobs on "is this a real code change?" call this |
| 21 | +# composite action; every existing job downstream then uses |
| 22 | +# `if: needs.detect-changes.outputs.has_code == 'true'`. The caller is |
| 23 | +# responsible for `actions/checkout` with `fetch-depth: 0` before invoking |
| 24 | +# this action — we need full history to compute the PR diff. |
| 25 | + |
| 26 | +name: 'Detect changes' |
| 27 | +description: >- |
| 28 | + Emit has_code='true' if the PR contains any changed file that does NOT |
| 29 | + match `ignore-paths`. When every changed file matches, has_code='false' |
| 30 | + and dependent jobs should report `skipped`. |
| 31 | +
|
| 32 | +inputs: |
| 33 | + ignore-paths: |
| 34 | + description: | |
| 35 | + Newline-separated glob patterns. If EVERY changed file in the PR |
| 36 | + matches one of these globs, has_code='false' (code CI is skipped — |
| 37 | + only dev.yml / docs*.yaml jobs run). |
| 38 | +
|
| 39 | + Glob syntax matches `pull_request: paths-ignore:`: |
| 40 | + ** any depth |
| 41 | + * any chars within one path segment |
| 42 | + foo/bar.txt a single specific file |
| 43 | + subdir/** everything under subdir/ |
| 44 | + **.png any .png file at any depth |
| 45 | + **/CHANGELOG.md CHANGELOG.md in any directory |
| 46 | +
|
| 47 | + To add a new entry: append one line below. To override per-workflow, |
| 48 | + pass `with: ignore-paths: |` from the caller. |
| 49 | + required: false |
| 50 | + default: | |
| 51 | + docs/** |
| 52 | + **.md |
| 53 | + .github/ISSUE_TEMPLATE/** |
| 54 | + .github/pull_request_template.md |
| 55 | +
|
| 56 | +outputs: |
| 57 | + has_code: |
| 58 | + description: >- |
| 59 | + 'true' if at least one changed file is outside `ignore-paths`, |
| 60 | + 'false' if every changed file matches one of those globs. |
| 61 | + value: ${{ steps.filter.outputs.has_code }} |
| 62 | + |
| 63 | +runs: |
| 64 | + using: composite |
| 65 | + steps: |
| 66 | + - id: filter |
| 67 | + shell: bash |
| 68 | + env: |
| 69 | + BASE_SHA: ${{ github.event.pull_request.base.sha }} |
| 70 | + HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 71 | + IGNORE_CODE_CI_FOR_PATHS: ${{ inputs.ignore-paths }} |
| 72 | + run: | |
| 73 | + # Fail-open: if anything goes wrong, default to running every job |
| 74 | + # rather than silently skipping (which would let broken CI merge). |
| 75 | + set +e |
| 76 | + if [ -z "$BASE_SHA" ]; then |
| 77 | + echo "has_code=true" >> "$GITHUB_OUTPUT"; exit 0 |
| 78 | + fi |
| 79 | + # Translate IGNORE_CODE_CI_FOR_PATHS (one glob per line) into git |
| 80 | + # pathspec excludes; `:(exclude,glob)` activates ** globbing. |
| 81 | + excludes=() |
| 82 | + while IFS= read -r pattern; do |
| 83 | + [ -n "$pattern" ] && excludes+=( ":(exclude,glob)$pattern" ) |
| 84 | + done <<< "$IGNORE_CODE_CI_FOR_PATHS" |
| 85 | + # If anything remains after stripping ignored paths, the PR has |
| 86 | + # real code changes and code CI should run. |
| 87 | + diff_output=$(git diff --name-only "$BASE_SHA..$HEAD_SHA" -- "${excludes[@]}") |
| 88 | + if [ -n "$diff_output" ]; then |
| 89 | + echo "has_code=true" >> "$GITHUB_OUTPUT" |
| 90 | + else |
| 91 | + echo "has_code=false" >> "$GITHUB_OUTPUT" |
| 92 | + fi |
| 93 | + exit 0 |
0 commit comments