Skip to content

Commit 3ae7f2f

Browse files
NikitaCOEURneilime
authored andcommitted
fix(gha): workaround cache-to and cache-from scope
This commit introduces a workaround for the GitHub Actions cache backend when using the docker `cache-from` and `cache-to` flags with BuildKit. The current version of `int128/docker-build-cache-config-action` incorrectly uses `ref=` instead of `scope=` for the `gha` cache backend, which leads to shared caches across branches and builds, resulting in frequent invalidations and inefficient caching. Until the upstream PR is merged and released: int128/docker-build-cache-config-action#1213 ...this commit replace `ref=` by `scope=` to ensure proper cache isolation and reliable cache behavior in CI. This workaround will be removed once the upstream fix is available in a stable release.
1 parent 62e592d commit 3ae7f2f

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

actions/docker/build-image/action.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,30 @@ runs:
210210
cache-type: ${{ inputs.cache-type }}
211211
extra-cache-to: "image-manifest=true,oci-mediatypes=true"
212212

213+
# FIXME: Remove this when https://github.com/int128/docker-build-cache-config-action/pull/1213 is merged
214+
- id: transform-cache-gha
215+
if: inputs.cache-type == 'gha'
216+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
217+
with:
218+
script: |
219+
const cacheFrom = `${{ steps.cache.outputs.cache-from }}`;
220+
const cacheTo = `${{ steps.cache.outputs.cache-to }}`;
221+
222+
core.info(`Original cache-from: ${cacheFrom}`);
223+
core.info(`Original cache-to: ${cacheTo}`);
224+
225+
// Transform cache-from: replace ref= with scope=
226+
const transformedCacheFrom = cacheFrom.replace(/ref=/g, 'scope=');
227+
228+
// Transform cache-to: replace ref= with scope=
229+
const transformedCacheTo = cacheTo.replace(/ref=/g, 'scope=');
230+
231+
core.info(`Transformed cache-from: ${transformedCacheFrom}`);
232+
core.info(`Transformed cache-to: ${transformedCacheTo}`);
233+
234+
core.setOutput('cache-from', transformedCacheFrom);
235+
core.setOutput('cache-to', transformedCacheTo);
236+
213237
- if: steps.get-docker-config.outputs.docker-exists != 'true'
214238
uses: docker/setup-docker-action@b60f85385d03ac8acfca6d9996982511d8620a19 # v4.3.0
215239

@@ -242,8 +266,10 @@ runs:
242266
secrets: ${{ inputs.secrets }}
243267
secret-envs: ${{ inputs.secret-envs }}
244268
platforms: ${{ inputs.platform }}
245-
cache-from: ${{ steps.cache.outputs.cache-from }}
246-
cache-to: ${{ steps.cache.outputs.cache-to }}
269+
# FIXME: Remove 'inputs.cache-type == 'gha' && steps.transform-cache-gha.outputs.cache-from ||' when https://github.com/int128/docker-build-cache-config-action/pull/1213 is merged
270+
cache-from: ${{ inputs.cache-type == 'gha' && steps.transform-cache-gha.outputs.cache-from || steps.cache.outputs.cache-from }}
271+
# FIXME: Remove 'inputs.cache-type == 'gha' && steps.transform-cache-gha.outputs.cache-to ||' when https://github.com/int128/docker-build-cache-config-action/pull/1213 is merged
272+
cache-to: ${{ inputs.cache-type == 'gha' && steps.transform-cache-gha.outputs.cache-to || steps.cache.outputs.cache-to }}
247273
outputs: "type=image,push=true,push-by-digest=true,name-canonical=true,name=${{ steps.metadata.outputs.image }}"
248274
labels: ${{ steps.metadata.outputs.labels }}
249275
annotations: ${{ steps.metadata.outputs.annotations }}

0 commit comments

Comments
 (0)