Skip to content

Commit 7418317

Browse files
authored
feat(docker): rewrite to cache with depot (#12491)
* chore: clean up build script * doc * feat(docker): rewrite to cache with depot * gha sucks * update id * runner
1 parent a02ee13 commit 7418317

File tree

5 files changed

+106
-129
lines changed

5 files changed

+106
-129
lines changed

.github/workflows/docker-publish.yml

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,47 @@ name: docker
33
permissions: {}
44

55
on:
6-
# Trigger without any parameters a proactive rebuild
7-
workflow_dispatch: {}
6+
workflow_dispatch:
7+
inputs:
8+
tag_name:
9+
default: nightly
10+
description: The tag we're building for
11+
type: string
812
workflow_call:
913
inputs:
1014
tag_name:
1115
required: true
1216
type: string
1317

18+
concurrency:
19+
group: docker-${{ github.head_ref }}
20+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
21+
1422
env:
1523
REGISTRY: ghcr.io
16-
# Will resolve to foundry-rs/foundry
1724
IMAGE_NAME: ${{ github.repository }}
1825

26+
# Keep in sync with `release.yml`.
27+
RUST_PROFILE: maxperf
28+
RUST_FEATURES: aws-kms,gcp-kms,cli,asm-keccak,js-tracer
29+
1930
jobs:
2031
build:
2132
name: build and push
22-
runs-on: depot-ubuntu-22.04-16
33+
runs-on: depot-ubuntu-latest
2334
permissions:
2435
contents: read
2536
id-token: write
2637
packages: write
27-
timeout-minutes: 120
38+
timeout-minutes: 60
2839
steps:
2940
- uses: actions/checkout@v5
3041
with:
3142
persist-credentials: false
32-
- uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # master
33-
with:
34-
toolchain: stable
35-
- uses: rui314/setup-mold@725a8794d15fc7563f59595bd9556495c0564878 # v1
36-
- name: Install gcc aarch64
37-
id: aarch_64_setup
38-
run: |
39-
sudo apt update && sudo apt install -y gcc-aarch64-linux-gnu
43+
4044
# Login against a Docker registry except on PR
4145
# https://github.com/docker/login-action
4246
- name: Login into registry ${{ env.REGISTRY }}
43-
# Ensure this doesn't trigger on PR's
44-
if: github.event_name != 'pull_request'
4547
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
4648
with:
4749
registry: ${{ env.REGISTRY }}
@@ -80,5 +82,20 @@ jobs:
8082
printf "TAGS -> %s\n" "${{ steps.docker_tagging.outputs.docker_tags }}"
8183
printf "LABELS -> %s\n" "${{ steps.meta.outputs.labels }}"
8284
83-
- name: Build and push foundry image
84-
run: make DOCKER_IMAGE_NAME=${{ steps.docker_tagging.outputs.docker_tags }} CARGO_TAG_NAME=${{ inputs.tag_name }} PROFILE=maxperf docker-build-push
85+
- name: Set up Depot CLI
86+
uses: depot/setup-action@b0b1ea4f69e92ebf5dea3f8713a1b0c37b2126a5 # v1
87+
88+
- name: Build and push Foundry image
89+
uses: depot/build-push-action@9785b135c3c76c33db102e45be96a25ab55cd507 # v1
90+
with:
91+
build-args: |
92+
RUST_PROFILE=${{ env.RUST_PROFILE }}
93+
RUST_FEATURES=${{ env.RUST_FEATURES }}
94+
TAG_NAME=${{ inputs.tag_name }}
95+
VERGEN_GIT_SHA=${{ github.sha }}
96+
project: 8gkbxxjrpw
97+
context: .
98+
tags: ${{ steps.docker_tagging.outputs.docker_tags }}
99+
labels: ${{ steps.meta.outputs.labels }}
100+
platforms: linux/amd64,linux/arm64
101+
push: true

.github/workflows/release.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ on:
1616
env:
1717
CARGO_TERM_COLOR: always
1818
IS_NIGHTLY: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
19-
PROFILE: maxperf
20-
STABLE_VERSION: "v1.4.3"
19+
20+
# Keep in sync with `docker-publish.yml`.
21+
RUST_PROFILE: maxperf
22+
RUST_FEATURES: aws-kms,gcp-kms,cli,asm-keccak,js-tracer
23+
24+
LAST_STABLE_VERSION: "v1.4.4"
2125

2226
jobs:
2327
prepare:
@@ -171,12 +175,12 @@ jobs:
171175
SVM_TARGET_PLATFORM: ${{ matrix.svm_target_platform }}
172176
PLATFORM_NAME: ${{ matrix.platform }}
173177
TARGET: ${{ matrix.target }}
174-
OUT_DIR: target/${{ matrix.target }}/${{ env.PROFILE }}
178+
OUT_DIR: target/${{ matrix.target }}/${{ env.RUST_PROFILE }}
175179
shell: bash
176180
run: |
177181
set -eo pipefail
178-
flags=(--target $TARGET --profile $PROFILE --bins
179-
--no-default-features --features aws-kms,gcp-kms,cli,asm-keccak,js-tracer)
182+
flags=(--target $TARGET --profile $RUST_PROFILE --bins
183+
--no-default-features --features "$RUST_FEATURES")
180184
181185
# `jemalloc` is not fully supported on MSVC or aarch64 Linux.
182186
if [[ "$TARGET" != *msvc* && "$TARGET" != "aarch64-unknown-linux-gnu" ]]; then
@@ -206,7 +210,7 @@ jobs:
206210
id: artifacts
207211
env:
208212
PLATFORM_NAME: ${{ matrix.platform }}
209-
OUT_DIR: target/${{ matrix.target }}/${{ env.PROFILE }}
213+
OUT_DIR: target/${{ matrix.target }}/${{ env.RUST_PROFILE }}
210214
VERSION_NAME: ${{ (env.IS_NIGHTLY == 'true' && 'nightly') || needs.prepare.outputs.tag_name }}
211215
ARCH: ${{ matrix.arch }}
212216
shell: bash
@@ -238,7 +242,7 @@ jobs:
238242
id: man
239243
if: matrix.target == 'x86_64-unknown-linux-gnu'
240244
env:
241-
OUT_DIR: target/${{ matrix.target }}/${{ env.PROFILE }}
245+
OUT_DIR: target/${{ matrix.target }}/${{ env.RUST_PROFILE }}
242246
VERSION_NAME: ${{ (env.IS_NIGHTLY == 'true' && 'nightly') || needs.prepare.outputs.tag_name }}
243247
shell: bash
244248
run: |

Dockerfile

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,67 @@
1-
# syntax=docker/dockerfile:1.4
1+
# syntax=docker/dockerfile:1
22

3-
FROM alpine:3.22 AS build-environment
3+
FROM rust:1-bookworm AS chef
4+
WORKDIR /app
45

5-
ARG TARGETARCH
6-
WORKDIR /opt
6+
RUN apt update && apt install -y build-essential libssl-dev git pkg-config curl perl
7+
RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | sh
8+
RUN cargo binstall cargo-chef sccache
79

8-
RUN apk add clang lld curl build-base linux-headers git \
9-
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh \
10-
&& chmod +x ./rustup.sh \
11-
&& ./rustup.sh -y
10+
# Prepare the cargo-chef recipe.
11+
FROM chef AS planner
12+
COPY . .
13+
RUN cargo chef prepare --recipe-path recipe.json
14+
15+
# Build the project.
16+
FROM chef AS builder
17+
COPY --from=planner /app/recipe.json recipe.json
18+
19+
ARG RUST_PROFILE
20+
ARG RUST_FEATURES
21+
22+
ENV CARGO_INCREMENTAL=0 \
23+
RUSTC_WRAPPER=sccache \
24+
SCCACHE_DIR=/sccache
25+
26+
# Build dependencies.
27+
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
28+
--mount=type=cache,target=/usr/local/cargo/git,sharing=locked \
29+
--mount=type=cache,target=$SCCACHE_DIR,sharing=locked \
30+
cargo chef cook --recipe-path recipe.json --profile ${RUST_PROFILE} --no-default-features --features "${RUST_FEATURES}"
1231

13-
RUN [[ "$TARGETARCH" = "arm64" ]] && echo "export CFLAGS=-mno-outline-atomics" >> $HOME/.profile || true
32+
ARG TAG_NAME="dev"
33+
ENV TAG_NAME=$TAG_NAME
34+
ARG VERGEN_GIT_SHA="ffffffffffffffffffffffffffffffffffffffff"
1435

15-
WORKDIR /opt/foundry
36+
# Build the project.
1637
COPY . .
38+
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
39+
--mount=type=cache,target=/usr/local/cargo/git,sharing=locked \
40+
--mount=type=cache,target=$SCCACHE_DIR,sharing=locked \
41+
cargo build --profile ${RUST_PROFILE} --no-default-features --features "${RUST_FEATURES}"
1742

18-
# see <https://github.com/foundry-rs/foundry/issues/7925>
19-
RUN git update-index --force-write-index
43+
# `dev` profile outputs to the `target/debug` directory.
44+
RUN ln -s /app/target/debug /app/target/dev \
45+
&& mkdir -p /app/output \
46+
&& mv \
47+
/app/target/${RUST_PROFILE}/forge \
48+
/app/target/${RUST_PROFILE}/cast \
49+
/app/target/${RUST_PROFILE}/anvil \
50+
/app/target/${RUST_PROFILE}/chisel \
51+
/app/output/
2052

21-
RUN --mount=type=cache,target=/root/.cargo/registry --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/opt/foundry/target \
22-
source $HOME/.profile && cargo build --release --features anvil/js-tracer,cast/aws-kms,cast/gcp-kms,cast/turnkey,forge/aws-kms,forge/gcp-kms,forge/turnkey \
23-
&& mkdir out \
24-
&& mv target/release/forge out/forge \
25-
&& mv target/release/cast out/cast \
26-
&& mv target/release/anvil out/anvil \
27-
&& mv target/release/chisel out/chisel \
28-
&& strip out/forge \
29-
&& strip out/cast \
30-
&& strip out/chisel \
31-
&& strip out/anvil;
53+
RUN sccache --show-stats || true
3254

33-
FROM alpine:3.22 AS foundry-client
55+
FROM ubuntu:22.04 AS runtime
3456

35-
RUN apk add --no-cache linux-headers git gcompat libstdc++
57+
# Install runtime dependencies.
58+
RUN apt update && apt install -y git
3659

37-
COPY --from=build-environment /opt/foundry/out/forge /usr/local/bin/forge
38-
COPY --from=build-environment /opt/foundry/out/cast /usr/local/bin/cast
39-
COPY --from=build-environment /opt/foundry/out/anvil /usr/local/bin/anvil
40-
COPY --from=build-environment /opt/foundry/out/chisel /usr/local/bin/chisel
60+
COPY --from=builder /app/output/* /usr/local/bin/
4161

42-
RUN adduser -Du 1000 foundry
62+
RUN groupadd -g 1000 foundry && \
63+
useradd -m -u 1000 -g foundry foundry
64+
USER foundry
4365

4466
ENTRYPOINT ["/bin/sh", "-c"]
4567

Dockerfile.cross

Lines changed: 0 additions & 28 deletions
This file was deleted.

Makefile

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,59 +32,21 @@ help: ## Display this help.
3232
build: ## Build the project.
3333
cargo build --features "$(FEATURES)" --profile "$(PROFILE)"
3434

35-
# The following commands use `cross` to build a cross-compile.
36-
#
37-
# These commands require that:
38-
#
39-
# - `cross` is installed (`cargo install cross`).
40-
# - Docker is running.
41-
# - The current user is in the `docker` group.
42-
#
43-
# The resulting binaries will be created in the `target/` directory.
44-
build-%:
45-
cross build --target $* --features "$(FEATURES)" --profile "$(PROFILE)"
46-
47-
.PHONY: docker-build-push
48-
docker-build-push: docker-build-prepare ## Build and push a cross-arch Docker image tagged with DOCKER_IMAGE_NAME.
49-
# Build x86_64-unknown-linux-gnu.
50-
cargo build --target x86_64-unknown-linux-gnu --features "jemalloc aws-kms gcp-kms turnkey cli asm-keccak js-tracer" --profile "$(PROFILE)"
51-
mkdir -p $(BIN_DIR)/amd64
52-
for bin in anvil cast chisel forge; do \
53-
cp $(CARGO_TARGET_DIR)/x86_64-unknown-linux-gnu/$(PROFILE)/$$bin $(BIN_DIR)/amd64/; \
54-
done
55-
56-
# Build aarch64-unknown-linux-gnu.
57-
rustup target add aarch64-unknown-linux-gnu
58-
RUSTFLAGS="-C linker=aarch64-linux-gnu-gcc" cargo build --target aarch64-unknown-linux-gnu --features "aws-kms gcp-kms turnkey cli asm-keccak js-tracer" --profile "$(PROFILE)"
59-
mkdir -p $(BIN_DIR)/arm64
60-
for bin in anvil cast chisel forge; do \
61-
cp $(CARGO_TARGET_DIR)/aarch64-unknown-linux-gnu/$(PROFILE)/$$bin $(BIN_DIR)/arm64/; \
62-
done
63-
64-
docker buildx build --file ./Dockerfile.cross . \
65-
--platform linux/amd64,linux/arm64 \
66-
$(foreach tag,$(shell echo $(DOCKER_IMAGE_NAME) | tr ',' ' '),--tag $(tag)) \
67-
--provenance=true \
68-
--push
69-
70-
.PHONY: docker-build-prepare
71-
docker-build-prepare: ## Prepare the Docker build environment.
72-
docker run --privileged --rm tonistiigi/binfmt:qemu-v7.0.0-28 --install amd64,arm64
73-
@if ! docker buildx inspect cross-builder &> /dev/null; then \
74-
echo "Creating a new buildx builder instance"; \
75-
docker buildx create --use --driver docker-container --name cross-builder; \
76-
else \
77-
echo "Using existing buildx builder instance"; \
78-
docker buildx use cross-builder; \
79-
fi
35+
.PHONY: build-docker
36+
build-docker: ## Build the docker image.
37+
docker build . -t "$(DOCKER_IMAGE_NAME)" \
38+
--build-arg "RUST_PROFILE=$(PROFILE)" \
39+
--build-arg "RUST_FEATURES=$(FEATURES)" \
40+
--build-arg "TAG_NAME=dev" \
41+
--build-arg "VERGEN_GIT_SHA=$(shell git rev-parse HEAD)"
8042

8143
##@ Test
8244

8345
## Run unit/doc tests and generate html coverage report in `target/llvm-cov/html` folder.
84-
## Notice that `llvm-cov` supports doc tests only in nightly builds because the `--doc` flag
46+
## Notice that `llvm-cov` supports doc tests only in nightly builds because the `--doc` flag
8547
## is unstable (https://github.com/taiki-e/cargo-llvm-cov/issues/2).
8648
.PHONY: test-coverage
87-
test-coverage:
49+
test-coverage:
8850
cargo +nightly llvm-cov --no-report nextest -E 'kind(test) & !test(/\b(issue|ext_integration)/)' && \
8951
cargo +nightly llvm-cov --no-report --doc && \
9052
cargo +nightly llvm-cov report --doctests --open

0 commit comments

Comments
 (0)