|
| 1 | +name: 'Check Version Keys' |
| 2 | +description: 'Check if specific versions.yml keys changed to decide whether a workflow should run. Keys are auto-detected from yq calls in the calling workflow file.' |
| 3 | + |
| 4 | +inputs: |
| 5 | + base-sha: |
| 6 | + description: 'Base commit SHA for comparison' |
| 7 | + required: true |
| 8 | + head-sha: |
| 9 | + description: 'Head commit SHA for comparison' |
| 10 | + required: true |
| 11 | + event-name: |
| 12 | + description: 'GitHub event name (push, pull_request, workflow_dispatch)' |
| 13 | + required: true |
| 14 | + |
| 15 | +outputs: |
| 16 | + should-run: |
| 17 | + description: '"true" if the workflow should run, "false" if it can be skipped' |
| 18 | + value: ${{ steps.check.outputs.should-run }} |
| 19 | + |
| 20 | +runs: |
| 21 | + using: 'composite' |
| 22 | + steps: |
| 23 | + - id: check |
| 24 | + shell: bash |
| 25 | + run: | |
| 26 | + EVENT="${{ inputs.event-name }}" |
| 27 | + BASE_SHA="${{ inputs.base-sha }}" |
| 28 | +
|
| 29 | + # Always run on workflow_dispatch or if base SHA is missing |
| 30 | + if [ "$EVENT" = "workflow_dispatch" ] || [ -z "$BASE_SHA" ]; then |
| 31 | + echo "should-run=true" >> "$GITHUB_OUTPUT" |
| 32 | + echo "Always run: event=$EVENT base_sha=$BASE_SHA" |
| 33 | + exit 0 |
| 34 | + fi |
| 35 | +
|
| 36 | + # Check if versions.yml is in the diff |
| 37 | + CHANGED_FILES=$(git diff --name-only "$BASE_SHA" "${{ inputs.head-sha }}" 2>/dev/null || true) |
| 38 | +
|
| 39 | + if ! echo "$CHANGED_FILES" | grep -q '^versions\.yml$'; then |
| 40 | + # versions.yml not changed — triggered by harness file changes, always run |
| 41 | + echo "should-run=true" >> "$GITHUB_OUTPUT" |
| 42 | + echo "Always run: versions.yml not in diff" |
| 43 | + exit 0 |
| 44 | + fi |
| 45 | +
|
| 46 | + # Auto-detect keys from the calling workflow file |
| 47 | + WORKFLOW_FILE=".github/workflows/$(echo "$GITHUB_WORKFLOW_REF" | sed -n 's|.*\.github/workflows/\(.*\)@.*|\1|p')" |
| 48 | + echo "Detected workflow file: $WORKFLOW_FILE" |
| 49 | +
|
| 50 | + if [ ! -f "$WORKFLOW_FILE" ]; then |
| 51 | + echo "should-run=true" >> "$GITHUB_OUTPUT" |
| 52 | + echo "Always run: could not locate workflow file" |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | +
|
| 56 | + KEYS=$(grep -oP "yq\s+'(\.[^']+)'\s+versions\.yml" "$WORKFLOW_FILE" | grep -oP "'\.[^']+'" | tr -d "'" | sort -u | tr '\n' ' ') |
| 57 | + echo "Auto-detected keys: $KEYS" |
| 58 | +
|
| 59 | + if [ -z "$KEYS" ]; then |
| 60 | + echo "should-run=true" >> "$GITHUB_OUTPUT" |
| 61 | + echo "Always run: no version keys detected in workflow" |
| 62 | + exit 0 |
| 63 | + fi |
| 64 | +
|
| 65 | + # versions.yml changed — check if any of the specific keys changed |
| 66 | + git fetch origin "$BASE_SHA" --depth=1 2>/dev/null || true |
| 67 | + OLD_FILE=$(git show "$BASE_SHA":versions.yml 2>/dev/null || echo "") |
| 68 | +
|
| 69 | + if [ -z "$OLD_FILE" ]; then |
| 70 | + # Can't get old file (new file or shallow clone issue) — run to be safe |
| 71 | + echo "should-run=true" >> "$GITHUB_OUTPUT" |
| 72 | + echo "Always run: could not retrieve old versions.yml" |
| 73 | + exit 0 |
| 74 | + fi |
| 75 | +
|
| 76 | + for KEY in $KEYS; do |
| 77 | + OLD_VAL=$(echo "$OLD_FILE" | yq "$KEY" 2>/dev/null || echo "") |
| 78 | + NEW_VAL=$(yq "$KEY" versions.yml 2>/dev/null || echo "") |
| 79 | + if [ "$OLD_VAL" != "$NEW_VAL" ]; then |
| 80 | + echo "should-run=true" >> "$GITHUB_OUTPUT" |
| 81 | + echo "Key changed: $KEY ($OLD_VAL -> $NEW_VAL)" |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | + echo "Key unchanged: $KEY ($OLD_VAL)" |
| 85 | + done |
| 86 | +
|
| 87 | + echo "should-run=false" >> "$GITHUB_OUTPUT" |
| 88 | + echo "No relevant keys changed, skipping" |
0 commit comments