Skip to content

Commit a4a2542

Browse files
committed
CI: try to cache PR CI Docker builds
1 parent c5b5713 commit a4a2542

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/ci/docker/run.sh

+26-3
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,34 @@ 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+
# On non-CI jobs, we don't do any caching.
96+
if [[ "$CI" == "" ]];
9897
then
9998
retry docker build --rm -t rust-ci -f "$dockerfile" "$context"
99+
# On PR CI jobs, we don't have permissions to write to the registry cache, but we can still read from it.
100+
elif [[ "$PR_CI_JOB" == "1" ]];
101+
then
102+
REGISTRY=ghcr.io
103+
# Most probably rust-lang-ci, but in general the owner of the repository where CI runs
104+
REGISTRY_USERNAME=rust-lang-ci
105+
# Tag used to cache the Docker build
106+
# It seems that it cannot be the same as $IMAGE_TAG, otherwise it overwrites the cache
107+
CACHE_IMAGE_TAG=${REGISTRY}/${REGISTRY_USERNAME}/rust-ci-cache:${cksum}
108+
109+
# Enable a new Docker driver so that --cache-from/to works with a registry backend
110+
docker buildx create --use --driver docker-container
111+
112+
# Build the image using registry caching backend
113+
retry docker \
114+
buildx \
115+
build \
116+
--rm \
117+
-t rust-ci \
118+
-f "$dockerfile" \
119+
--cache-from type=registry,ref=${CACHE_IMAGE_TAG} \
120+
--output=type=docker \
121+
"$context"
122+
# On auto/try builds, we can also write to it
100123
else
101124
REGISTRY=ghcr.io
102125
# Most probably rust-lang-ci, but in general the owner of the repository where CI runs

0 commit comments

Comments
 (0)