Skip to content

Commit abdb773

Browse files
authored
Rollup merge of #59253 - kennytm:precise-docker-cache-hash, r=pietroalbini
Calculate Docker cache hash precisely from Dockerfile's dependencies #58549 changed the Docker cache calculation to include every file under `src/ci/docker`, so that when files under `dist-x86_64-linux` is changed, its dependent image `dist-i686-linux` will also be rebuilt. However, this ultraconservative solution caused the `dist-i686-linux` to be rebuilt every time an irrelevant Dockerfile (e.g. the PowerPC ones) is changed, which increases the building time beyond 3 hours and forcing a spurious but expected failure. This commit instead parses the Dockerfile itself and look for the actual dependencies. The scripts needs to be copied into the Docker image, which must be done with the COPY command, so we just need to find all lines with a COPY command and add the source file into the hash calculator. Note: this script only handles single-lined COPY command in the form `COPY src1 src2 src3 dst`, since these are the only variant used inside this repository.
2 parents 9da8fe4 + 07aee1d commit abdb773

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/ci/docker/run.sh

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ if [ -f "$docker_dir/$image/Dockerfile" ]; then
2222
hash_key=/tmp/.docker-hash-key.txt
2323
rm -f "${hash_key}"
2424
echo $image >> $hash_key
25-
find $docker_dir -type f | sort | xargs cat >> $hash_key
25+
26+
cat "$docker_dir/$image/Dockerfile" >> $hash_key
27+
# Look for all source files involves in the COPY command
28+
copied_files=/tmp/.docker-copied-files.txt
29+
rm -f "$copied_files"
30+
for i in $(sed -n -e 's/^COPY \(.*\) .*$/\1/p' "$docker_dir/$image/Dockerfile"); do
31+
# List the file names
32+
find "$docker_dir/$i" -type f >> $copied_files
33+
done
34+
# Sort the file names and cat the content into the hash key
35+
sort $copied_files | xargs cat >> $hash_key
36+
2637
docker --version >> $hash_key
2738
cksum=$(sha512sum $hash_key | \
2839
awk '{print $1}')

0 commit comments

Comments
 (0)