]> git.lizzy.rs Git - rust.git/blob - src/ci/docker/run.sh
[CI] record docker image info for reuse
[rust.git] / src / ci / docker / run.sh
1 #!/usr/bin/env bash
2
3 set -e
4
5 export MSYS_NO_PATHCONV=1
6
7 script=`cd $(dirname $0) && pwd`/`basename $0`
8 image=$1
9
10 docker_dir="`dirname $script`"
11 ci_dir="`dirname $docker_dir`"
12 src_dir="`dirname $ci_dir`"
13 root_dir="`dirname $src_dir`"
14
15 objdir=$root_dir/obj
16 dist=$objdir/build/dist
17
18 source "$ci_dir/shared.sh"
19
20 travis_fold start build_docker
21 travis_time_start
22
23 if [ -f "$docker_dir/$image/Dockerfile" ]; then
24     if [ "$CI" != "" ]; then
25       hash_key=/tmp/.docker-hash-key.txt
26       rm -f "${hash_key}"
27       echo $image >> $hash_key
28
29       cat "$docker_dir/$image/Dockerfile" >> $hash_key
30       # Look for all source files involves in the COPY command
31       copied_files=/tmp/.docker-copied-files.txt
32       rm -f "$copied_files"
33       for i in $(sed -n -e 's/^COPY \(.*\) .*$/\1/p' "$docker_dir/$image/Dockerfile"); do
34         # List the file names
35         find "$docker_dir/$i" -type f >> $copied_files
36       done
37       # Sort the file names and cat the content into the hash key
38       sort $copied_files | xargs cat >> $hash_key
39
40       docker --version >> $hash_key
41       cksum=$(sha512sum $hash_key | \
42         awk '{print $1}')
43       s3url="s3://$SCCACHE_BUCKET/docker/$cksum"
44       url="https://s3-us-west-1.amazonaws.com/$SCCACHE_BUCKET/docker/$cksum"
45       echo "Attempting to download $s3url"
46       rm -f /tmp/rustci_docker_cache
47       set +e
48       retry curl -y 30 -Y 10 --connect-timeout 30 -f -L -C - -o /tmp/rustci_docker_cache "$url"
49       loaded_images=$(docker load -i /tmp/rustci_docker_cache | sed 's/.* sha/sha/')
50       set -e
51       echo "Downloaded containers:\n$loaded_images"
52     fi
53
54     dockerfile="$docker_dir/$image/Dockerfile"
55     if [ -x /usr/bin/cygpath ]; then
56         context="`cygpath -w $docker_dir`"
57         dockerfile="`cygpath -w $dockerfile`"
58     else
59         context="$docker_dir"
60     fi
61     retry docker \
62       build \
63       --rm \
64       -t rust-ci \
65       -f "$dockerfile" \
66       "$context"
67
68     if [ "$s3url" != "" ]; then
69       digest=$(docker inspect rust-ci --format '{{.Id}}')
70       echo "Built container $digest"
71       if ! grep -q "$digest" <(echo "$loaded_images"); then
72         echo "Uploading finished image to $s3url"
73         set +e
74         docker history -q rust-ci | \
75           grep -v missing | \
76           xargs docker save | \
77           gzip | \
78           aws s3 cp - $s3url
79         set -e
80       else
81         echo "Looks like docker image is the same as before, not uploading"
82       fi
83       # Record the container image for reuse, e.g. by rustup.rs builds
84       info="$dist/image-$image.txt"
85       mkdir -p "$dist"
86       echo "$url" >"$info"
87       echo "$digest" >>"$info"
88     fi
89 elif [ -f "$docker_dir/disabled/$image/Dockerfile" ]; then
90     if [ -n "$TRAVIS_OS_NAME" ]; then
91         echo Cannot run disabled images on travis!
92         exit 1
93     fi
94     # retry messes with the pipe from tar to docker. Not needed on non-travis
95     # Transform changes the context of disabled Dockerfiles to match the enabled ones
96     tar --transform 's#^./disabled/#./#' -C $docker_dir -c . | docker \
97       build \
98       --rm \
99       -t rust-ci \
100       -f "$image/Dockerfile" \
101       -
102 else
103     echo Invalid image: $image
104     exit 1
105 fi
106
107 travis_fold end build_docker
108 travis_time_finish
109
110 mkdir -p $HOME/.cargo
111 mkdir -p $objdir/tmp
112 mkdir -p $objdir/cores
113
114 args=
115 if [ "$SCCACHE_BUCKET" != "" ]; then
116     args="$args --env SCCACHE_BUCKET"
117     args="$args --env SCCACHE_REGION"
118     args="$args --env AWS_ACCESS_KEY_ID"
119     args="$args --env AWS_SECRET_ACCESS_KEY"
120 else
121     mkdir -p $HOME/.cache/sccache
122     args="$args --env SCCACHE_DIR=/sccache --volume $HOME/.cache/sccache:/sccache"
123 fi
124
125 # Run containers as privileged as it should give them access to some more
126 # syscalls such as ptrace and whatnot. In the upgrade to LLVM 5.0 it was
127 # discovered that the leak sanitizer apparently needs these syscalls nowadays so
128 # we'll need `--privileged` for at least the `x86_64-gnu` builder, so this just
129 # goes ahead and sets it for all builders.
130 args="$args --privileged"
131
132 exec docker \
133   run \
134   --volume "$root_dir:/checkout:ro" \
135   --volume "$objdir:/checkout/obj" \
136   --workdir /checkout/obj \
137   --env SRC=/checkout \
138   $args \
139   --env CARGO_HOME=/cargo \
140   --env DEPLOY \
141   --env DEPLOY_ALT \
142   --env LOCAL_USER_ID=`id -u` \
143   --env TRAVIS \
144   --env TRAVIS_BRANCH \
145   --env TOOLSTATE_REPO_ACCESS_TOKEN \
146   --env CI_JOB_NAME="${CI_JOB_NAME-$IMAGE}" \
147   --volume "$HOME/.cargo:/cargo" \
148   --volume "$HOME/rustsrc:$HOME/rustsrc" \
149   --init \
150   --rm \
151   rust-ci \
152   /checkout/src/ci/run.sh