]> git.proxmox.com Git - rustc.git/blame - src/ci/docker/run.sh
New upstream version 1.41.1+dfsg1
[rustc.git] / src / ci / docker / run.sh
CommitLineData
abe05a73 1#!/usr/bin/env bash
476ff2be
SL
2
3set -e
4
abe05a73
XL
5export MSYS_NO_PATHCONV=1
6
476ff2be
SL
7script=`cd $(dirname $0) && pwd`/`basename $0`
8image=$1
9
10docker_dir="`dirname $script`"
11ci_dir="`dirname $docker_dir`"
12src_dir="`dirname $ci_dir`"
13root_dir="`dirname $src_dir`"
14
532ac7d7
XL
15objdir=$root_dir/obj
16dist=$objdir/build/dist
17
8bb4bdeb
XL
18source "$ci_dir/shared.sh"
19
7cac9316 20if [ -f "$docker_dir/$image/Dockerfile" ]; then
0531ce1d 21 if [ "$CI" != "" ]; then
0731742a 22 hash_key=/tmp/.docker-hash-key.txt
9fa01778
XL
23 rm -f "${hash_key}"
24 echo $image >> $hash_key
532ac7d7
XL
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
0731742a
XL
37 docker --version >> $hash_key
38 cksum=$(sha512sum $hash_key | \
0531ce1d 39 awk '{print $1}')
dc9dc135 40
0531ce1d 41 s3url="s3://$SCCACHE_BUCKET/docker/$cksum"
dc9dc135
XL
42 url="https://$SCCACHE_BUCKET.s3.amazonaws.com/docker/$cksum"
43 upload="aws s3 cp - $s3url"
44
45 echo "Attempting to download $url"
94b46f34 46 rm -f /tmp/rustci_docker_cache
0531ce1d 47 set +e
9fa01778 48 retry curl -y 30 -Y 10 --connect-timeout 30 -f -L -C - -o /tmp/rustci_docker_cache "$url"
94b46f34 49 loaded_images=$(docker load -i /tmp/rustci_docker_cache | sed 's/.* sha/sha/')
0531ce1d
XL
50 set -e
51 echo "Downloaded containers:\n$loaded_images"
52 fi
53
abe05a73
XL
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
7cac9316
XL
61 retry docker \
62 build \
63 --rm \
64 -t rust-ci \
abe05a73
XL
65 -f "$dockerfile" \
66 "$context"
0531ce1d 67
dc9dc135 68 if [ "$upload" != "" ]; then
0531ce1d
XL
69 digest=$(docker inspect rust-ci --format '{{.Id}}')
70 echo "Built container $digest"
71 if ! grep -q "$digest" <(echo "$loaded_images"); then
dc9dc135 72 echo "Uploading finished image to $url"
0531ce1d
XL
73 set +e
74 docker history -q rust-ci | \
75 grep -v missing | \
76 xargs docker save | \
77 gzip | \
dc9dc135 78 $upload
0531ce1d
XL
79 set -e
80 else
81 echo "Looks like docker image is the same as before, not uploading"
82 fi
532ac7d7
XL
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"
0531ce1d 88 fi
7cac9316 89elif [ -f "$docker_dir/disabled/$image/Dockerfile" ]; then
dc9dc135
XL
90 if isCI; then
91 echo Cannot run disabled images on CI!
7cac9316
XL
92 exit 1
93 fi
ea8adc8c
XL
94 # Transform changes the context of disabled Dockerfiles to match the enabled ones
95 tar --transform 's#^./disabled/#./#' -C $docker_dir -c . | docker \
7cac9316
XL
96 build \
97 --rm \
98 -t rust-ci \
ea8adc8c
XL
99 -f "$image/Dockerfile" \
100 -
7cac9316
XL
101else
102 echo Invalid image: $image
103 exit 1
104fi
105
476ff2be 106mkdir -p $HOME/.cargo
8bb4bdeb 107mkdir -p $objdir/tmp
8faf50e0 108mkdir -p $objdir/cores
e74abb32 109mkdir -p /tmp/toolstate
32a655c1
SL
110
111args=
112if [ "$SCCACHE_BUCKET" != "" ]; then
3b2f2976
XL
113 args="$args --env SCCACHE_BUCKET"
114 args="$args --env SCCACHE_REGION"
115 args="$args --env AWS_ACCESS_KEY_ID"
116 args="$args --env AWS_SECRET_ACCESS_KEY"
32a655c1
SL
117else
118 mkdir -p $HOME/.cache/sccache
119 args="$args --env SCCACHE_DIR=/sccache --volume $HOME/.cache/sccache:/sccache"
120fi
476ff2be 121
3b2f2976
XL
122# Run containers as privileged as it should give them access to some more
123# syscalls such as ptrace and whatnot. In the upgrade to LLVM 5.0 it was
124# discovered that the leak sanitizer apparently needs these syscalls nowadays so
125# we'll need `--privileged` for at least the `x86_64-gnu` builder, so this just
126# goes ahead and sets it for all builders.
127args="$args --privileged"
128
416331ca
XL
129# Things get a little weird if this script is already running in a docker
130# container. If we're already in a docker container then we assume it's set up
131# to do docker-in-docker where we have access to a working `docker` command.
132#
133# If this is the case (we check via the presence of `/.dockerenv`)
134# then we can't actually use the `--volume` argument. Typically we use
135# `--volume` to efficiently share the build and source directory between this
136# script and the container we're about to spawn. If we're inside docker already
137# though the `--volume` argument maps the *host's* folder to the container we're
138# about to spawn, when in fact we want the folder in this container itself. To
139# work around this we use a recipe cribbed from
140# https://circleci.com/docs/2.0/building-docker-images/#mounting-folders to
141# create a temporary container with a volume. We then copy the entire source
142# directory into this container, and then use that copy in the container we're
143# about to spawn. Finally after the build finishes we re-extract the object
144# directory.
145#
146# Note that none of this is necessary if we're *not* in a docker-in-docker
147# scenario. If this script is run on a bare metal host then we share a bunch of
148# data directories to share as much data as possible. Note that we also use
149# `LOCAL_USER_ID` (recognized in `src/ci/run.sh`) to ensure that files are all
150# read/written as the same user as the bare-metal user.
151if [ -f /.dockerenv ]; then
152 docker create -v /checkout --name checkout alpine:3.4 /bin/true
153 docker cp . checkout:/checkout
154 args="$args --volumes-from checkout"
155else
156 args="$args --volume $root_dir:/checkout:ro"
157 args="$args --volume $objdir:/checkout/obj"
158 args="$args --volume $HOME/.cargo:/cargo"
159 args="$args --volume $HOME/rustsrc:$HOME/rustsrc"
e74abb32 160 args="$args --volume /tmp/toolstate:/tmp/toolstate"
416331ca
XL
161 args="$args --env LOCAL_USER_ID=`id -u`"
162fi
163
164docker \
476ff2be 165 run \
476ff2be
SL
166 --workdir /checkout/obj \
167 --env SRC=/checkout \
32a655c1 168 $args \
476ff2be 169 --env CARGO_HOME=/cargo \
3b2f2976
XL
170 --env DEPLOY \
171 --env DEPLOY_ALT \
dc9dc135 172 --env CI \
dc9dc135
XL
173 --env TF_BUILD \
174 --env BUILD_SOURCEBRANCHNAME \
60c5eb7d
XL
175 --env GITHUB_ACTIONS \
176 --env GITHUB_REF \
ff7c6d11 177 --env TOOLSTATE_REPO_ACCESS_TOKEN \
dc9dc135 178 --env TOOLSTATE_REPO \
416331ca 179 --env TOOLSTATE_PUBLISH \
83c7162d 180 --env CI_JOB_NAME="${CI_JOB_NAME-$IMAGE}" \
3b2f2976 181 --init \
32a655c1 182 --rm \
476ff2be
SL
183 rust-ci \
184 /checkout/src/ci/run.sh
416331ca
XL
185
186if [ -f /.dockerenv ]; then
187 rm -rf $objdir
188 docker cp checkout:/checkout/obj $objdir
189fi