Skip to content

Commit 66d6b56

Browse files
Add check to skip push if image already exists
1 parent 768778c commit 66d6b56

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

.github/workflows/docker-publish.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,30 @@ jobs:
4545
type=sha,prefix={{branch}}-,format=short
4646
type=semver,pattern={{version}}
4747
48+
- name: Check if image exists
49+
id: check
50+
if: github.event_name != 'pull_request'
51+
continue-on-error: true
52+
run: |
53+
TAGS="${{ steps.meta.outputs.tags }}"
54+
IMAGE_EXISTS=false
55+
56+
for TAG in $TAGS; do
57+
echo "Checking if image exists: $TAG"
58+
if docker manifest inspect $TAG > /dev/null 2>&1; then
59+
echo "Image already exists: $TAG"
60+
IMAGE_EXISTS=true
61+
else
62+
echo "Image does not exist: $TAG"
63+
IMAGE_EXISTS=false
64+
break
65+
fi
66+
done
67+
68+
echo "should_push=$([[ "$IMAGE_EXISTS" == "false" ]] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
69+
4870
- name: Build and push Docker image
71+
if: github.event_name == 'pull_request' || steps.check.outputs.should_push == 'true'
4972
uses: docker/build-push-action@v6
5073
with:
5174
context: .
@@ -55,3 +78,8 @@ jobs:
5578
cache-from: type=gha
5679
cache-to: type=gha,mode=max
5780
platforms: linux/amd64,linux/arm64
81+
82+
- name: Image push skipped
83+
if: github.event_name != 'pull_request' && steps.check.outputs.should_push != 'true'
84+
run: |
85+
echo "All image tags already exist in registry. Skipping build and push."

0 commit comments

Comments
 (0)