Skip to content

Commit 330127d

Browse files
committed
fix(docker/build-image): enhance cache handling for GitHub Actions triggers
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 8e671ab commit 330127d

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

actions/docker/build-image/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ permissions:
134134
secret-envs: ""
135135

136136
# Cache type. Set to `false` or empty to disable cache entirely.
137+
# For `gha`, cache restore remains enabled on untrusted default-branch-scoped triggers,
138+
# but cache export is skipped automatically because GitHub only grants read-only cache access there.
137139
# See https://docs.docker.com/build/cache/backends.
138140
#
139141
# Default: `gha`
@@ -204,6 +206,7 @@ permissions:
204206
| **`secret-envs`** | List of secret environment variables to expose to the build (e.g., `key=envname, MY_SECRET=MY_ENV_VAR`). | **false** | - |
205207
| | See <https://docs.docker.com/build/ci/github-actions/secrets/>. | | |
206208
| **`cache-type`** | Cache type. Set to `false` or empty to disable cache entirely. | **false** | `gha` |
209+
| | For `gha`, cache restore remains enabled on untrusted default-branch-scoped triggers, but cache export is skipped automatically because GitHub only grants read-only cache access there. | | |
207210
| | See <https://docs.docker.com/build/cache/backends>. | | |
208211
| **`buildkitd-config-inline`** | Inline BuildKit daemon configuration. | **false** | - |
209212
| | See <https://github.com/docker/setup-buildx-action#inputs>. | | |

actions/docker/build-image/action.yml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ inputs:
9898
cache-type:
9999
description: |
100100
Cache type. Set to `false` or empty to disable cache entirely.
101+
For `gha`, cache restore remains enabled on untrusted default-branch-scoped triggers,
102+
but cache export is skipped automatically because GitHub only grants read-only cache access there.
101103
See https://docs.docker.com/build/cache/backends.
102104
default: "gha"
103105
required: false
@@ -189,7 +191,9 @@ runs:
189191
CACHE_REGISTRY: ${{ steps.docker-setup.outputs.cache-registry }}
190192
CACHE_TYPE_INPUT: ${{ inputs.cache-type }}
191193
CONTEXT_INPUT: ${{ inputs.context }}
194+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
192195
DOCKERFILE_INPUT: ${{ inputs.dockerfile }}
196+
GH_REF: ${{ github.ref }}
193197
METADATA_IMAGE: ${{ steps.metadata.outputs.image }}
194198
MULTI_PLATFORM_INPUT: ${{ inputs.multi-platform }}
195199
PLATFORM_INPUT: ${{ inputs.platform }}
@@ -243,6 +247,31 @@ runs:
243247
core.setOutput('cache-disabled', cacheDisabled ? 'true' : 'false');
244248
core.setOutput('cache-type', cacheType);
245249
250+
const defaultBranch = (process.env.DEFAULT_BRANCH || '').trim();
251+
const githubRef = (process.env.GH_REF || '').trim();
252+
const defaultBranchRef = defaultBranch.length > 0 ? `refs/heads/${defaultBranch}` : '';
253+
const usesDefaultBranchScope = defaultBranchRef.length > 0 && githubRef === defaultBranchRef;
254+
const ghaCacheReadWriteEvents = new Set([
255+
'push',
256+
'schedule',
257+
'workflow_dispatch',
258+
'repository_dispatch',
259+
'delete',
260+
'registry_package',
261+
'page_build',
262+
]);
263+
const cacheExportDisabled = cacheType === 'gha'
264+
&& usesDefaultBranchScope
265+
&& !ghaCacheReadWriteEvents.has(context.eventName);
266+
core.setOutput('cache-export-disabled', cacheExportDisabled ? 'true' : 'false');
267+
268+
if (cacheExportDisabled) {
269+
core.info(
270+
`Disabling gha cache export for ${context.eventName} on ${githubRef}. `
271+
+ 'GitHub only grants read-only cache access for this default-branch-scoped trigger, so cache restores remain enabled and cache writes are skipped.'
272+
);
273+
}
274+
246275
const metadataImage = process.env.METADATA_IMAGE || '';
247276
const cacheRegistry = (process.env.CACHE_REGISTRY || '').trim();
248277
if (cacheType === 'registry' && !cacheRegistry.length) {
@@ -353,7 +382,7 @@ runs:
353382
secret-envs: ${{ inputs.secret-envs }}
354383
platforms: ${{ inputs.platform }}
355384
cache-from: ${{ steps.get-docker-config.outputs.cache-disabled != 'true' && steps.cache-arguments.outputs.cache-from || '' }}
356-
cache-to: ${{ steps.get-docker-config.outputs.cache-disabled != 'true' && steps.cache-arguments.outputs.cache-to || '' }}
385+
cache-to: ${{ steps.get-docker-config.outputs.cache-disabled != 'true' && steps.get-docker-config.outputs.cache-export-disabled != 'true' && steps.cache-arguments.outputs.cache-to || '' }}
357386
no-cache: ${{ steps.get-docker-config.outputs.cache-disabled }}
358387
outputs: |
359388
${{ steps.get-docker-config.outputs.multi-platform && 'type=image,push-by-digest=true,name-canonical=true,push=true' || 'type=image,push=true' }}

0 commit comments

Comments
 (0)