@@ -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