Update conditional workflows (#16728)

* Update conditional workflows

* Second versions to make it more readable
This commit is contained in:
Stian Thorgersen 2023-01-31 16:27:33 +01:00 committed by GitHub
parent 20a7a5acdb
commit 88a249ea0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 40 deletions

View file

@ -1,54 +1,86 @@
#!/bin/bash -e
REMOTE=$1
BASE_REF=$2
REMOTE="$1"
BASE_REF="$2"
if [ "$BASE_REF" != "" ]; then
if [ "$GITHUB_OUTPUT" != "" ]; then
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")'"
echo "--------------------------------------------------------------------------------"
echo "Fetching '$BASE_REF' in '`git remote get-url $REMOTE`'"
echo "--------------------------------------------------------------------------------"
git fetch --depth 1 $REMOTE $BASE_REF
git fetch --depth 1 "$REMOTE" "$BASE_REF"
fi
echo "--------------------------------------------------------------------------------"
echo "Changes compared to '$BASE_REF' in '`git remote get-url $REMOTE`'"
echo "--------------------------------------------------------------------------------"
git diff $REMOTE/$BASE_REF --name-only
# 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 "----------------------------------------------------------------------------------------"
else
echo "--------------------------------------------------------------------------------"
echo "Not a pull request, marking everything as changed"
fi
echo "--------------------------------------------------------------------------------"
echo "Run conditions"
echo "--------------------------------------------------------------------------------"
declare -A JOB_CONDITIONS
cat .github/actions/conditional/conditions | grep '=' | grep -v '#' | while read c; do
KEY=`echo $c | cut -d '=' -f 1`
PATTERN=`echo $c | cut -d '=' -f 2`
readarray -t CONDITIONS <<< "$(cat "$CONDITIONS_FILE" | grep -v '^[ ]*#' | grep -v '^[ ]*$')"
if [ "$BASE_REF" != "" ]; then
DIFF=`echo $PATTERN | xargs git diff $REMOTE/$BASE_REF --name-only`
if [ "$DIFF" != "" ]; then
CHANGED=true
for C in "${CONDITIONS[@]}"; do
read -r -a CONDITION <<< "$C"
if [ "$IS_PR" == true ]; then
PATTERN="${CONDITION[0]}"
# 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"
else
CHANGED=false
RUN_JOB=false
echo " $REGEX"
fi
else
CHANGED=true
# Always run job if not a PR
RUN_JOB=true
fi
# Temporarily always run ci and operator as there's some issues with conditions
if [ "$KEY" == "ci" ] || [ "$KEY" == "operator" ]; then
CHANGED=true
fi
# Set what jobs should run for the regex
for ((i = 1; i < ${#CONDITION[@]}; i++)); do
JOB=${CONDITION[$i]}
echo "$KEY=$CHANGED"
if [ "$GITHUB_OUTPUT" != "" ]; then
echo "$KEY=$CHANGED" >> $GITHUB_OUTPUT
# If already set to run, ignore
if [ "${JOB_CONDITIONS[$JOB]}" != true ]; then
JOB_CONDITIONS[$JOB]=$RUN_JOB
fi
done
done
echo "--------------------------------------------------------------------------------"
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
fi
done

View file

@ -1,12 +1,16 @@
# File patterns used to decide what workflows/jobs to execute for a given PR
#
# To test a pattern run '.github/actions/conditional/conditional.sh <remote name> <branch>'
# Alternatively, run 'git diff <remote name>/<branch> <pattern>' (from the root directory)
ci=*/pom.xml */src/main/ */src/test/ .github/actions/ .github/workflows/
.github/actions/ ci operator codeql-java codeql-themes codeql-js_adapter
operator=*/pom.xml */src/main/ */src/test/ .github/actions/ .github/workflows/
.github/workflows/ci.yml ci
.github/workflows/operator-ci.yml operator
.github/workflows/codeql-analysis.yml codeql-java codeql-themes codeql-js_adapter
codeql-java=*.java .github/actions/ .github/workflows/codeql-analysis.yml
codeql-themes=themes/ .github/actions/ .github/workflows/codeql-analysis.yml
codeql-js_adapter=adapters/oidc/js/ .github/actions/ .github/workflows/codeql-analysis.yml
*/src/main/ ci operator
*/src/test/ ci operator
*.java codeql-java
themes/ codeql-themes
adapters/oidc/js/ codeql-js_adapter