Skip to content

Commit 5ad8b17

Browse files
authored
Generate cache-map from Dockerfile (#30)
1 parent ed2668f commit 5ad8b17

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

.github/workflows/integration-tests.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ jobs:
122122
build-args: |
123123
DEBIAN_VERSION=12.9
124124
from-scratch: ${{ matrix.test.from-scratch || 'false' }}
125-
cache-mount-ids: |
126-
/var/cache/apt
127-
/var/lib/apt
128125
- name: Validate image works
129126
run: |
130127
docker pull "${{ steps.build.outputs.image }}"

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ jobs:
3737
github-token=${{ secrets.token || github.token }}
3838
# Build images from scratch on "main". Ensures that caching doesn't result in using insecure system packages.
3939
from-scratch: ${{ github.ref == 'refs/heads/main' }}
40-
cache-mount-ids: |
41-
/var/cache/apt
42-
/var/lib/apt
4340
```
4441
4542
## Inputs
@@ -52,7 +49,7 @@ jobs:
5249
| `build-args` | List of [build-time variables](https://docs.docker.com/reference/cli/docker/buildx/build/#build-arg). | No | <pre><code>HTTP_PROXY=http://10.20.30.2:1234 &#10;FTP_PROXY=http://40.50.60.5:4567</code></pre> |
5350
| `build-secrets` | List of [secrets](https://docs.docker.com/engine/reference/commandline/buildx_build/#secret) to expose to the build. | No | <pre><code>GIT_AUTH_TOKEN=mytoken</code></pre> |
5451
| `from-scratch` | Do not read from the cache when building the image. Writes to caches will still occur. Defaults to `false`. | No | `false` |
55-
| `cache-mount-ids` | List of build [cache mount IDs or targets](https://docs.docker.com/reference/dockerfile/#run---mounttypecache) to preserve across builds. | No | <pre><code>/var/cache/apt&#10;/var/lib/apt</code></pre> |
52+
| `cache-mount-ids` | List of build [cache mount IDs or targets](https://docs.docker.com/reference/dockerfile/#run---mounttypecache) to preserve across builds. By default the IDs are determined from the Dockerfile as specified in `dockerfile`. | No | <pre><code>/var/cache/apt&#10;/var/lib/apt</code></pre> |
5653

5754
## Outputs
5855

action.yaml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ inputs:
2828
occur.
2929
default: "false"
3030
cache-mount-ids:
31-
description: List of build cache mount IDs or targets to preserve across builds.
31+
description: >-
32+
List of build cache mount IDs or targets to preserve across builds. By default the
33+
IDs are determined from the Dockerfile as specified in `dockerfile`.
3234
default: ""
3335
outputs:
3436
image:
@@ -157,15 +159,22 @@ runs:
157159
env:
158160
from_tags: ${{ steps.cache-from.outputs.tags }}
159161
to_tags: ${{ steps.cache-to.outputs.tags }}
162+
# Clean up the bind root path which otherwise may look like: /home/runner/work/docker-build/docker-build/.//cache-mount
163+
- name: Bind root
164+
id: bind-root
165+
shell: bash
166+
run: echo "path=$(readlink -f "${bind_root:?}")" | tee -a "$GITHUB_OUTPUT"
167+
env:
168+
bind_root: ${{ github.action_path }}/cache-mount
160169
- name: Process cache mount IDs
161170
id: cache-mount
171+
if: ${{ inputs.cache-mount-ids != '' }}
162172
shell: bash
163173
run: |
164174
# Process cache mount IDs
165175
ids_json="$(jq -R 'select(. != "")' <<<"${cache_mount_ids}" | jq -sc .)"
166176
167177
# We fully support user provided IDs which are absolute paths.
168-
bind_root="$(readlink -f "${bind_root:?}")"
169178
cache_map_json="$(jq --arg bind_root "${bind_root:?}" 'map(. as $id | {"key": ($bind_root + "/" + $id), "value": {"id": $id, "target": ("/tmp/" + $id)}}) | from_entries' <<<"${ids_json}")"
170179
171180
# Specify our multiline output using GH action flavored heredocs
@@ -177,20 +186,21 @@ runs:
177186
} | tee -a "$GITHUB_OUTPUT"
178187
env:
179188
cache_mount_ids: ${{ inputs.cache-mount-ids }}
180-
bind_root: ${{ github.action_path }}/cache-mount
189+
bind_root: ${{ steps.bind-root.outputs.path }}
181190
- name: Retrieve Docker cache mount bundle
182191
uses: actions/cache@v4
183192
with:
184-
path: |
185-
${{ github.action_path }}/cache-mount
193+
path: ${{ steps.bind-root.outputs.path }}
186194
key: docker-build-cache-mount-${{ hashFiles(format('{0}/**', inputs.context)) }}
187195
restore-keys: |
188196
docker-build-cache-mount
189197
# https://docs.docker.com/build/ci/github-actions/cache/#cache-mounts
190198
- name: Restore Docker cache mounts
191-
uses: reproducible-containers/buildkit-cache-dance@61bd187f75f25d38e056fdd48506fac777c6ebec
199+
uses: reproducible-containers/buildkit-cache-dance@5b81f4d29dc8397a7d341dba3aeecc7ec54d6361 # v3.3.0
192200
with:
193201
cache-map: ${{ steps.cache-mount.outputs.cache-map }}
202+
dockerfile: ${{ inputs.dockerfile || format('{0}/Dockerfile', inputs.context) }}
203+
cache-dir: ${{ steps.bind-root.outputs.path }}
194204
builder: ${{ steps.builder.outputs.name }}
195205
- name: Build and Push
196206
id: build-push

0 commit comments

Comments
 (0)