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 name: Changed Files
description: Checks changes against target branch description: Checks changes against target branch
inputs:
token:
description: GitHub Token
required: true
outputs: outputs:
ci: ci:
description: Should "ci.yml" execute description: Should "ci.yml" execute
@ -33,4 +38,6 @@ runs:
- id: changes - id: changes
name: Find changes name: Find changes
shell: bash 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 #!/bin/bash -e
REMOTE="$1" REPOSITORY="$1"
BASE_REF="$2" REF="$2"
CONDITIONS_FILE=".github/actions/conditional/conditions" CONDITIONS_FILE=".github/actions/conditional/conditions"
[ "$BASE_REF" != "" ] && IS_PR=true || IS_PR=false if [[ "$REF" =~ refs/pull/([0-9]+)/merge ]]; then
[ "$GITHUB_OUTPUT" != "" ] && IS_GITHUB_ACTIONS=true || IS_GITHUB_ACTIONS=false PR=$(echo $REF | cut -f 3 -d '/')
IS_PR=true
else
IS_PR=false
fi
if [ "$IS_PR" == true ]; then if [ "$IS_PR" == true ]; then
# Fetch remote if running on GitHub Actions
if [ "$IS_GITHUB_ACTIONS" == true ]; then
echo "========================================================================================" echo "========================================================================================"
echo "Fetching '$BASE_REF' in '$(git remote get-url "$REMOTE")'" echo "Changes in PR: $PR"
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 "----------------------------------------------------------------------------------------" 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" echo "$CHANGED_FILES"
fi fi
@ -45,10 +40,6 @@ for C in "${CONDITIONS[@]}"; do
# Convert pattern to regex # Convert pattern to regex
REGEX="$PATTERN" 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 # Escape '.' to make it match the '.' character only
REGEX=$(echo "$REGEX" | sed 's|\.|\\.|g') REGEX=$(echo "$REGEX" | sed 's|\.|\\.|g')
@ -60,8 +51,8 @@ for C in "${CONDITIONS[@]}"; do
REGEX=$(echo "$REGEX" | sed 's|/$|/.*|g') REGEX=$(echo "$REGEX" | sed 's|/$|/.*|g')
# If no directory separators allow any directory structure before # If no directory separators allow any directory structure before
if ( echo "$REGEX" | grep -v -E '\/' &>/dev/null ); then if ( echo "$REGEX" | grep -v -E '/' &>/dev/null ); then
REGEX="(.*\/)?$REGEX" REGEX="(.*/)?$REGEX"
fi fi
# Check if changed files matches regex # Check if changed files matches regex
@ -98,7 +89,7 @@ do
echo "$JOB=${JOB_CONDITIONS[$JOB]}" echo "$JOB=${JOB_CONDITIONS[$JOB]}"
# Set output for GitHub job # Set output for GitHub job
if [ "$IS_GITHUB_ACTIONS" == true ]; then if [ "$GITHUB_OUTPUT" != "" ]; then
echo "$JOB=${JOB_CONDITIONS[$JOB]}" >> $GITHUB_OUTPUT echo "$JOB=${JOB_CONDITIONS[$JOB]}" >> $GITHUB_OUTPUT
fi fi
done done

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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