From 46e6c31648227cf1c005f76b201d8f52f660e48f Mon Sep 17 00:00:00 2001 From: Nick Boldt Date: Mon, 29 Jan 2024 13:02:04 -0400 Subject: [PATCH] chore: only generate PR previews and next... (#161) * chore: only generate PR previews and next builds for paths listed in the GH action (exclude changes to doc, etc.) Signed-off-by: Nick Boldt * indent Signed-off-by: Nick Boldt * use a check-changes stage to set an env.CHANGES with either a list of changed files or a nullstring; if null, don't build anything Signed-off-by: Nick Boldt * run 'PR Publish' stage for all PRs, but if no changes, skip the subsequent setup/build/publish stages Signed-off-by: Nick Boldt --------- Signed-off-by: Nick Boldt --- .github/workflows/next-container-build.yaml | 21 ++++++++++++-- .github/workflows/pr-container-build.yaml | 31 ++++++++++++++++++--- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/.github/workflows/next-container-build.yaml b/.github/workflows/next-container-build.yaml index e991c3a4..4d8506db 100644 --- a/.github/workflows/next-container-build.yaml +++ b/.github/workflows/next-container-build.yaml @@ -18,9 +18,9 @@ name: Build and push operator, bundle, and catalog images on: push: branches: - - main - - rhdh-1.[0-9]+ - - 1.[0-9]+.x + - main + - rhdh-1.[0-9]+ + - 1.[0-9]+.x concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -30,9 +30,24 @@ env: REGISTRY: quay.io jobs: + check-changes: + # check if the change for this PR necessitates a rebuild of containers + runs-on: ubuntu-latest + steps: + - name: check-changes + # check changes in this commit for regex include and exclude matches; pipe to an env var + run: | + CHANGES="$(git diff --name-only | \ + grep -E "/docker/|\.dockerignore|workflows/.+-container-build.yaml|Makefile|/bundle/|/config/|go.mod|go.sum|.+\.go" | \ + grep -v -E ".+_test.go|/.rhdh/")" \ + >> $GITHUB_ENV + next-build: name: Next build runs-on: ubuntu-latest + needs: check-changes + # run this stage only if there are changes that match the includes and not the excludes + if: ${{ env.CHANGES != '' }} permissions: contents: read packages: write diff --git a/.github/workflows/pr-container-build.yaml b/.github/workflows/pr-container-build.yaml index 0e1f4afc..327cbbec 100644 --- a/.github/workflows/pr-container-build.yaml +++ b/.github/workflows/pr-container-build.yaml @@ -23,9 +23,9 @@ on: pull_request_target: types: [opened, synchronize, reopened, ready_for_review] branches: - - main - - rhdh-1.[0-9]+ - - 1.[0-9]+.x + - main + - rhdh-1.[0-9]+ + - 1.[0-9]+.x concurrency: group: ${{ github.workflow }}-${{ github.event.number || github.event.pull_request.head.ref }} @@ -48,10 +48,23 @@ jobs: run: | echo "✓" + check-changes: + # check if the change for this PR necessitates a rebuild of containers + runs-on: ubuntu-latest + needs: authorize + steps: + - name: check-changes + # check changes in this commit for regex include and exclude matches; pipe to an env var + run: | + CHANGES="$(git diff --name-only | \ + grep -E "/docker/|\.dockerignore|workflows/.+-container-build.yaml|Makefile|/bundle/|/config/|go.mod|go.sum|.+\.go" | \ + grep -v -E ".+_test.go|/.rhdh/")" \ + >> $GITHUB_ENV + pr-build: name: PR Publish runs-on: ubuntu-latest - needs: authorize + needs: [authorize, check-changes] permissions: contents: read packages: write @@ -66,11 +79,15 @@ jobs: repository: ${{ github.event.pull_request.head.repo.full_name }} - name: Setup Go + # run this stage only if there are changes that match the includes and not the excludes + if: ${{ env.CHANGES != '' }} uses: actions/setup-go@v4 with: go-version-file: 'go.mod' - name: Get the last commit short SHA of the PR + # run this stage only if there are changes that match the includes and not the excludes + if: ${{ env.CHANGES != '' }} run: | SHORT_SHA=$(git rev-parse --short ${{ github.event.pull_request.head.sha }}) echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_ENV @@ -78,6 +95,8 @@ jobs: echo "BASE_VERSION=$BASE_VERSION" >> $GITHUB_ENV - name: Login to quay.io + # run this stage only if there are changes that match the includes and not the excludes + if: ${{ env.CHANGES != '' }} uses: docker/login-action@v2 with: registry: ${{ env.REGISTRY }} @@ -85,6 +104,8 @@ jobs: password: ${{ secrets.QUAY_TOKEN }} - name: Build and push operator, bundle, and catalog images + # run this stage only if there are changes that match the includes and not the excludes + if: ${{ env.CHANGES != '' }} run: | # install skopeo, podman sudo apt-get -y update; sudo apt-get -y install skopeo podman @@ -104,6 +125,8 @@ jobs: skopeo --insecure-policy copy --all docker://quay.io/janus-idp/${image}:${VERSION} docker://quay.io/janus-idp/${image}:${VERSION%-*} done - name: Comment image links in PR + # run this stage only if there are changes that match the includes and not the excludes + if: ${{ env.CHANGES != '' }} uses: actions/github-script@v6 with: script: |