diff --git a/README.md b/README.md index 5ea5642..fd1af60 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ particular step in a workflow. - [build-container-image](./build-container-image/README.md) - [build-product-image](./build-product-image/README.md) +- [detect-changes](./detect-changes/README.md) - [free-disk-space](./free-disk-space/README.md) - [publish-helm-chart](./publish-helm-chart/README.md) - [publish-image](./publish-image/README.md) diff --git a/detect-changes/README.md b/detect-changes/README.md new file mode 100644 index 0000000..e621ac5 --- /dev/null +++ b/detect-changes/README.md @@ -0,0 +1,25 @@ +# `detect-changes` + +> Manifest: [detect-changes/action.yml][detect-changes] + +This action detects changed files by providing a list of glob patterns. +It can be used in situations where the GHA native path filtering cannot be used. + +## Inputs and Outputs + +> [!TIP] +> For descriptions of the inputs and outputs, see the complete [detect-changes] action. + +### Inputs + +| Input | Required | Description | +| ---------- | -------- | --------------------------------------------------------------- | +| `patterns` | No | A list of glob patterns to detect changes in. Defaults to ['*'] | + +### Outputs + +| Output | Description | +| ----------- | ------------------------------------------------------------------------------------------------ | +| `detected` | `'true'` or `'false'` indicating if any changed files were matched by the provided glob patterns | + +[detect-changes]: ./action.yaml diff --git a/detect-changes/action.yaml b/detect-changes/action.yaml new file mode 100644 index 0000000..377b67e --- /dev/null +++ b/detect-changes/action.yaml @@ -0,0 +1,35 @@ +--- +name: detect-changes +description: Detect changed files by providing a list of glob patterns +inputs: + patterns: + description: A list of glob patterns to detect changes in. Defaults to ['*']. + default: "['*']" +outputs: + detected: + description: Returns if any changed files were matched by the provided glob patterns. + value: ${{ steps.check.outputs.MATCHED }} +runs: + using: composite + steps: + - name: Check for changed files + id: check + env: + PATTERNS: ${{ inputs.patterns }} + shell: bash + run: | + set -euo pipefail + + # This is cursed... we use `.[] | .` to remove quotes and any yaml formatting. + readarray -t GLOBS < <(echo -n "$PATTERNS" | yq '.[] | .') + + MATCHED="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" + fi + done + + echo "MATCHED=$MATCHED" | tee -a "$GITHUB_OUTPUT"