Skip to content

Commit 948e031

Browse files
authored
Rollup merge of rust-lang#122563 - Kobzol:ci-pr-caching, r=Mark-Simulacrum
CI: cache PR CI Docker builds An attempt to restore Docker caching for PR CI workflows that share the Docker image with an auto/try workflow. This was broken by my previous [PR](rust-lang#119290) that changed how we cache the Docker builds. [Before](rust-lang#122545): ![image](https://github.com/rust-lang/rust/assets/4539057/05e0d347-af64-4e85-bc99-0e4ac07192ec) After: ![image](https://github.com/rust-lang/rust/assets/4539057/2f657d60-b242-45eb-ac61-d8f71787edda) r? ``@ghost``
2 parents 1745f3d + 0754595 commit 948e031

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/ci/docker/run.sh

+29-12
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,38 @@ if [ -f "$docker_dir/$image/Dockerfile" ]; then
9292
# Print docker version
9393
docker --version
9494

95-
# On non-CI or PR jobs, we don't have permissions to write to the registry cache, so we should
96-
# not use `docker login` nor caching.
97-
if [[ "$CI" == "" ]] || [[ "$PR_CI_JOB" == "1" ]];
95+
REGISTRY=ghcr.io
96+
# PR CI runs on rust-lang, but we want to use the cache from rust-lang-ci
97+
REGISTRY_USERNAME=rust-lang-ci
98+
# Tag used to push the final Docker image, so that it can be pulled by e.g. rustup
99+
IMAGE_TAG=${REGISTRY}/${REGISTRY_USERNAME}/rust-ci:${cksum}
100+
# Tag used to cache the Docker build
101+
# It seems that it cannot be the same as $IMAGE_TAG, otherwise it overwrites the cache
102+
CACHE_IMAGE_TAG=${REGISTRY}/${REGISTRY_USERNAME}/rust-ci-cache:${cksum}
103+
104+
# On non-CI jobs, we don't do any caching.
105+
if [[ "$CI" == "" ]];
98106
then
99107
retry docker build --rm -t rust-ci -f "$dockerfile" "$context"
100-
else
101-
REGISTRY=ghcr.io
102-
# Most probably rust-lang-ci, but in general the owner of the repository where CI runs
103-
REGISTRY_USERNAME=${GITHUB_REPOSITORY_OWNER}
104-
# Tag used to push the final Docker image, so that it can be pulled by e.g. rustup
105-
IMAGE_TAG=${REGISTRY}/${REGISTRY_USERNAME}/rust-ci:${cksum}
106-
# Tag used to cache the Docker build
107-
# It seems that it cannot be the same as $IMAGE_TAG, otherwise it overwrites the cache
108-
CACHE_IMAGE_TAG=${REGISTRY}/${REGISTRY_USERNAME}/rust-ci-cache:${cksum}
108+
# On PR CI jobs, we don't have permissions to write to the registry cache,
109+
# but we can still read from it.
110+
elif [[ "$PR_CI_JOB" == "1" ]];
111+
then
112+
# Enable a new Docker driver so that --cache-from works with a registry backend
113+
docker buildx create --use --driver docker-container
109114

115+
# Build the image using registry caching backend
116+
retry docker \
117+
buildx \
118+
build \
119+
--rm \
120+
-t rust-ci \
121+
-f "$dockerfile" \
122+
--cache-from type=registry,ref=${CACHE_IMAGE_TAG} \
123+
--output=type=docker \
124+
"$context"
125+
# On auto/try builds, we can also write to the cache.
126+
else
110127
# Log into the Docker registry, so that we can read/write cache and the final image
111128
echo ${DOCKER_TOKEN} | docker login ${REGISTRY} \
112129
--username ${REGISTRY_USERNAME} \

0 commit comments

Comments
 (0)