Skip to content

Commit 7dae399

Browse files
committed
Cache CI Docker images in ghcr registry
1 parent cb25c5b commit 7dae399

File tree

3 files changed

+57
-77
lines changed

3 files changed

+57
-77
lines changed

.github/workflows/ci.yml

+13-20
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ name: CI
2828
- "**"
2929
permissions:
3030
contents: read
31+
packages: write
3132
defaults:
3233
run:
3334
shell: bash
@@ -42,6 +43,7 @@ jobs:
4243
CI_JOB_NAME: "${{ matrix.name }}"
4344
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
4445
HEAD_SHA: "${{ github.event.pull_request.head.sha || github.sha }}"
46+
DOCKER_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
4547
SCCACHE_BUCKET: rust-lang-ci-sccache2
4648
TOOLSTATE_REPO: "https://github.com/rust-lang-nursery/rust-toolstate"
4749
CACHE_DOMAIN: ci-caches.rust-lang.org
@@ -50,18 +52,8 @@ jobs:
5052
strategy:
5153
matrix:
5254
include:
53-
- name: mingw-check
54-
os: ubuntu-20.04-4core-16gb
55-
env: {}
56-
- name: mingw-check-tidy
57-
os: ubuntu-20.04-4core-16gb
58-
env: {}
59-
- name: x86_64-gnu-llvm-16
60-
env:
61-
ENABLE_GCC_CODEGEN: "1"
62-
os: ubuntu-20.04-16core-64gb
63-
- name: x86_64-gnu-tools
64-
os: ubuntu-20.04-16core-64gb
55+
- name: dist-quick
56+
os: ubuntu-20.04
6557
env: {}
6658
timeout-minutes: 600
6759
runs-on: "${{ matrix.os }}"
@@ -172,6 +164,7 @@ jobs:
172164
CI_JOB_NAME: "${{ matrix.name }}"
173165
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
174166
HEAD_SHA: "${{ github.event.pull_request.head.sha || github.sha }}"
167+
DOCKER_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
175168
SCCACHE_BUCKET: rust-lang-ci-sccache2
176169
DEPLOY_BUCKET: rust-lang-ci2
177170
TOOLSTATE_REPO: "https://github.com/rust-lang-nursery/rust-toolstate"
@@ -204,7 +197,7 @@ jobs:
204197
os: ubuntu-20.04-8core-32gb
205198
env: {}
206199
- name: dist-arm-linux
207-
os: ubuntu-20.04-16core-64gb
200+
os: ubuntu-20.04
208201
env: {}
209202
- name: dist-armhf-linux
210203
os: ubuntu-20.04-8core-32gb
@@ -251,12 +244,12 @@ jobs:
251244
- name: dist-x86_64-linux
252245
env:
253246
CODEGEN_BACKENDS: "llvm,cranelift"
254-
os: ubuntu-20.04-16core-64gb
247+
os: ubuntu-20.04
255248
- name: dist-x86_64-linux-alt
256249
env:
257250
IMAGE: dist-x86_64-linux
258251
CODEGEN_BACKENDS: "llvm,cranelift"
259-
os: ubuntu-20.04-16core-64gb
252+
os: ubuntu-20.04
260253
- name: dist-x86_64-musl
261254
env:
262255
CODEGEN_BACKENDS: "llvm,cranelift"
@@ -291,7 +284,7 @@ jobs:
291284
- name: x86_64-gnu-integration
292285
env:
293286
CI_ONLY_WHEN_CHANNEL: nightly
294-
os: ubuntu-20.04-16core-64gb
287+
os: ubuntu-20.04
295288
- name: x86_64-gnu-debug
296289
os: ubuntu-20.04-8core-32gb
297290
env: {}
@@ -554,6 +547,7 @@ jobs:
554547
CI_JOB_NAME: "${{ matrix.name }}"
555548
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
556549
HEAD_SHA: "${{ github.event.pull_request.head.sha || github.sha }}"
550+
DOCKER_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
557551
SCCACHE_BUCKET: rust-lang-ci-sccache2
558552
DEPLOY_BUCKET: rust-lang-ci2
559553
TOOLSTATE_REPO: "https://github.com/rust-lang-nursery/rust-toolstate"
@@ -567,10 +561,9 @@ jobs:
567561
strategy:
568562
matrix:
569563
include:
570-
- name: dist-x86_64-linux
571-
env:
572-
CODEGEN_BACKENDS: "llvm,cranelift"
573-
os: ubuntu-20.04-16core-64gb
564+
- name: dist-quick
565+
os: ubuntu-20.04
566+
env: {}
574567
timeout-minutes: 600
575568
runs-on: "${{ matrix.os }}"
576569
steps:

src/ci/docker/run.sh

+42-57
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,6 @@ if [ -f "$docker_dir/$image/Dockerfile" ]; then
7474

7575
cksum=$(sha512sum $hash_key | \
7676
awk '{print $1}')
77-
78-
url="https://$CACHE_DOMAIN/docker/$cksum"
79-
80-
echo "Attempting to download $url"
81-
rm -f /tmp/rustci_docker_cache
82-
set +e
83-
retry curl --max-time 600 -y 30 -Y 10 --connect-timeout 30 -f -L -C - \
84-
-o /tmp/rustci_docker_cache "$url"
85-
86-
docker_archive_hash=$(sha512sum /tmp/rustci_docker_cache | awk '{print $1}')
87-
echo "Downloaded archive hash: ${docker_archive_hash}"
88-
89-
echo "Loading images into docker"
90-
# docker load sometimes hangs in the CI, so time out after 10 minutes with TERM,
91-
# KILL after 12 minutes
92-
loaded_images=$(/usr/bin/timeout -k 720 600 docker load -i /tmp/rustci_docker_cache \
93-
| sed 's/.* sha/sha/')
94-
set -e
95-
printf "Downloaded containers:\n$loaded_images\n"
9677
fi
9778

9879
dockerfile="$docker_dir/$image/Dockerfile"
@@ -103,46 +84,50 @@ if [ -f "$docker_dir/$image/Dockerfile" ]; then
10384
context="$script_dir"
10485
fi
10586
echo "::group::Building docker image for $image"
87+
echo "Image input checksum ${cksum}"
10688

107-
# As of August 2023, Github Actions have updated Docker to 23.X,
108-
# which uses the BuildKit by default. It currently throws aways all
109-
# intermediate layers, which breaks our usage of S3 layer caching.
110-
# Therefore we opt-in to the old build backend for now.
111-
export DOCKER_BUILDKIT=0
112-
retry docker \
113-
build \
114-
--rm \
115-
-t rust-ci \
116-
-f "$dockerfile" \
117-
"$context"
118-
echo "::endgroup::"
119-
120-
if [ "$CI" != "" ]; then
121-
s3url="s3://$SCCACHE_BUCKET/docker/$cksum"
122-
upload="aws s3 cp - $s3url"
123-
digest=$(docker inspect rust-ci --format '{{.Id}}')
124-
echo "Built container $digest"
125-
if ! grep -q "$digest" <(echo "$loaded_images"); then
126-
echo "Uploading finished image $digest to $url"
127-
set +e
128-
# Print image history for easier debugging of layer SHAs
129-
docker history rust-ci
130-
docker history -q rust-ci | \
131-
grep -v missing | \
132-
xargs docker save | \
133-
gzip | \
134-
$upload
135-
set -e
136-
else
137-
echo "Looks like docker image is the same as before, not uploading"
138-
fi
139-
# Record the container image for reuse, e.g. by rustup.rs builds
140-
info="$dist/image-$image.txt"
141-
mkdir -p "$dist"
142-
echo "$url" >"$info"
143-
echo "$digest" >>"$info"
144-
cat "$info"
89+
# On PR jobs, we don't have permissions to write to the registry cache, so we should
90+
# not use `docker login` nor caching.
91+
if [ "$PR_CI_JOB" -eq 1 ]
92+
then
93+
retry docker build --rm -t rust-ci -f "$dockerfile" "$context"
94+
else
95+
REGISTRY_USERNAME=rust-lang-ci
96+
IMAGE_TAG=ghcr.io/${REGISTRY_USERNAME}/rust-ci:${cksum}
97+
98+
echo ${DOCKER_TOKEN} | docker login ghcr.io --username ${REGISTRY_USERNAME} --password-stdin
99+
100+
# Enable a new Docker driver so that --cache-from/to works with a registry backend
101+
docker buildx create --use --driver docker-container
102+
103+
# Build the image using registry caching backend
104+
retry docker \
105+
buildx \
106+
build \
107+
--rm \
108+
-t rust-ci \
109+
-f "$dockerfile" \
110+
--cache-from type=registry,ref=${IMAGE_TAG} \
111+
--cache-to type=registry,ref=${IMAGE_TAG},compression=zstd \
112+
--output=type=docker \
113+
"$context"
114+
115+
# Print images for debugging purposes
116+
docker images
117+
118+
# Tag the built image and push it to the registry
119+
docker tag rust-ci "${IMAGE_TAG}"
120+
docker push "${IMAGE_TAG}"
121+
122+
if [ "$CI" != "" ]; then
123+
# Record the container registry tag/url for reuse, e.g. by rustup.rs builds
124+
info="$dist/image-$image.txt"
125+
mkdir -p "$dist"
126+
echo "${IMAGE_TAG}" > "$info"
127+
cat "$info"
128+
fi
145129
fi
130+
echo "::endgroup::"
146131
elif [ -f "$docker_dir/disabled/$image/Dockerfile" ]; then
147132
if isCI; then
148133
echo Cannot run disabled images on CI!

src/ci/github-actions/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ x--expand-yaml-anchors--remove:
3434
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
3535
# commit of PR sha or commit sha. `GITHUB_SHA` is not accurate for PRs.
3636
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
37+
DOCKER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3738

3839
- &public-variables
3940
SCCACHE_BUCKET: rust-lang-ci-sccache2
@@ -301,6 +302,7 @@ on:
301302

302303
permissions:
303304
contents: read
305+
packages: write
304306

305307
defaults:
306308
run:

0 commit comments

Comments
 (0)