From ee3bde54e0dddba16c70603038ed919cacb92300 Mon Sep 17 00:00:00 2001 From: Gustavo Lira e Silva Date: Tue, 14 Jan 2025 12:54:09 -0300 Subject: [PATCH] Skip PR build for changes limited to e2e folder or existing image (#2135) Add steps to detect PRs with changes only in the `e2e/` folder and skip the build in such cases. Additionally, check if the image already exists in Quay.io and bypass the build if it does. This reduces unnecessary builds, improving efficiency in the CI workflow. --- .github/workflows/pr-build-image.yaml | 38 ++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-build-image.yaml b/.github/workflows/pr-build-image.yaml index 44578c7c56..7c1f1378a5 100644 --- a/.github/workflows/pr-build-image.yaml +++ b/.github/workflows/pr-build-image.yaml @@ -52,7 +52,26 @@ jobs: git fetch base-origin ${{ github.event.pull_request.base.ref }} git merge --no-edit base-origin/${{ github.event.pull_request.base.ref }} + - name: Determine Changed Files + id: changes + run: | + BASE_COMMIT=${{ github.event.pull_request.base.sha }} + HEAD_COMMIT=${{ github.event.pull_request.head.sha }} + + CHANGED_FILES=$(git diff --name-only "$BASE_COMMIT" "$HEAD_COMMIT") + + echo "Changed files:" + echo "$CHANGED_FILES" + + if echo "$CHANGED_FILES" | grep -qv '^e2e-tests/'; then + echo "Changes detected outside the e2e folder. Proceeding with the build." + echo "proceed_with_build=true" >> $GITHUB_ENV + else + echo "No changes outside the e2e folder. Skipping the build." + echo "proceed_with_build=false" >> $GITHUB_ENV + - name: Get the last commit short SHA of the PR + if: env.proceed_with_build == 'true' run: | SHORT_SHA=$(git rev-parse --short=8 ${{ github.event.pull_request.head.sha }}) echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_ENV @@ -63,7 +82,24 @@ jobs: -e 's|("Last Commit:.+)|"Last Commit: '$repoPR' @ '$SHORT_SHA'"|' fi + - name: Check if Image Already Exists + if: env.proceed_with_build == 'true' + run: | + IMAGE_TAG="pr-${{ github.event.number }}" + IMAGE_NAME="${{ env.REGISTRY }}/janus-idp/backstage-showcase:${IMAGE_TAG}" + + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "https://quay.io/v2/janus-idp/backstage-showcase/manifests/${IMAGE_TAG}") + + if [ "$HTTP_CODE" -eq 200 ]; then + echo "Image $IMAGE_NAME already exists. Skipping the build." + echo "image_exists=true" >> $GITHUB_ENV + else + echo "Image $IMAGE_NAME does not exist. Proceeding with the build." + echo "image_exists=false" >> $GITHUB_ENV + fi + - name: Build and Push with Buildx + if: env.proceed_with_build == 'true' && env.image_exists == 'false' uses: ./.github/actions/docker-build with: registry: ${{ env.REGISTRY }} @@ -73,11 +109,11 @@ jobs: imageTags: | type=ref,prefix=pr-,event=pr type=ref,prefix=pr-,suffix=-${{ env.SHORT_SHA }},event=pr - # to autodelete PR image tags, set an expiry date imageLabels: quay.expires-after=14d push: true - name: Comment the image pull link + if: env.proceed_with_build == 'true' && env.image_exists == 'false' uses: actions/github-script@v7 with: script: |