Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions detect-changes/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,40 @@ inputs:
outputs:
detected:
description: Returns if any changed files were matched by the provided glob patterns.
value: ${{ steps.check.outputs.MATCHED }}
value: ${{ steps.check.outputs.DETECTED }}
runs:
using: composite
steps:
- name: Check for changed files
id: check
env:
PATTERNS: ${{ inputs.patterns }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
GITHUB_EVENT_NAME: ${{ github.event_name }}
shell: bash
run: |
set -euo pipefail

if [ "$GITHUB_EVENT_NAME" != 'pull_request' ]; then
# This is a short-circuit for when not run on a PR.
# It saves having to make the job conditions even more complicated.
echo "Not running on a PR. Hard-coding result:"
echo "DETECTED=true" | tee -a "$GITHUB_OUTPUT"
exit 0
fi

# TODO (@NickLarsenNZ): validate the YAML list. mikefarah-yq doesn't do this.

# This is cursed... we use `.[] | .` to remove quotes and any yaml formatting.
readarray -t GLOBS < <(echo -n "$PATTERNS" | yq '.[] | .')

MATCHED="false"
DETECTED="false"
for GLOB in "${GLOBS[@]}"; do
if ! git diff --exit-code --name-only "${BASE_SHA}.." -- "$GLOB"; then
# When git diff exist with an error, that means there was a diff
# for the glob.
MATCHED="true"
DETECTED="true"
fi
done

echo "MATCHED=$MATCHED" | tee -a "$GITHUB_OUTPUT"
echo "DETECTED=$DETECTED" | tee -a "$GITHUB_OUTPUT"