Skip to content

Commit f9534ea

Browse files
committed
Cache CI Docker images in ghcr registry
1 parent f2348fb commit f9534ea

File tree

4 files changed

+94
-79
lines changed

4 files changed

+94
-79
lines changed

.github/workflows/ci.yml

+15-17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
CI_JOB_NAME: "${{ matrix.name }}"
4343
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
4444
HEAD_SHA: "${{ github.event.pull_request.head.sha || github.sha }}"
45+
DOCKER_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
4546
SCCACHE_BUCKET: rust-lang-ci-sccache2
4647
TOOLSTATE_REPO: "https://github.com/rust-lang-nursery/rust-toolstate"
4748
CACHE_DOMAIN: ci-caches.rust-lang.org
@@ -50,19 +51,10 @@ jobs:
5051
strategy:
5152
matrix:
5253
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
54+
- name: quick
6055
env:
6156
ENABLE_GCC_CODEGEN: "1"
62-
os: ubuntu-20.04-16core-64gb
63-
- name: x86_64-gnu-tools
64-
os: ubuntu-20.04-16core-64gb
65-
env: {}
57+
os: ubuntu-20.04
6658
timeout-minutes: 600
6759
runs-on: "${{ matrix.os }}"
6860
steps:
@@ -168,10 +160,13 @@ jobs:
168160
if: "success() && !env.SKIP_JOB && (github.event_name == 'push' || env.DEPLOY == '1' || env.DEPLOY_ALT == '1')"
169161
auto:
170162
name: "auto - ${{ matrix.name }}"
163+
permissions:
164+
packages: write
171165
env:
172166
CI_JOB_NAME: "${{ matrix.name }}"
173167
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
174168
HEAD_SHA: "${{ github.event.pull_request.head.sha || github.sha }}"
169+
DOCKER_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
175170
SCCACHE_BUCKET: rust-lang-ci-sccache2
176171
DEPLOY_BUCKET: rust-lang-ci2
177172
TOOLSTATE_REPO: "https://github.com/rust-lang-nursery/rust-toolstate"
@@ -204,7 +199,7 @@ jobs:
204199
os: ubuntu-20.04-8core-32gb
205200
env: {}
206201
- name: dist-arm-linux
207-
os: ubuntu-20.04-16core-64gb
202+
os: ubuntu-20.04
208203
env: {}
209204
- name: dist-armhf-linux
210205
os: ubuntu-20.04-8core-32gb
@@ -251,12 +246,12 @@ jobs:
251246
- name: dist-x86_64-linux
252247
env:
253248
CODEGEN_BACKENDS: "llvm,cranelift"
254-
os: ubuntu-20.04-16core-64gb
249+
os: ubuntu-20.04
255250
- name: dist-x86_64-linux-alt
256251
env:
257252
IMAGE: dist-x86_64-linux
258253
CODEGEN_BACKENDS: "llvm,cranelift"
259-
os: ubuntu-20.04-16core-64gb
254+
os: ubuntu-20.04
260255
- name: dist-x86_64-musl
261256
env:
262257
CODEGEN_BACKENDS: "llvm,cranelift"
@@ -291,7 +286,7 @@ jobs:
291286
- name: x86_64-gnu-integration
292287
env:
293288
CI_ONLY_WHEN_CHANNEL: nightly
294-
os: ubuntu-20.04-16core-64gb
289+
os: ubuntu-20.04
295290
- name: x86_64-gnu-debug
296291
os: ubuntu-20.04-8core-32gb
297292
env: {}
@@ -561,11 +556,14 @@ jobs:
561556
if: "success() && !env.SKIP_JOB && (github.event_name == 'push' || env.DEPLOY == '1' || env.DEPLOY_ALT == '1')"
562557
try:
563558
name: "try - ${{ matrix.name }}"
559+
permissions:
560+
packages: write
564561
env:
565562
DIST_TRY_BUILD: 1
566563
CI_JOB_NAME: "${{ matrix.name }}"
567564
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse
568565
HEAD_SHA: "${{ github.event.pull_request.head.sha || github.sha }}"
566+
DOCKER_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
569567
SCCACHE_BUCKET: rust-lang-ci-sccache2
570568
DEPLOY_BUCKET: rust-lang-ci2
571569
TOOLSTATE_REPO: "https://github.com/rust-lang-nursery/rust-toolstate"
@@ -579,10 +577,10 @@ jobs:
579577
strategy:
580578
matrix:
581579
include:
582-
- name: dist-x86_64-linux
580+
- name: quick
583581
env:
584582
CODEGEN_BACKENDS: "llvm,cranelift"
585-
os: ubuntu-20.04-16core-64gb
583+
os: ubuntu-20.04
586584
timeout-minutes: 600
587585
runs-on: "${{ matrix.os }}"
588586
steps:
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM ubuntu:23.04
2+
3+
ARG DEBIAN_FRONTEND=noninteractive
4+
5+
RUN apt-get update && apt-get install -y --no-install-recommends \
6+
make \
7+
file \
8+
curl \
9+
ca-certificates \
10+
python3 \
11+
git && rm -rf /var/lib/apt/lists/*
12+
13+
# Note: libgccjit needs to match the default gcc version for the linker to find it.
14+
15+
16+
COPY scripts/sccache.sh /scripts/
17+
RUN sh /scripts/sccache.sh
18+
19+
# We are disabling CI LLVM since this builder is intentionally using a host
20+
# LLVM, rather than the typical src/llvm-project LLVM.
21+
ENV NO_DOWNLOAD_CI_LLVM 1
22+
23+
# This is not the latest LLVM version, so some components required by tests may
24+
# be missing.
25+
ENV IS_NOT_LATEST_LLVM 1
26+
27+
# Using llvm-link-shared due to libffi issues -- see #34486
28+
ENV RUST_CONFIGURE_ARGS \
29+
--build=x86_64-unknown-linux-gnu \
30+
--llvm-root=/usr/lib/llvm-16 \
31+
--enable-llvm-link-shared \
32+
--set rust.thin-lto-import-instr-limit=10
33+
34+
COPY host-x86_64/x86_64-gnu-llvm-16/script.sh /tmp/
35+
36+
ENV SCRIPT /tmp/script.sh

src/ci/docker/run.sh

+35-50
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,44 +84,48 @@ if [ -f "$docker_dir/$image/Dockerfile" ]; then
10384
context="$script_dir"
10485
fi
10586
echo "::group::Building docker image for $image"
106-
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"
87+
echo "Image checksum ${cksum}"
88+
89+
# On PR jobs, we don't have permissions to write to the cache, so we should not use
90+
# `docker login` nor caching.
91+
if [ "$PR_CI_JOB" -eq 1 ]
92+
then
93+
docker pull ghcr.io/rust-lang-ci/rust-ci:e933e07d88a3a99bf4260cfb60899ada91f8df72a6588179fcf65ebe7ce824675eb8f2c985515ca3c51f2d0f5c006cb1d9e2fa66af562cdc91537385af559d59
94+
# docker buildx create --use --driver docker-container
95+
# retry docker buildx build --rm -t rust-ci \
96+
# --output=type=docker \
97+
# --cache-from type=registry,ref=ghcr.io/rust-lang-ci/rust-ci:${cksum} \
98+
# -f "$dockerfile" "$context"
99+
else
100+
docker pull ghcr.io/rust-lang-ci/rust-ci:e933e07d88a3a99bf4260cfb60899ada91f8df72a6588179fcf65ebe7ce824675eb8f2c985515ca3c51f2d0f5c006cb1d9e2fa66af562cdc91537385af559d59
101+
102+
docker buildx create --use --driver docker-container
103+
104+
# Login to Docker registry
105+
echo ${DOCKER_TOKEN} | docker login ghcr.io --username rust-lang-ci --password-stdin
106+
107+
dest="type=registry,ref=ghcr.io/rust-lang-ci/rust-ci:${cksum},compression=zstd,mode=max"
108+
109+
retry docker \
110+
buildx \
111+
build \
112+
--rm \
113+
-t rust-ci \
114+
-f "$dockerfile" \
115+
--cache-from type=registry,ref=ghcr.io/rust-lang-ci/rust-ci:${cksum} \
116+
--cache-to ${dest} \
117+
--output=type=docker \
118+
"$context"
119+
docker manifest inspect rust-ci
120+
fi
118121
echo "::endgroup::"
119122

120123
if [ "$CI" != "" ]; then
121-
s3url="s3://$SCCACHE_BUCKET/docker/$cksum"
122-
upload="aws s3 cp - $s3url"
123124
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
139125
# Record the container image for reuse, e.g. by rustup.rs builds
140126
info="$dist/image-$image.txt"
141127
mkdir -p "$dist"
142-
echo "$url" >"$info"
143-
echo "$digest" >>"$info"
128+
echo "${cksum}" > "$info"
144129
cat "$info"
145130
fi
146131
elif [ -f "$docker_dir/disabled/$image/Dockerfile" ]; then

src/ci/github-actions/ci.yml

+8-12
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
@@ -84,7 +85,7 @@ x--expand-yaml-anchors--remove:
8485
<<: *base-job
8586

8687
- &job-linux-16c
87-
os: ubuntu-20.04-16core-64gb
88+
os: ubuntu-20.04
8889
<<: *base-job
8990

9091
- &job-macos-xl
@@ -328,23 +329,16 @@ jobs:
328329
strategy:
329330
matrix:
330331
include:
331-
- name: mingw-check
332-
<<: *job-linux-4c
333-
334-
- name: mingw-check-tidy
335-
<<: *job-linux-4c
336-
337-
- name: x86_64-gnu-llvm-16
332+
- name: quick
338333
env:
339334
ENABLE_GCC_CODEGEN: "1"
340335
<<: *job-linux-16c
341336

342-
- name: x86_64-gnu-tools
343-
<<: *job-linux-16c
344-
345337
auto:
346338
<<: *base-ci-job
347339
name: auto - ${{ matrix.name }}
340+
permissions:
341+
packages: write
348342
env:
349343
<<: [*shared-ci-variables, *prod-variables]
350344
if: github.event_name == 'push' && github.ref == 'refs/heads/auto' && github.repository == 'rust-lang-ci/rust'
@@ -725,6 +719,8 @@ jobs:
725719
try:
726720
<<: *base-ci-job
727721
name: try - ${{ matrix.name }}
722+
permissions:
723+
packages: write
728724
env:
729725
DIST_TRY_BUILD: 1
730726
<<: [*shared-ci-variables, *prod-variables]
@@ -733,7 +729,7 @@ jobs:
733729
matrix:
734730
include:
735731
- &dist-x86_64-linux
736-
name: dist-x86_64-linux
732+
name: quick
737733
env:
738734
CODEGEN_BACKENDS: llvm,cranelift
739735
<<: *job-linux-16c

0 commit comments

Comments
 (0)