Skip to content

Commit 0561ff3

Browse files
authored
Thread sccache use into container build (#1632)
### What does this PR do? Now that we have cargo-chef going in our container build via PR #1630 thread the sccache introduced in PR #1629 through. The goal is to improve the speed of cold container builds.
1 parent 0472706 commit 0561ff3

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

.github/workflows/container.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ jobs:
2222
permissions:
2323
contents: read
2424
packages: write
25+
id-token: write # Required for OIDC authentication with AWS
2526

2627
steps:
2728
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
2829
with:
2930
sparse-checkout: .
3031

32+
- name: Configure AWS Credentials
33+
uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708 # v5.1.1
34+
with:
35+
role-to-assume: arn:aws:iam::850406765696:role/lading-ci-sccache-oidc
36+
aws-region: us-west-2
37+
3138
- name: Set up Docker Buildx
3239
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
3340

@@ -59,6 +66,13 @@ jobs:
5966
labels: ${{ steps.meta.outputs.labels }}
6067
cache-from: type=registry,ref=ghcr.io/datadog/lading:cache-${{ matrix.arch }}
6168
cache-to: type=registry,ref=ghcr.io/datadog/lading:cache-${{ matrix.arch }},mode=max
69+
build-args: |
70+
SCCACHE_BUCKET=lading-sccache
71+
SCCACHE_REGION=us-west-2
72+
secrets: |
73+
aws_access_key_id=${{ env.AWS_ACCESS_KEY_ID }}
74+
aws_secret_access_key=${{ env.AWS_SECRET_ACCESS_KEY }}
75+
aws_session_token=${{ env.AWS_SESSION_TOKEN }}
6276
6377
copy-to-ecr:
6478
name: Copy ${{ matrix.arch }} to ECR

Dockerfile

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,50 @@ RUN cargo chef prepare --recipe-path recipe.json
99

1010
# Stage 1: Cacher - Build dependencies only
1111
FROM docker.io/rust:1.90.0-slim-bookworm AS cacher
12+
ARG SCCACHE_BUCKET
13+
ARG SCCACHE_REGION
14+
ARG AWS_ACCESS_KEY_ID
15+
ARG AWS_SECRET_ACCESS_KEY
16+
ARG AWS_SESSION_TOKEN
17+
ENV CARGO_INCREMENTAL=0
1218
WORKDIR /app
1319
RUN apt-get update && apt-get install -y \
1420
pkg-config=1.8.1-1 \
1521
libssl-dev=3.0.17-1~deb12u3 \
1622
protobuf-compiler=3.21.12-3 \
1723
fuse3=3.14.0-4 \
1824
libfuse3-dev=3.14.0-4 \
25+
curl \
1926
&& rm -rf /var/lib/apt/lists/*
27+
# Download pre-built sccache binary
28+
RUN case "$(uname -m)" in \
29+
x86_64) ARCH=x86_64-unknown-linux-musl ;; \
30+
aarch64) ARCH=aarch64-unknown-linux-musl ;; \
31+
*) echo "Unsupported architecture" && exit 1 ;; \
32+
esac && \
33+
curl -L https://github.com/mozilla/sccache/releases/download/v0.8.2/sccache-v0.8.2-${ARCH}.tar.gz | tar xz && \
34+
mv sccache-v0.8.2-${ARCH}/sccache /usr/local/cargo/bin/ && \
35+
rm -rf sccache-v0.8.2-${ARCH}
2036
RUN cargo install cargo-chef --version 0.1.73
2137
COPY --from=planner /app/recipe.json recipe.json
2238
# This layer is cached until Cargo.toml/Cargo.lock change
23-
RUN cargo chef cook --release --locked --features logrotate_fs --recipe-path recipe.json
39+
# Use BuildKit secrets to pass AWS credentials securely (not exposed in image metadata)
40+
RUN --mount=type=secret,id=aws_access_key_id \
41+
--mount=type=secret,id=aws_secret_access_key \
42+
--mount=type=secret,id=aws_session_token \
43+
export AWS_ACCESS_KEY_ID=$(cat /run/secrets/aws_access_key_id) && \
44+
export AWS_SECRET_ACCESS_KEY=$(cat /run/secrets/aws_secret_access_key) && \
45+
export AWS_SESSION_TOKEN=$(cat /run/secrets/aws_session_token) && \
46+
export RUSTC_WRAPPER=sccache && \
47+
cargo chef cook --release --locked --features logrotate_fs --recipe-path recipe.json
2448

2549
# Stage 2: Builder - Build source code
2650
FROM docker.io/rust:1.90.0-slim-bookworm AS builder
51+
ARG SCCACHE_BUCKET
52+
ARG SCCACHE_REGION
53+
ENV CARGO_INCREMENTAL=0
54+
ENV SCCACHE_BUCKET=${SCCACHE_BUCKET}
55+
ENV SCCACHE_REGION=${SCCACHE_REGION}
2756
WORKDIR /app
2857
RUN apt-get update && apt-get install -y \
2958
pkg-config=1.8.1-1 \
@@ -32,13 +61,21 @@ RUN apt-get update && apt-get install -y \
3261
fuse3=3.14.0-4 \
3362
libfuse3-dev=3.14.0-4 \
3463
&& rm -rf /var/lib/apt/lists/*
35-
# Copy cached dependencies
64+
# Copy cached dependencies and sccache from cacher
3665
COPY --from=cacher /app/target target
3766
COPY --from=cacher /usr/local/cargo /usr/local/cargo
3867
# Copy source code (frequently changes)
3968
COPY . .
40-
# Build binary - reuses cached dependencies
41-
RUN cargo build --release --locked --bin lading --features logrotate_fs
69+
# Build binary - reuses cached dependencies + sccache
70+
# Use BuildKit secrets to pass AWS credentials securely (not exposed in image metadata)
71+
RUN --mount=type=secret,id=aws_access_key_id \
72+
--mount=type=secret,id=aws_secret_access_key \
73+
--mount=type=secret,id=aws_session_token \
74+
export AWS_ACCESS_KEY_ID=$(cat /run/secrets/aws_access_key_id) && \
75+
export AWS_SECRET_ACCESS_KEY=$(cat /run/secrets/aws_secret_access_key) && \
76+
export AWS_SESSION_TOKEN=$(cat /run/secrets/aws_session_token) && \
77+
export RUSTC_WRAPPER=sccache && \
78+
cargo build --release --locked --bin lading --features logrotate_fs
4279

4380
# Stage 3: Runtime
4481
FROM docker.io/debian:bookworm-20241202-slim

0 commit comments

Comments
 (0)