2023-01-30 07:07:10 +00:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
REMOTE="$1"
|
|
|
|
BASE_REF="$2"
|
2023-01-30 07:07:10 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
CONDITIONS_FILE=".github/actions/conditional/conditions"
|
|
|
|
|
|
|
|
[ "$BASE_REF" != "" ] && IS_PR=true || IS_PR=false
|
|
|
|
[ "$GITHUB_OUTPUT" != "" ] && IS_GITHUB_ACTIONS=true || IS_GITHUB_ACTIONS=false
|
|
|
|
|
|
|
|
if [ "$IS_PR" == true ]; then
|
|
|
|
# Fetch remote if running on GitHub Actions
|
|
|
|
if [ "$IS_GITHUB_ACTIONS" == true ]; then
|
|
|
|
echo "========================================================================================"
|
|
|
|
echo "Fetching '$BASE_REF' in '$(git remote get-url "$REMOTE")'"
|
2023-01-30 07:07:10 +00:00
|
|
|
echo "--------------------------------------------------------------------------------"
|
2023-01-31 15:27:33 +00:00
|
|
|
git fetch --depth 1 "$REMOTE" "$BASE_REF"
|
2023-01-30 07:07:10 +00:00
|
|
|
fi
|
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
# Get list of changes files
|
|
|
|
echo "========================================================================================"
|
|
|
|
echo "Changes compared to '$BASE_REF' in '$(git remote get-url "$REMOTE")'"
|
|
|
|
echo "----------------------------------------------------------------------------------------"
|
|
|
|
CHANGED_FILES=$(git diff "$REMOTE/$BASE_REF" --name-only)
|
|
|
|
echo "$CHANGED_FILES"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "========================================================================================"
|
|
|
|
if [ "$IS_PR" == true ]; then
|
|
|
|
echo "Matching regex"
|
|
|
|
echo "----------------------------------------------------------------------------------------"
|
2023-01-30 07:07:10 +00:00
|
|
|
else
|
|
|
|
echo "Not a pull request, marking everything as changed"
|
|
|
|
fi
|
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
declare -A JOB_CONDITIONS
|
|
|
|
|
|
|
|
readarray -t CONDITIONS <<< "$(cat "$CONDITIONS_FILE" | grep -v '^[ ]*#' | grep -v '^[ ]*$')"
|
|
|
|
|
|
|
|
for C in "${CONDITIONS[@]}"; do
|
|
|
|
read -r -a CONDITION <<< "$C"
|
2023-01-30 07:07:10 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
if [ "$IS_PR" == true ]; then
|
|
|
|
PATTERN="${CONDITION[0]}"
|
2023-01-30 07:07:10 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
# Convert pattern to regex
|
|
|
|
REGEX=$(echo "$PATTERN" | sed 's|\.|[.]|g' | sed 's|/$|/.*|g' | sed 's|^*|.*|g')
|
|
|
|
|
|
|
|
# Check if changed files matches regex
|
|
|
|
if ( echo "$CHANGED_FILES" | grep -q "^$REGEX$"); then
|
|
|
|
RUN_JOB=true
|
|
|
|
echo "* $REGEX"
|
2023-01-30 07:07:10 +00:00
|
|
|
else
|
2023-01-31 15:27:33 +00:00
|
|
|
RUN_JOB=false
|
|
|
|
echo " $REGEX"
|
2023-01-30 07:07:10 +00:00
|
|
|
fi
|
|
|
|
else
|
2023-01-31 15:27:33 +00:00
|
|
|
# Always run job if not a PR
|
|
|
|
RUN_JOB=true
|
2023-01-30 07:07:10 +00:00
|
|
|
fi
|
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
# Set what jobs should run for the regex
|
|
|
|
for ((i = 1; i < ${#CONDITION[@]}; i++)); do
|
|
|
|
JOB=${CONDITION[$i]}
|
2023-01-31 07:57:32 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
# If already set to run, ignore
|
|
|
|
if [ "${JOB_CONDITIONS[$JOB]}" != true ]; then
|
|
|
|
JOB_CONDITIONS[$JOB]=$RUN_JOB
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2023-01-30 07:07:10 +00:00
|
|
|
|
2023-01-31 15:27:33 +00:00
|
|
|
echo "========================================================================================"
|
|
|
|
echo "Run workflows/jobs"
|
|
|
|
echo "----------------------------------------------------------------------------------------"
|
|
|
|
|
|
|
|
# List all jobs and if they should run or not
|
|
|
|
for JOB in "${!JOB_CONDITIONS[@]}"
|
|
|
|
do
|
|
|
|
echo "$JOB=${JOB_CONDITIONS[$JOB]}"
|
|
|
|
|
|
|
|
# Set output for GitHub job
|
|
|
|
if [ "$IS_GITHUB_ACTIONS" == true ]; then
|
|
|
|
echo "$JOB=${JOB_CONDITIONS[$JOB]}" >> $GITHUB_OUTPUT
|
2023-01-30 07:07:10 +00:00
|
|
|
fi
|
|
|
|
done
|