Skip to content

Commit 6835e96

Browse files
committed
Merge bitcoin/bitcoin#31545: ci: optionally use local docker build cache
e87429a ci: optionally use local docker build cache (0xb10c) Pull request description: By setting `DANGER_DOCKER_BUILD_CACHE_HOST_DIR`, the task-specific docker images built during the CI run can be cached. This allows, for example, ephemeral CI runners to reuse the docker images (or layers of it) from earlier runs, by persisting the image cache before the ephemeral CI runner is shut down. The cache keyed by `CONTAINER_NAME`. As `--cache-to` doesn't remove old cache files, the existing cache is removed after a successful `docker build` and the newly cached image is moved to it's location to avoid the cache from growing indefinitely with old, unused layers. When `--cache-from` doesn't find the directory, the cached version is a cache-miss, or the cache can't be imported for whatever other reason, it warns and `docker build` continues by building the docker image. This feature is opt-in. The documentation for the docker build cache of `type=local` can be found on https://docs.docker.com/build/cache/backends/local/ This replaces bitcoin/bitcoin#31377 - some of the discussion there might provide more context. ACKs for top commit: maflcko: I haven't tested this, and it looks harmless and is easy to revert, if needed. So lgtm ACK e87429a achow101: ACK e87429a TheCharlatan: tACK e87429a willcl-ark: ACK e87429a Tree-SHA512: 0887c395dee2e2020394933246d4c1bfb6dde7165219cbe93eccfe01379e05c75dce8920b6edd7df07364c703fcee7be4fba8fa45fd0e0e89da9e24759f67a71
2 parents c7869cb + e87429a commit 6835e96

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

ci/test/02_run_container.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ if [ -z "$DANGER_RUN_CI_ON_HOST" ]; then
2525
fi
2626
echo "Creating $CI_IMAGE_NAME_TAG container to run in"
2727

28+
DOCKER_BUILD_CACHE_ARG=""
29+
DOCKER_BUILD_CACHE_TEMPDIR=""
30+
DOCKER_BUILD_CACHE_OLD_DIR=""
31+
DOCKER_BUILD_CACHE_NEW_DIR=""
32+
# If set, use an `docker build` cache directory on the CI host
33+
# to cache docker image layers for the CI container image.
34+
# This cache can be multiple GB in size. Prefixed with DANGER
35+
# as setting it removes (old cache) files from the host.
36+
if [ "$DANGER_DOCKER_BUILD_CACHE_HOST_DIR" ]; then
37+
# Directory where the current cache for this run could be. If not existing
38+
# or empty, "docker build" will warn, but treat it as cache-miss and continue.
39+
DOCKER_BUILD_CACHE_OLD_DIR="${DANGER_DOCKER_BUILD_CACHE_HOST_DIR}/${CONTAINER_NAME}"
40+
# Temporary directory for a newly created cache. We can't write the new
41+
# cache into OLD_DIR directly, as old cache layers would not be removed.
42+
# The NEW_DIR contents are moved to OLD_DIR after OLD_DIR has been cleared.
43+
# This happens after `docker build`. If a task fails or is aborted, the
44+
# DOCKER_BUILD_CACHE_TEMPDIR might be retained on the host. If the host isn't
45+
# ephemeral, it has to take care of cleaning old TEMPDIR's up.
46+
DOCKER_BUILD_CACHE_TEMPDIR="$(mktemp --directory ci-docker-build-cache-XXXXXXXXXX)"
47+
DOCKER_BUILD_CACHE_NEW_DIR="${DOCKER_BUILD_CACHE_TEMPDIR}/${CONTAINER_NAME}"
48+
DOCKER_BUILD_CACHE_ARG="--cache-from type=local,src=${DOCKER_BUILD_CACHE_OLD_DIR} --cache-to type=local,dest=${DOCKER_BUILD_CACHE_NEW_DIR},mode=max"
49+
fi
50+
2851
# shellcheck disable=SC2086
2952
DOCKER_BUILDKIT=1 docker build \
3053
--file "${BASE_READ_ONLY_DIR}/ci/test_imagefile" \
@@ -34,8 +57,18 @@ if [ -z "$DANGER_RUN_CI_ON_HOST" ]; then
3457
--platform="${CI_IMAGE_PLATFORM}" \
3558
--label="${CI_IMAGE_LABEL}" \
3659
--tag="${CONTAINER_NAME}" \
60+
$DOCKER_BUILD_CACHE_ARG \
3761
"${BASE_READ_ONLY_DIR}"
3862

63+
if [ "$DANGER_DOCKER_BUILD_CACHE_HOST_DIR" ]; then
64+
if [ -e "${DOCKER_BUILD_CACHE_NEW_DIR}/index.json" ]; then
65+
echo "Removing the existing docker build cache in ${DOCKER_BUILD_CACHE_OLD_DIR}"
66+
rm -rf "${DOCKER_BUILD_CACHE_OLD_DIR}"
67+
echo "Moving the contents of ${DOCKER_BUILD_CACHE_NEW_DIR} to ${DOCKER_BUILD_CACHE_OLD_DIR}"
68+
mv "${DOCKER_BUILD_CACHE_NEW_DIR}" "${DOCKER_BUILD_CACHE_OLD_DIR}"
69+
fi
70+
fi
71+
3972
docker volume create "${CONTAINER_NAME}_ccache" || true
4073
docker volume create "${CONTAINER_NAME}_depends" || true
4174
docker volume create "${CONTAINER_NAME}_depends_sources" || true

0 commit comments

Comments
 (0)