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