From 27d1d0a6a41059a6e499b137ef8b3cc50fb610b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 3 May 2024 15:49:15 +0200 Subject: [PATCH 1/3] Do not include Docker version in caching key We are now using Docker caching directly rather than downloading archives from S3, so it should not be needed. This should unblock reusing the cache locally. --- src/ci/docker/run.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ci/docker/run.sh b/src/ci/docker/run.sh index fcc507b572c27..b9a559039e09f 100755 --- a/src/ci/docker/run.sh +++ b/src/ci/docker/run.sh @@ -71,8 +71,6 @@ if [ -f "$docker_dir/$image/Dockerfile" ]; then # only run in x86_64 machines. uname -m >> $hash_key - docker --version >> $hash_key - # Include cache version. Can be used to manually bust the Docker cache. echo "2" >> $hash_key From 0372065097ec04e65fcf105c2a07f4cda5ed29f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 3 May 2024 15:50:02 +0200 Subject: [PATCH 2/3] Calculate the Docker hash key also locally --- src/ci/docker/run.sh | 60 +++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/ci/docker/run.sh b/src/ci/docker/run.sh index b9a559039e09f..acd88870b169f 100755 --- a/src/ci/docker/run.sh +++ b/src/ci/docker/run.sh @@ -50,37 +50,35 @@ fi CACHE_DOMAIN="${CACHE_DOMAIN:-ci-caches.rust-lang.org}" if [ -f "$docker_dir/$image/Dockerfile" ]; then - if isCI; then - hash_key=/tmp/.docker-hash-key.txt - rm -f "${hash_key}" - echo $image >> $hash_key - - cat "$docker_dir/$image/Dockerfile" >> $hash_key - # Look for all source files involves in the COPY command - copied_files=/tmp/.docker-copied-files.txt - rm -f "$copied_files" - for i in $(sed -n -e '/^COPY --from=/! s/^COPY \(.*\) .*$/\1/p' \ - "$docker_dir/$image/Dockerfile"); do - # List the file names - find "$script_dir/$i" -type f >> $copied_files - done - # Sort the file names and cat the content into the hash key - sort $copied_files | xargs cat >> $hash_key - - # Include the architecture in the hash key, since our Linux CI does not - # only run in x86_64 machines. - uname -m >> $hash_key - - # Include cache version. Can be used to manually bust the Docker cache. - echo "2" >> $hash_key - - echo "Image input" - cat $hash_key - - cksum=$(sha512sum $hash_key | \ - awk '{print $1}') - echo "Image input checksum ${cksum}" - fi + hash_key=/tmp/.docker-hash-key.txt + rm -f "${hash_key}" + echo $image >> $hash_key + + cat "$docker_dir/$image/Dockerfile" >> $hash_key + # Look for all source files involves in the COPY command + copied_files=/tmp/.docker-copied-files.txt + rm -f "$copied_files" + for i in $(sed -n -e '/^COPY --from=/! s/^COPY \(.*\) .*$/\1/p' \ + "$docker_dir/$image/Dockerfile"); do + # List the file names + find "$script_dir/$i" -type f >> $copied_files + done + # Sort the file names and cat the content into the hash key + sort $copied_files | xargs cat >> $hash_key + + # Include the architecture in the hash key, since our Linux CI does not + # only run in x86_64 machines. + uname -m >> $hash_key + + # Include cache version. Can be used to manually bust the Docker cache. + echo "2" >> $hash_key + + echo "Image input" + cat $hash_key + + cksum=$(sha512sum $hash_key | \ + awk '{print $1}') + echo "Image input checksum ${cksum}" dockerfile="$docker_dir/$image/Dockerfile" if [ -x /usr/bin/cygpath ]; then From 02f4ef7cde5ab7b69545791f3750ae03d251e8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 3 May 2024 15:52:00 +0200 Subject: [PATCH 3/3] Allow reusing CI Docker cache when running CI images locally --- src/ci/docker/run.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ci/docker/run.sh b/src/ci/docker/run.sh index acd88870b169f..22dcb808c743e 100755 --- a/src/ci/docker/run.sh +++ b/src/ci/docker/run.sh @@ -101,10 +101,18 @@ if [ -f "$docker_dir/$image/Dockerfile" ]; then # It seems that it cannot be the same as $IMAGE_TAG, otherwise it overwrites the cache CACHE_IMAGE_TAG=${REGISTRY}/${REGISTRY_USERNAME}/rust-ci-cache:${cksum} - # On non-CI jobs, we don't do any caching. + # On non-CI jobs, we try to download a pre-built image from the rust-lang-ci + # ghcr.io registry. If it is not possible, we fall back to building the image + # locally. if ! isCI; then - retry docker build --rm -t rust-ci -f "$dockerfile" "$context" + if docker pull "${IMAGE_TAG}"; then + echo "Downloaded Docker image from CI" + docker tag "${IMAGE_TAG}" rust-ci + else + echo "Building local Docker image" + retry docker build --rm -t rust-ci -f "$dockerfile" "$context" + fi # On PR CI jobs, we don't have permissions to write to the registry cache, # but we can still read from it. elif [[ "$PR_CI_JOB" == "1" ]];