|
| 1 | +name: Check File Changes |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + skip-extensions: |
| 7 | + description: Space-separated list of file extensions that should skip tests |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + default: md mdc txt rst png jpg jpeg gif svg ico pdf |
| 11 | + outputs: |
| 12 | + should-skip: |
| 13 | + description: Whether the workflow should skip (only skippable files changed) |
| 14 | + value: ${{ jobs.check-changes.outputs.should-skip }} |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | +jobs: |
| 20 | + check-changes: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + outputs: |
| 23 | + should-skip: ${{ steps.check.outputs.should-skip }} |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v5 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Check if only skippable files changed |
| 30 | + id: check |
| 31 | + run: | |
| 32 | + # Use input parameter or default |
| 33 | + SKIP_EXTENSIONS="${{ inputs.skip-extensions }}" |
| 34 | +
|
| 35 | + if [ "${{ github.event_name }}" == "schedule" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 36 | + echo "Triggered by schedule or manual dispatch, running full tests" |
| 37 | + echo "should-skip=false" >> $GITHUB_OUTPUT |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | +
|
| 41 | + # Get the list of changed files |
| 42 | + git fetch origin ${{ github.base_ref }} |
| 43 | + CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD) |
| 44 | +
|
| 45 | + # Check if we have any changes |
| 46 | + if [ -z "$CHANGED_FILES" ]; then |
| 47 | + echo "No files changed, running tests" |
| 48 | + echo "should-skip=false" >> $GITHUB_OUTPUT |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +
|
| 52 | + # Build regex pattern from extensions (e.g., \.(md|txt|png)$) |
| 53 | + PATTERN=$(echo "$SKIP_EXTENSIONS" | sed 's/ /|/g') |
| 54 | + REGEX="\.($PATTERN)$" |
| 55 | +
|
| 56 | + # Check if all changes match skippable extensions |
| 57 | + NON_SKIPPABLE_FILES=$(echo "$CHANGED_FILES" | grep -Ev "$REGEX" || true) |
| 58 | +
|
| 59 | + if [ -z "$NON_SKIPPABLE_FILES" ]; then |
| 60 | + echo "Only skippable files changed (extensions: $SKIP_EXTENSIONS), skipping tests" |
| 61 | + echo "should-skip=true" >> $GITHUB_OUTPUT |
| 62 | + else |
| 63 | + echo "Non-skippable files changed, running tests" |
| 64 | + echo "should-skip=false" >> $GITHUB_OUTPUT |
| 65 | + fi |
| 66 | +
|
| 67 | + skip-job: |
| 68 | + needs: check-changes |
| 69 | + if: needs.check-changes.outputs.should-skip == 'true' |
| 70 | + runs-on: ubuntu-latest |
| 71 | + steps: |
| 72 | + - name: Skip tests |
| 73 | + run: echo "Only documentation/assets changed, skipping tests" |
0 commit comments