|
| 1 | +name: Detect Changed Files |
| 2 | +description: Smart file change detection for efficient CI |
| 3 | + |
| 4 | +inputs: |
| 5 | + pattern: |
| 6 | + description: 'Regex pattern to filter files' |
| 7 | + required: false |
| 8 | + default: '.*' |
| 9 | + |
| 10 | +outputs: |
| 11 | + changed_files: |
| 12 | + description: 'Newline-separated list of changed files' |
| 13 | + value: ${{ steps.detect.outputs.changed_files }} |
| 14 | + has_changes: |
| 15 | + description: 'Whether any matching files changed' |
| 16 | + value: ${{ steps.detect.outputs.has_changes }} |
| 17 | + file_count: |
| 18 | + description: 'Number of changed files' |
| 19 | + value: ${{ steps.detect.outputs.file_count }} |
| 20 | + has_ts_files: |
| 21 | + description: 'Whether any TypeScript files changed' |
| 22 | + value: ${{ steps.detect.outputs.has_ts_files }} |
| 23 | + has_test_files: |
| 24 | + description: 'Whether any test files changed' |
| 25 | + value: ${{ steps.detect.outputs.has_test_files }} |
| 26 | + ts_files: |
| 27 | + description: 'List of TypeScript files' |
| 28 | + value: ${{ steps.detect.outputs.ts_files }} |
| 29 | + test_files: |
| 30 | + description: 'List of test files' |
| 31 | + value: ${{ steps.detect.outputs.test_files }} |
| 32 | + |
| 33 | +runs: |
| 34 | + using: composite |
| 35 | + steps: |
| 36 | + - name: Detect changed files |
| 37 | + id: detect |
| 38 | + shell: bash |
| 39 | + run: | |
| 40 | + # Get changed files based on event type |
| 41 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 42 | + CHANGED=$(git diff --name-only \ |
| 43 | + ${{ github.event.pull_request.base.sha }} \ |
| 44 | + ${{ github.sha }}) |
| 45 | + elif [ "${{ github.event_name }}" = "push" ]; then |
| 46 | + if [ "${{ github.event.before }}" = "0000000000000000000000000000000000000000" ]; then |
| 47 | + # New branch |
| 48 | + CHANGED=$(git diff --name-only origin/main...HEAD 2>/dev/null || git ls-files) |
| 49 | + else |
| 50 | + CHANGED=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }}) |
| 51 | + fi |
| 52 | + else |
| 53 | + CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || git ls-files) |
| 54 | + fi |
| 55 | +
|
| 56 | + # Apply pattern filter |
| 57 | + PATTERN="${{ inputs.pattern }}" |
| 58 | + if [ "$PATTERN" != ".*" ]; then |
| 59 | + FILTERED=$(echo "$CHANGED" | grep -E "$PATTERN" || echo "") |
| 60 | + else |
| 61 | + FILTERED="$CHANGED" |
| 62 | + fi |
| 63 | +
|
| 64 | + # TypeScript files |
| 65 | + TS_FILES=$(echo "$CHANGED" | grep -E '\.(ts|tsx)$' || echo "") |
| 66 | +
|
| 67 | + # Test files |
| 68 | + TEST_FILES=$(echo "$CHANGED" | grep -E '\.(test|spec)\.(ts|tsx|js|jsx)$' || echo "") |
| 69 | +
|
| 70 | + # Count and flags |
| 71 | + if [ -n "$FILTERED" ]; then |
| 72 | + COUNT=$(echo "$FILTERED" | wc -l) |
| 73 | + HAS_CHANGES='true' |
| 74 | + else |
| 75 | + COUNT=0 |
| 76 | + HAS_CHANGES='false' |
| 77 | + fi |
| 78 | +
|
| 79 | + if [ -n "$TS_FILES" ]; then |
| 80 | + HAS_TS='true' |
| 81 | + else |
| 82 | + HAS_TS='false' |
| 83 | + fi |
| 84 | +
|
| 85 | + if [ -n "$TEST_FILES" ]; then |
| 86 | + HAS_TESTS='true' |
| 87 | + else |
| 88 | + HAS_TESTS='false' |
| 89 | + fi |
| 90 | +
|
| 91 | + # Set outputs |
| 92 | + echo "changed_files<<EOF" >> $GITHUB_OUTPUT |
| 93 | + echo "$FILTERED" >> $GITHUB_OUTPUT |
| 94 | + echo "EOF" >> $GITHUB_OUTPUT |
| 95 | +
|
| 96 | + echo "ts_files<<EOF" >> $GITHUB_OUTPUT |
| 97 | + echo "$TS_FILES" >> $GITHUB_OUTPUT |
| 98 | + echo "EOF" >> $GITHUB_OUTPUT |
| 99 | +
|
| 100 | + echo "test_files<<EOF" >> $GITHUB_OUTPUT |
| 101 | + echo "$TEST_FILES" >> $GITHUB_OUTPUT |
| 102 | + echo "EOF" >> $GITHUB_OUTPUT |
| 103 | +
|
| 104 | + echo "has_changes=$HAS_CHANGES" >> $GITHUB_OUTPUT |
| 105 | + echo "file_count=$COUNT" >> $GITHUB_OUTPUT |
| 106 | + echo "has_ts_files=$HAS_TS" >> $GITHUB_OUTPUT |
| 107 | + echo "has_test_files=$HAS_TESTS" >> $GITHUB_OUTPUT |
0 commit comments