Fix conditional checking for PR testing (#21947)

* Fix conditional checking for PR testing

Closes #21946

* Update .github/actions/conditional/action.yml

Co-authored-by: Jon Koops <jonkoops@gmail.com>

---------

Co-authored-by: Jon Koops <jonkoops@gmail.com>
This commit is contained in:
Stian Thorgersen 2023-07-25 15:43:53 +02:00 committed by GitHub
parent bb8ba1af5a
commit 8848dfed74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 25 deletions

View file

@ -1,6 +1,11 @@
name: Changed Files
description: Checks changes against target branch
inputs:
token:
description: GitHub Token
required: true
outputs:
ci:
description: Should "ci.yml" execute
@ -33,4 +38,6 @@ runs:
- id: changes
name: Find changes
shell: bash
run: .github/actions/conditional/conditional.sh origin ${{ github.base_ref }}
run: .github/actions/conditional/conditional.sh ${{ github.repository }} ${{ github.ref }}
env:
GITHUB_TOKEN: ${{ inputs.token }}

View file

@ -1,27 +1,22 @@
#!/bin/bash -e
REMOTE="$1"
BASE_REF="$2"
REPOSITORY="$1"
REF="$2"
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 [[ "$REF" =~ refs/pull/([0-9]+)/merge ]]; then
PR=$(echo $REF | cut -f 3 -d '/')
IS_PR=true
else
IS_PR=false
fi
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 "--------------------------------------------------------------------------------"
git fetch --depth 1 "$REMOTE" "$BASE_REF"
fi
# Get list of changes files
echo "========================================================================================"
echo "Changes compared to '$BASE_REF' in '$(git remote get-url "$REMOTE")'"
echo "Changes in PR: $PR"
echo "----------------------------------------------------------------------------------------"
CHANGED_FILES=$(git diff "$REMOTE/$BASE_REF" --name-only)
CHANGED_FILES=$(gh api -X GET --paginate repos/$REPOSITORY/pulls/$PR/files --jq .[].filename)
echo "$CHANGED_FILES"
fi
@ -45,10 +40,6 @@ for C in "${CONDITIONS[@]}"; do
# Convert pattern to regex
REGEX="$PATTERN"
#REGEX=$(echo "$PATTERN" | sed 's|\.|\\.|g' | sed 's|/$|/.*|g' | sed 's|^*|.*|g')
# Escape '/' characters
REGEX=$(echo "$REGEX" | sed 's|\/|\\/|g')
# Escape '.' to make it match the '.' character only
REGEX=$(echo "$REGEX" | sed 's|\.|\\.|g')
@ -60,8 +51,8 @@ for C in "${CONDITIONS[@]}"; do
REGEX=$(echo "$REGEX" | sed 's|/$|/.*|g')
# If no directory separators allow any directory structure before
if ( echo "$REGEX" | grep -v -E '\/' &>/dev/null ); then
REGEX="(.*\/)?$REGEX"
if ( echo "$REGEX" | grep -v -E '/' &>/dev/null ); then
REGEX="(.*/)?$REGEX"
fi
# Check if changed files matches regex
@ -98,7 +89,7 @@ do
echo "$JOB=${JOB_CONDITIONS[$JOB]}"
# Set output for GitHub job
if [ "$IS_GITHUB_ACTIONS" == true ]; then
echo "$JOB=${JOB_CONDITIONS[$JOB]}" >> $GITHUB_OUTPUT
if [ "$GITHUB_OUTPUT" != "" ]; then
echo "$JOB=${JOB_CONDITIONS[$JOB]}" >> $GITHUB_OUTPUT
fi
done

View file

@ -35,6 +35,8 @@ jobs:
- id: conditional
uses: ./.github/actions/conditional
with:
token: ${{ secrets.GITHUB_TOKEN }}
build:
name: Build

View file

@ -31,6 +31,8 @@ jobs:
- id: conditional
uses: ./.github/actions/conditional
with:
token: ${{ secrets.GITHUB_TOKEN }}
java:
name: CodeQL Java

View file

@ -33,6 +33,8 @@ jobs:
- id: conditional
uses: ./.github/actions/conditional
with:
token: ${{ secrets.GITHUB_TOKEN }}
build:
name: Build

View file

@ -34,6 +34,8 @@ jobs:
- id: conditional
uses: ./.github/actions/conditional
with:
token: ${{ secrets.GITHUB_TOKEN }}
build:
name: Build

View file

@ -28,6 +28,8 @@ jobs:
- id: conditional
uses: ./.github/actions/conditional
with:
token: ${{ secrets.GITHUB_TOKEN }}
build-keycloak:
name: Build Keycloak

View file

@ -35,6 +35,8 @@ jobs:
- id: conditional
uses: ./.github/actions/conditional
with:
token: ${{ secrets.GITHUB_TOKEN }}
build:
name: Build distribution