Skip to content

Commit 6f11fe5

Browse files
committed
Use docker cache mounts for apt, pip and cargo
The cache mounts are cached using standard github actions cache when building in the CI pipeline. Note that the build stage no longer contains the whole source tree, these are instead mounted into the build container when building to avoid invalidating cached build container layers.
1 parent d517b1c commit 6f11fe5

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

.github/workflows/build-docker.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,48 @@ jobs:
7979
platform=${{ matrix.platform }}
8080
echo "PLATFORM_TUPLE=${platform//\//-}" >> $GITHUB_ENV
8181
82+
- name: Docker apt & pip caches
83+
uses: actions/cache@v4
84+
id: docker-caches
85+
with:
86+
path: |
87+
var-cache-apt
88+
var-lib-apt
89+
root-cache-pip
90+
key: docker-caches-${{ matrix.platform }}-${{ hashFiles('Dockerfile') }}
91+
92+
- name: Docker Cargo caches
93+
uses: actions/cache@v4
94+
id: docker-cargo-caches
95+
with:
96+
path: |
97+
root-target
98+
usr-local-cargo-git-db
99+
usr-local-cargo-registry
100+
key: docker-cargo-caches-${{ matrix.platform }}-${{ hashFiles('Dockerfile', 'crates/**', 'Cargo.toml', 'Cargo.lock') }}
101+
102+
- name: Inject apt & pip caches into docker
103+
uses: reproducible-containers/buildkit-cache-dance@v3
104+
with:
105+
cache-map: |
106+
{
107+
"var-cache-apt": "/var/cache/apt",
108+
"var-lib-apt": "/var/lib/apt",
109+
"root-cache-pip": "/root/.cache/pip"
110+
}
111+
skip-extraction: ${{ steps.docker-caches.outputs.cache-hit }}
112+
113+
- name: Inject Cargo caches into docker
114+
uses: reproducible-containers/buildkit-cache-dance@v3
115+
with:
116+
cache-map: |
117+
{
118+
"root-target": "/root/target",
119+
"usr-local-cargo-git-db": "/usr/local/cargo/git/db",
120+
"usr-local-cargo-registry": "/usr/local/cargo/registry/"
121+
}
122+
skip-extraction: ${{ steps.docker-cargo-caches.outputs.cache-hit }}
123+
82124
# Adapted from https://docs.docker.com/build/ci/github-actions/multi-platform/
83125
- name: Build and push by digest
84126
id: build
@@ -116,6 +158,8 @@ jobs:
116158
steps:
117159
# Login to DockerHub first, to avoid rate-limiting
118160
- uses: docker/login-action@v3
161+
# PRs from forks don't have access to secrets, disable this step in that case.
162+
if: ${{ github.event.pull_request.head.repo.full_name == 'astral-sh/uv' }}
119163
with:
120164
username: astralshbot
121165
password: ${{ secrets.DOCKERHUB_TOKEN_RO }}

Dockerfile

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ FROM --platform=$BUILDPLATFORM ubuntu AS build
22
ENV HOME="/root"
33
WORKDIR $HOME
44

5-
RUN apt update \
5+
RUN \
6+
--mount=type=cache,target=/var/cache/apt,sharing=locked \
7+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
8+
# remove the default docker-specific apt config that auto-deletes /var/apt/cache archives
9+
rm -f /etc/apt/apt.conf.d/docker-clean && \
10+
# and configure apt-get to keep downloaded archives in the cache
11+
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache && \
12+
apt update \
613
&& apt install -y --no-install-recommends \
714
build-essential \
815
curl \
916
python3-venv \
10-
cmake \
11-
&& apt clean \
12-
&& rm -rf /var/lib/apt/lists/*
17+
cmake
1318

1419
# Setup zig as cross compiling linker
1520
RUN python3 -m venv $HOME/.venv
16-
RUN .venv/bin/pip install cargo-zigbuild
21+
RUN \
22+
--mount=type=cache,target=/root/.cache/pip \
23+
.venv/bin/pip install cargo-zigbuild
1724
ENV PATH="$HOME/.venv/bin:$PATH"
1825

1926
# Install rust
@@ -32,10 +39,17 @@ ENV PATH="$HOME/.cargo/bin:$PATH"
3239
RUN rustup target add $(cat rust_target.txt)
3340

3441
# Build
35-
COPY crates crates
36-
COPY ./Cargo.toml Cargo.toml
37-
COPY ./Cargo.lock Cargo.lock
38-
RUN case "${TARGETPLATFORM}" in \
42+
RUN \
43+
# bind mounts to access Cargo config, lock, and sources, without having to
44+
# copy them into the build layer and so bloat the docker build cache
45+
--mount=type=bind,source=crates,target=crates \
46+
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
47+
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
48+
# Cache mounts to speed up builds
49+
--mount=type=cache,target=/root/target/ \
50+
--mount=type=cache,target=/usr/local/cargo/git/db \
51+
--mount=type=cache,target=/usr/local/cargo/registry/ \
52+
case "${TARGETPLATFORM}" in \
3953
"linux/arm64") export JEMALLOC_SYS_WITH_LG_PAGE=16;; \
4054
esac && \
4155
cargo zigbuild --bin uv --bin uvx --target $(cat rust_target.txt) --release

0 commit comments

Comments
 (0)