Skip to content

Commit 96c3a34

Browse files
committed
CI: only build and push images if they changed
1 parent b04a6dc commit 96c3a34

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

.github/workflows/reusable-build-and-push.yml

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ jobs:
121121
repo-owner: ${{ needs.build-info.outputs.repo-owner }}
122122
steps:
123123
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
124+
with:
125+
fetch-depth: 100
124126

125127
- run: |
126128
echo "inputs:"
@@ -157,13 +159,52 @@ jobs:
157159
username: ${{ vars.DOCKER_HUB_USERNAME || env.repo-owner }}
158160
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
159161

162+
- name: find latest commit for image tag
163+
id: get-latest-commit
164+
run: |
165+
set -x
166+
context=$( echo '${{ toJSON(matrix.build-config) }}' | jq -r '.context' | sed "s|$PWD|.|g")
167+
echo "context: $context"
168+
dockerfile=$( echo '${{ toJSON(matrix.build-config) }}' | jq -r '.dockerfile' | sed "s|$PWD|.|g")
169+
echo "dockerfile: $dockerfile"
170+
git log --stat -n 1 $context $dockerfile
171+
latest_commit=$(git log --pretty=format:"%H" -n 1 $context $dockerfile)
172+
echo "latest_commit: $latest_commit"
173+
echo "result=$latest_commit" | tr -d "\n" >> $GITHUB_OUTPUT
174+
175+
- name: check if image already exists
176+
id: check-image
177+
run: |
178+
set +e
179+
docker pull ${{ matrix.build-config.image }}:${{ steps.get-latest-commit.outputs.result }}
180+
image_exists=$?
181+
set -e
182+
if ([ $image_exists -eq 0 ]); then
183+
echo "image already exists"
184+
echo "result=true" | tr -d "\n" >> $GITHUB_OUTPUT
185+
else
186+
echo "image does not exist"
187+
echo "result=false" | tr -d "\n" >> $GITHUB_OUTPUT
188+
fi
189+
190+
- name: Add latest commit to image tag if image does not exist
191+
id: add-latest-commit-to-tags-if-image-does-not-exist
192+
uses: actions/github-script@v7
193+
with:
194+
script: |
195+
if (!${{ steps.check-image.outputs.result }}) {
196+
return [...JSON.parse('${{ env.tags }}'), '${{ steps.get-latest-commit.outputs.result }}']
197+
}
198+
return JSON.parse('${{ env.tags }}')
199+
160200
- uses: actions/github-script@v7
161201
id: expand-tags
162202
with:
163203
script: |
164-
return JSON.parse('${{ env.tags }}').map(tag => `${{ matrix.build-config.image }}:${ tag }`)
204+
return JSON.parse('${{ steps.add-latest-commit-to-tags-if-image-does-not-exist.outputs.result }}').map(tag => `${{ matrix.build-config.image }}:${ tag }`)
165205
166206
- name: populate build args from secrets/vars
207+
if: steps.check-image.outputs.result == 'false'
167208
id: populate-build-args
168209
uses: actions/github-script@v7
169210
with:
@@ -185,6 +226,7 @@ jobs:
185226
return args
186227
187228
- name: transform build args
229+
if: steps.check-image.outputs.result == 'false'
188230
id: transform-build-args
189231
run: |
190232
set -x
@@ -193,13 +235,15 @@ jobs:
193235
echo 'EOF' >> $GITHUB_OUTPUT
194236
cat $GITHUB_OUTPUT
195237
196-
- name: debug output
238+
- name: debug transform build args output
239+
if: steps.check-image.outputs.result == 'false'
197240
run: |
198241
echo "result: ${{ steps.transform-build-args.outputs.result }}"
199242
echo "result: ${{ toJSON(steps.transform-build-args.outputs) }}"
200243
201244
- name: Build and push image
202245
uses: docker/build-push-action@v6
246+
if: steps.check-image.outputs.result == 'false'
203247
with:
204248
push: true
205249
file: ${{ matrix.build-config.dockerfile }}
@@ -209,3 +253,11 @@ jobs:
209253
${{ steps.transform-build-args.outputs.result }}
210254
cache-from: type=gha,scope=${{ matrix.build-config.image }}
211255
cache-to: type=gha,scope=${{ matrix.build-config.image }},mode=max
256+
257+
- name: Retag and push images
258+
if: steps.check-image.outputs.result == 'true'
259+
run: |
260+
for tag in $(echo '${{ env.tags }}' | jq -r '.[]'); do
261+
docker tag ${{ matrix.build-config.image }}:${{ steps.get-latest-commit.outputs.result }} ${{ matrix.build-config.image }}:${tag}
262+
docker push ${{ matrix.build-config.image }}:${tag}
263+
done

0 commit comments

Comments
 (0)