Skip to content

Commit 3315e6d

Browse files
authored
fix: build-push-docker layer caching (#1484)
* test: integ test docker cache * fix * rework some things, testing * try mode=min * test: only create cache on push/schedule events * chore: prep for PR * fix: cache defaulting
1 parent e774533 commit 3315e6d

4 files changed

Lines changed: 53 additions & 32 deletions

File tree

.changeset/few-lobsters-attend.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"build-push-docker": minor
3+
---
4+
5+
fix and rework docker caching logic. cache restores are now disabled by default,
6+
and cache-to/cache-from inputs are no longer post-processed

.changeset/lucky-pianos-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ctf-build-image": minor
3+
---
4+
5+
fix: rework docker caching setup

actions/build-push-docker/action.yml

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ inputs:
1919
See `--no-cache` flag in:
2020
https://docs.docker.com/engine/reference/commandline/buildx_build/#cache
2121
required: false
22-
default: "true"
22+
default: "false"
2323
docker-save-cache:
2424
description: |
2525
Whether to save the Docker build cache after the build. If set to `false`,
@@ -30,21 +30,13 @@ inputs:
3030
# See: https://github.com/moby/buildkit#github-actions-cache-experimental
3131
docker-build-cache-from:
3232
description: |
33-
Source of Docker build cache.
34-
35-
",scope=buildkit-<runner arch>" is appended to this input in order to set
36-
caching for the specific runner architecture.
33+
Source of Docker build cache. Defaulted below.
3734
required: false
38-
default: "type=gha,timeout=10m"
3935
# See: https://github.com/moby/buildkit#github-actions-cache-experimental
4036
docker-build-cache-to:
4137
description: |
42-
Destination of Docker build cache.
43-
44-
",scope=buildkit-<runner arch>" is appended to this input in order to set
45-
caching for the specific runner architecture.
38+
Destination of Docker build cache. Defaulted below.
4639
required: false
47-
default: "type=gha,timeout=10m,mode=max,ignore-error=true"
4840
docker-push:
4941
description: "Push the docker image. Build only (no push) if: false."
5042
required: false
@@ -317,29 +309,38 @@ runs:
317309
env:
318310
DOCKER_RESTORE_CACHE: ${{ inputs.docker-restore-cache }}
319311
DOCKER_SAVE_CACHE: ${{ inputs.docker-save-cache }}
320-
DOCKER_BUILD_CACHE_FROM: >-
321-
${{
322-
format('{0},scope={1}', inputs.docker-build-cache-from, runner.arch)
323-
}}
324-
DOCKER_BUILD_CACHE_TO: >-
325-
${{
326-
format('{0},scope={1}', inputs.docker-build-cache-to, runner.arch)
327-
}}
312+
DOCKER_BUILD_CACHE_FROM: ${{ inputs.docker-build-cache-from }}
313+
DOCKER_BUILD_CACHE_TO: ${{ inputs.docker-build-cache-to }}
314+
DEFAULT_DOCKER_BUILD_CACHE_FROM:
315+
type=gha,timeout=10m,scope=generic-${{ runner.os }}-${{ runner.arch }}
316+
DEFAULT_DOCKER_BUILD_CACHE_TO:
317+
type=gha,timeout=10m,mode=min,ignore-error=true,scope=generic-${{
318+
runner.os }}-${{ runner.arch }}
328319
run: |
329-
if [[ "${DOCKER_RESTORE_CACHE}" == "true" ]]; then
330-
echo "no-cache=false" | tee -a "${GITHUB_OUTPUT}"
331-
echo "cache-from=${DOCKER_BUILD_CACHE_FROM}" | tee -a "${GITHUB_OUTPUT}"
332-
else
320+
CACHE_TO="${DOCKER_BUILD_CACHE_TO}"
321+
if [[ "${DOCKER_SAVE_CACHE}" == "true" ]]; then
322+
if [[ -z "${CACHE_TO}" ]]; then
323+
CACHE_TO="${DEFAULT_DOCKER_BUILD_CACHE_TO}"
324+
fi
325+
echo "cache-to=${CACHE_TO}" | tee -a "${GITHUB_OUTPUT}"
326+
fi
327+
328+
if [[ "${DOCKER_RESTORE_CACHE}" == "false" ]]; then
333329
echo "no-cache=true" | tee -a "${GITHUB_OUTPUT}"
330+
exit 0
334331
fi
335332
336-
if [[ "${DOCKER_SAVE_CACHE}" == "true" ]]; then
337-
echo "cache-to=${DOCKER_BUILD_CACHE_TO}" | tee -a "${GITHUB_OUTPUT}"
333+
CACHE_FROM="${DOCKER_BUILD_CACHE_FROM}"
334+
if [[ -z "${CACHE_FROM}" ]]; then
335+
CACHE_FROM="${DEFAULT_DOCKER_BUILD_CACHE_FROM}"
338336
fi
339337
338+
echo "no-cache=false" | tee -a "${GITHUB_OUTPUT}"
339+
echo "cache-from=${CACHE_FROM}" | tee -a "${GITHUB_OUTPUT}"
340+
340341
- name: Build & push image
341342
id: build-image
342-
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6.13.0
343+
uses: docker/build-push-action@d08e5c354a6adb9ed34480a06d141179aa583294 # v7.0.0
343344
env:
344345
DOCKER_BUILD_CHECKS_ANNOTATIONS: true
345346
DOCKER_BUILD_SUMMARY: true
@@ -359,9 +360,10 @@ runs:
359360
tags: ${{ steps.docker-meta.outputs.tags }}
360361
labels: ${{ steps.docker-meta.outputs.labels }}
361362
platforms: ${{ inputs.platform }}
363+
# disables cache when building image
362364
no-cache: ${{ steps.docker-cache.outputs.no-cache }}
363-
cache-from: ${{ steps.docker-cache.outputs.docker-cache-from }}
364-
cache-to: ${{ steps.docker-cache.outputs.docker-cache-to }}
365+
cache-from: ${{ steps.docker-cache.outputs.cache-from }}
366+
cache-to: ${{ steps.docker-cache.outputs.cache-to }}
365367
secrets: |
366368
GIT_AUTH_TOKEN=${{ inputs.github-token || ''}}
367369

actions/ctf-build-image/action.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,19 @@ runs:
181181
docker-attestations: "false"
182182
docker-registry-url: ${{ inputs.docker-registry-url }}
183183
docker-repository-name: ${{ inputs.docker-repository-name }}
184-
184+
# only save on events which are expected to be from the default branch
185+
docker-save-cache:
186+
${{ github.event_name == 'schedule' || github.event_name == 'push' }}
187+
# dont use cache on events which are expected to be from the default branch
188+
# this is to create a fresh cache/snapshot unpolluted by previous cache entries
185189
docker-restore-cache:
186-
${{ github.event_name == 'pull_request' || github.event_name ==
187-
'merge_group' }}
188-
docker-save-cache: ${{ github.event_name == 'push' }}
190+
${{ github.event_name != 'schedule' && github.event_name != 'push' }}
191+
docker-build-cache-to:
192+
"type=gha,timeout=10m,mode=min,ignore-error=true,scope=ctf-build-image-${{
193+
runner.os }}-${{ runner.arch }}"
194+
docker-build-cache-from:
195+
"type=gha,timeout=10m,scope=ctf-build-image-${{ runner.os }}-${{
196+
runner.arch }}"
189197

190198
tags: type=raw,value=${{ inputs.image-tag }}
191199
aws-account-number: ${{ inputs.aws-account-number }}

0 commit comments

Comments
 (0)