Skip to content

Commit c1219e6

Browse files
committed
🔧 Refactor Docker image build steps for better PR handling and caching
Signed-off-by: Phil Huang <phil.huang@microsoft.com>
1 parent c838843 commit c1219e6

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

.github/workflows/docker-publish.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,26 @@ jobs:
6969
with:
7070
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
7171

72-
# Build and push Docker image with Buildx (don't push on PR)
73-
# https://github.com/docker/build-push-action
74-
- name: Build and push Docker image
75-
id: build-and-push
72+
# Build Docker image for validation on PRs (no push)
73+
- name: Build Docker image (PR validation)
74+
if: github.event_name == 'pull_request'
7675
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
7776
with:
7877
context: .
79-
push: ${{ github.event_name != 'pull_request' }}
78+
push: false
79+
tags: ${{ steps.meta.outputs.tags }}
80+
labels: ${{ steps.meta.outputs.labels }}
81+
cache-from: type=gha
82+
cache-to: type=gha,mode=max
83+
84+
# Build and push Docker image to GHCR outside of PRs
85+
- name: Build and push Docker image to GHCR
86+
if: github.event_name != 'pull_request'
87+
id: push-to-ghcr
88+
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
89+
with:
90+
context: .
91+
push: true
8092
tags: ${{ steps.meta.outputs.tags }}
8193
labels: ${{ steps.meta.outputs.labels }}
8294
cache-from: type=gha
@@ -93,7 +105,7 @@ jobs:
93105
env:
94106
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
95107
TAGS: ${{ steps.meta.outputs.tags }}
96-
DIGEST: ${{ steps.build-and-push.outputs.digest }}
108+
DIGEST: ${{ steps.push-to-ghcr.outputs.digest }}
97109
# This step uses the identity token to provision an ephemeral certificate
98110
# against the sigstore community Fulcio instance.
99111
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}

0 commit comments

Comments
 (0)