Skip to content

Commit 18fb7af

Browse files
authored
perf(docker): move version ARG below cached layers to fix cache invalidation (#385)
The OPENSHELL_CARGO_VERSION build arg contains a git commit hash that changes on every build (e.g. 0.0.7-dev.11+g085b131ae). Declaring this ARG near the top of each Dockerfile invalidated every layer below it -- including expensive dependency installs, toolchain setup, and the Rust dependency pre-build step -- on every single commit. Move the ARG declaration to just before the RUN that actually uses it so upstream layers stay cached. This recovers ~5-6 minutes per build on the gateway image (from ~9m back toward ~2-3m) and similarly improves cluster, CLI, and Python wheel builds. Also removes unused OPENSHELL_IMAGE_TAG ARG from cli-macos, python-wheels, and python-wheels-macos Dockerfiles.
1 parent 48cb689 commit 18fb7af

5 files changed

Lines changed: 15 additions & 8 deletions

File tree

deploy/docker/Dockerfile.cli-macos

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ FROM ${OSXCROSS_IMAGE} AS osxcross
1818

1919
FROM python:3.12-slim AS builder
2020

21-
ARG OPENSHELL_CARGO_VERSION
22-
ARG OPENSHELL_IMAGE_TAG
2321
ARG CARGO_TARGET_CACHE_SCOPE=default
2422

2523
ENV PATH="/root/.cargo/bin:/usr/local/bin:/osxcross/bin:${PATH}"
@@ -105,6 +103,9 @@ RUN touch crates/openshell-cli/src/main.rs \
105103
crates/openshell-core/build.rs \
106104
proto/*.proto
107105

106+
# Declare version ARG here (not earlier) so the git-hash-bearing value does not
107+
# invalidate the expensive dependency-build layers above on every commit.
108+
ARG OPENSHELL_CARGO_VERSION
108109
RUN --mount=type=cache,id=cargo-registry-cli-macos,sharing=locked,target=/root/.cargo/registry \
109110
--mount=type=cache,id=cargo-git-cli-macos,sharing=locked,target=/root/.cargo/git \
110111
--mount=type=cache,id=cargo-target-cli-macos-${CARGO_TARGET_CACHE_SCOPE},sharing=locked,target=/build/target \

deploy/docker/Dockerfile.cluster

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certifi
7676
FROM --platform=$BUILDPLATFORM rust:1.88-slim AS supervisor-builder
7777
ARG TARGETARCH
7878
ARG BUILDARCH
79-
ARG OPENSHELL_CARGO_VERSION
8079
ARG CARGO_TARGET_CACHE_SCOPE=default
8180
ARG SCCACHE_MEMCACHED_ENDPOINT
8281

@@ -132,6 +131,9 @@ RUN touch crates/openshell-sandbox/src/main.rs \
132131
proto/*.proto
133132

134133
# Build the supervisor binary
134+
# Declare version ARG here (not earlier) so the git-hash-bearing value does not
135+
# invalidate the expensive dependency-build layers above on every commit.
136+
ARG OPENSHELL_CARGO_VERSION
135137
RUN --mount=type=cache,id=cargo-registry-supervisor-${TARGETARCH},sharing=locked,target=/usr/local/cargo/registry \
136138
--mount=type=cache,id=cargo-target-supervisor-${TARGETARCH}-${CARGO_TARGET_CACHE_SCOPE},sharing=locked,target=/build/target \
137139
--mount=type=cache,id=sccache-supervisor-${TARGETARCH},sharing=locked,target=/tmp/sccache \

deploy/docker/Dockerfile.gateway

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
FROM --platform=$BUILDPLATFORM rust:1.88-slim AS builder
1111
ARG TARGETARCH
1212
ARG BUILDARCH
13-
ARG OPENSHELL_CARGO_VERSION
1413
ARG CARGO_TARGET_CACHE_SCOPE=default
1514

1615
# Install build dependencies
@@ -68,6 +67,9 @@ RUN touch crates/openshell-server/src/main.rs \
6867
proto/*.proto
6968

7069
# Build the actual application
70+
# Declare version ARG here (not earlier) so the git-hash-bearing value does not
71+
# invalidate the expensive dependency-build layers above on every commit.
72+
ARG OPENSHELL_CARGO_VERSION
7173
RUN --mount=type=cache,id=cargo-registry-gateway-${TARGETARCH},sharing=locked,target=/usr/local/cargo/registry \
7274
--mount=type=cache,id=cargo-target-gateway-${TARGETARCH}-${CARGO_TARGET_CACHE_SCOPE},sharing=locked,target=/build/target \
7375
--mount=type=cache,id=sccache-gateway-${TARGETARCH},sharing=locked,target=/tmp/sccache \

deploy/docker/Dockerfile.python-wheels

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ FROM base AS builder
2828

2929
ARG TARGETARCH
3030
ARG BUILDARCH
31-
ARG OPENSHELL_CARGO_VERSION
32-
ARG OPENSHELL_IMAGE_TAG
3331
ARG CARGO_TARGET_CACHE_SCOPE=default
3432

3533
ARG SCCACHE_MEMCACHED_ENDPOINT
@@ -86,6 +84,9 @@ RUN touch crates/openshell-cli/src/main.rs \
8684
crates/openshell-core/build.rs \
8785
proto/*.proto
8886

87+
# Declare version ARG here (not earlier) so the git-hash-bearing value does not
88+
# invalidate the expensive dependency-build layers above on every commit.
89+
ARG OPENSHELL_CARGO_VERSION
8990
RUN --mount=type=cache,id=cargo-registry-python-wheels-${TARGETARCH},sharing=locked,target=/root/.cargo/registry \
9091
--mount=type=cache,id=cargo-git-python-wheels-${TARGETARCH},sharing=locked,target=/root/.cargo/git \
9192
--mount=type=cache,id=cargo-target-python-wheels-${TARGETARCH}-${CARGO_TARGET_CACHE_SCOPE},sharing=locked,target=/build/target \

deploy/docker/Dockerfile.python-wheels-macos

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ FROM ${OSXCROSS_IMAGE} AS osxcross
1212
FROM python:${PYTHON_VERSION}-slim AS builder
1313

1414
ARG TARGETARCH
15-
ARG OPENSHELL_CARGO_VERSION
16-
ARG OPENSHELL_IMAGE_TAG
1715
ARG CARGO_TARGET_CACHE_SCOPE=default
1816

1917
ENV PATH="/root/.cargo/bin:/usr/local/bin:/osxcross/bin:${PATH}"
@@ -93,6 +91,9 @@ RUN touch crates/openshell-cli/src/main.rs \
9391
crates/openshell-core/build.rs \
9492
proto/*.proto
9593

94+
# Declare version ARG here (not earlier) so the git-hash-bearing value does not
95+
# invalidate the expensive dependency-build layers above on every commit.
96+
ARG OPENSHELL_CARGO_VERSION
9697
RUN --mount=type=cache,id=cargo-registry-python-wheels-macos-${TARGETARCH},sharing=locked,target=/root/.cargo/registry \
9798
--mount=type=cache,id=cargo-git-python-wheels-macos-${TARGETARCH},sharing=locked,target=/root/.cargo/git \
9899
--mount=type=cache,id=cargo-target-python-wheels-macos-${TARGETARCH}-${CARGO_TARGET_CACHE_SCOPE},sharing=locked,target=/build/target \

0 commit comments

Comments
 (0)