Skip to content

Commit 7d72e48

Browse files
committed
Another attempt at docker caching in CI
The default "gha" seems to be spraying so much into the cache that everything's just gettting evicted for taking up too much space. I also don't know why it's spraying so much. Try out something else which is more targeted and should at least have a useful cache key name.
1 parent 0b2a3af commit 7d72e48

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: 'Build docker image'
2+
description: 'Build docker image'
3+
4+
inputs:
5+
context:
6+
description: 'Context argument'
7+
required: true
8+
file:
9+
description: 'Docker file'
10+
required: true
11+
cache_key_prefix:
12+
description: 'Cache key prefix'
13+
required: true
14+
15+
runs:
16+
using: composite
17+
steps:
18+
# Docker-recommended setup.
19+
- name: Set up Docker Buildx
20+
uses: docker/setup-buildx-action@v4
21+
22+
# Disable things that docker has on-by-default from the above which we're
23+
# not interested in.
24+
- name: Disable some docker things that are on-by-default
25+
shell: bash
26+
run: |
27+
echo DOCKER_BUILD_CHECKS_ANNOTATIONS=false >> $GITHUB_ENV
28+
echo DOCKER_BUILD_SUMMARY=false >> $GITHUB_ENV
29+
echo DOCKER_BUILD_RECORD_UPLOAD=false >> $GITHUB_ENV
30+
31+
# We'll be using this cache key and the "local" caching mode of docker. This
32+
# allows pretty fine-grained views into what's being cache. Experimentation
33+
# found that the default `gha` caching mode sprays quite a lot into the
34+
# cache such that everything keeps getting evicted for being over the size
35+
# limit, so this is the next-best attempt to do something a bit more
36+
# targeted.
37+
- name: Setup Github Actions cache
38+
uses: actions/cache@v5
39+
with:
40+
path: ${{ runner.tool_cache }}/docker-buildx-cache
41+
key: ${{ inputs.cache_key_prefix }}-${{ hashFiles(inputs.file) }}
42+
43+
- name: Build docker image
44+
uses: docker/build-push-action@v7
45+
with:
46+
context: ${{ inputs.context }}
47+
file: ${{ inputs.file }}
48+
tags: build-image
49+
cache-from: type=local,src=${{ runner.tool_cache }}/docker-buildx-cache
50+
cache-to: type=local,dest=${{ runner.tool_cache }}/docker-buildx-cache
51+
outputs: type=docker

.github/workflows/main.yml

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,40 +1297,19 @@ jobs:
12971297
with:
12981298
submodules: true
12991299

1300-
# Attempt to speed up building of docker images for Linux-based binaries by
1301-
# using docker's recommended way of building/caching with github actions.
1302-
# Notably this caches layers to github actions which enables subsequent
1303-
# reuse in future runs. The goal here is to make this step faster while
1304-
# additionally reducing network flakiness since the github actions cache
1305-
# generally works better than upstream package servers, or at least in
1306-
# theory.
1300+
# Build the docker image here, before it's built below, to have
1301+
# finer-grained control over caching. The way this is set up is that the
1302+
# build in this step is picked up by the later step automatically, and if
1303+
# this one's buggy the next one will just take a bit longer.
13071304
#
1308-
# The way that this is designed is to build the image with docker-related
1309-
# github actions here primarily. This populates the github actions cache
1310-
# but also the local docker daemon cache too. Subsequently when the actual
1311-
# build script runs it'll attempt to rebuild the image and it should get a
1312-
# bunch of cache hits from here. This way we can configure the caching here
1313-
# within github actions without impacting the functionality of the script
1314-
# itself and continue enabling it to run locally.
1315-
- name: Set up Docker Buildx
1316-
uses: docker/setup-buildx-action@v4
1305+
# The goal here is to hit the network less because apt-get servers tend
1306+
# to be relatively flaky...
1307+
- uses: ./.github/actions/docker-build-cached
13171308
if: ${{ matrix.env.DOCKER_IMAGE }}
1318-
- name: Disable some docker things that are on-by-default
1319-
run: |
1320-
echo DOCKER_BUILD_CHECKS_ANNOTATIONS=false >> $GITHUB_ENV
1321-
echo DOCKER_BUILD_SUMMARY=false >> $GITHUB_ENV
1322-
echo DOCKER_BUILD_RECORD_UPLOAD=false >> $GITHUB_ENV
1323-
if: ${{ matrix.env.DOCKER_IMAGE }}
1324-
- name: Build docker image
1325-
if: ${{ matrix.env.DOCKER_IMAGE }}
1326-
uses: docker/build-push-action@v7
13271309
with:
13281310
context: ci/docker
13291311
file: ${{ matrix.env.DOCKER_IMAGE }}
1330-
tags: build-image
1331-
cache-from: type=gha
1332-
cache-to: type=gha,mode=max
1333-
outputs: type=docker
1312+
cache_key_prefix: docker-${{ matrix.target }}
13341313

13351314
- uses: ./.github/actions/install-ninja
13361315
if: '${{ !matrix.env.DOCKER_IMAGE }}'

0 commit comments

Comments
 (0)