Skip to content

Commit

Permalink
[ci] fix files_changed to differentiate when you are in a pr or main …
Browse files Browse the repository at this point in the history
…as well as fix check_bazel and files_changed base_sha to point to HEAD~1 when running on a main merge
  • Loading branch information
pablo-guardiola committed Sep 10, 2024
1 parent cf1be40 commit 8ea3b60
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ci/check_bazel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if [ -n "$GITHUB_BASE_REF" ]; then
base_sha=$(git rev-parse "$GITHUB_BASE_REF")
else
echo "Not in a pull request, skipping base ref fetch."
base_sha=$(git rev-parse HEAD)
base_sha=$(git rev-parse HEAD~1)
fi

# Get the latest commit SHA for the PR branch (the head ref in the forked repository)
Expand Down
25 changes: 21 additions & 4 deletions ci/files_changed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@
#
# Usage: ./ci/files_changed.sh <regex>

set -e
set -euo pipefail

# Trap to handle unexpected errors and log them
trap 'echo "An unexpected error occurred during file change check."; echo "check_result=1" >> "$GITHUB_OUTPUT"; exit 1' ERR

# Check for file changes
if git rev-parse --abbrev-ref HEAD | grep -q ^main$ || git diff --name-only "origin/$GITHUB_BASE_REF" | grep -E "$1" ; then
# Determine the base ref or fallback to HEAD~1 when running on main
if [[ -z "${GITHUB_BASE_REF:-}" ]]; then
echo "GITHUB_BASE_REF is empty, likely running on main branch. Using HEAD~1 for comparison."
base_ref="HEAD~1"
else
base_ref="origin/$GITHUB_BASE_REF"
fi

if git rev-parse --abbrev-ref HEAD | grep -q ^main$ ; then
echo "Relevant file changes detected!"
echo "check_result=0" >> "$GITHUB_OUTPUT"
exit 0
fi

# Run git diff and store output
diff_output=$(git diff --name-only "$base_ref" || exit 1) # Ensure git diff failures are caught

# Check for relevant file changes
if echo "$diff_output" | grep -E "$1" ; then
echo "Relevant file changes detected!"
echo "check_result=0" >> "$GITHUB_OUTPUT"
exit 0 # Relevant changes detected
exit 0
else
echo "No relevant changes found."
echo "check_result=2" >> "$GITHUB_OUTPUT"
Expand Down

0 comments on commit 8ea3b60

Please sign in to comment.