Skip to content

Commit 5f6b520

Browse files
committed
Detect Dockerfile changes to decide when to rebuild all versions
On PRs and pushes, only build versions that don't exist in GHCR yet. If the Dockerfile itself changed, rebuild all versions to validate (without pushing on PRs).
1 parent 9578f34 commit 5f6b520

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

.github/workflows/ruby.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
pull_request:
1010
paths:
1111
- ruby/Dockerfile
12+
- .github/workflows/ruby.yml
1213
workflow_dispatch:
1314
inputs:
1415
force:
@@ -41,6 +42,20 @@ jobs:
4142

4243
- uses: docker/setup-buildx-action@v4
4344

45+
- name: Check if Dockerfile changed
46+
id: dockerfile
47+
run: |
48+
if [ "${{ github.event_name }}" = "pull_request" ]; then
49+
BASE="${{ github.event.pull_request.base.sha }}"
50+
else
51+
BASE="${{ github.event.before }}"
52+
fi
53+
if git diff --name-only "$BASE" "${{ github.sha }}" -- ruby/Dockerfile | grep -q .; then
54+
echo "changed=true" >> "$GITHUB_OUTPUT"
55+
else
56+
echo "changed=false" >> "$GITHUB_OUTPUT"
57+
fi
58+
4459
- name: Check if image exists
4560
id: check
4661
run: |
@@ -51,14 +66,14 @@ jobs:
5166
fi
5267
5368
- uses: docker/login-action@v4
54-
if: github.event_name != 'pull_request' && (steps.check.outputs.exists != 'true' || (github.event_name == 'workflow_dispatch' && inputs.force))
69+
if: github.event_name != 'pull_request' && (steps.check.outputs.exists != 'true' || steps.dockerfile.outputs.changed == 'true' || (github.event_name == 'workflow_dispatch' && inputs.force))
5570
with:
5671
registry: ${{ env.REGISTRY }}
5772
username: ${{ github.actor }}
5873
password: ${{ secrets.GITHUB_TOKEN }}
5974

6075
- uses: docker/build-push-action@v7
61-
if: github.event_name == 'pull_request' || steps.check.outputs.exists != 'true' || (github.event_name == 'workflow_dispatch' && inputs.force)
76+
if: steps.check.outputs.exists != 'true' || steps.dockerfile.outputs.changed == 'true' || (github.event_name == 'workflow_dispatch' && inputs.force)
6277
with:
6378
context: ruby
6479
platforms: linux/amd64,linux/arm64

0 commit comments

Comments
 (0)