Skip to content

Commit 8af9377

Browse files
committed
Add cargo-chef and multi-platform native builds
1 parent becff4b commit 8af9377

5 files changed

Lines changed: 92 additions & 8 deletions

File tree

.github/workflows/docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
id: docker_build
3939
uses: docker/build-push-action@v5
4040
with:
41-
file: Dockerfile
41+
file: Dockerfile.multi
4242
push: ${{ github.event_name != 'pull_request' }}
4343
tags: ${{ steps.metadata.outputs.tags }}
4444
cache-from: type=gha,scope=main

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openworkers-logs"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55

66
[dependencies]

Dockerfile

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
FROM rust:1.91 AS builder
1+
FROM rust:1.91 AS chef
2+
RUN cargo install cargo-chef
3+
WORKDIR /build
24

3-
RUN mkdir -p /build
5+
FROM chef AS planner
6+
COPY . .
7+
RUN cargo chef prepare --recipe-path recipe.json
48

9+
FROM chef AS builder
510
ENV RUST_BACKTRACE=1
611
ENV SQLX_OFFLINE=true
712

8-
WORKDIR /build
9-
10-
COPY . /build
13+
COPY --from=planner /build/recipe.json recipe.json
14+
# Build dependencies - this is the caching Docker layer!
15+
RUN --mount=type=cache,target=$CARGO_HOME/git \
16+
--mount=type=cache,target=$CARGO_HOME/registry \
17+
--mount=type=cache,target=/build/target \
18+
cargo chef cook --release --recipe-path recipe.json
1119

20+
# Build application
21+
COPY . .
1222
RUN --mount=type=cache,target=$CARGO_HOME/git \
1323
--mount=type=cache,target=$CARGO_HOME/registry \
1424
--mount=type=cache,target=/build/target \

Dockerfile.multi

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
FROM debian:bookworm-slim AS common
2+
3+
RUN apt-get update \
4+
# Install ca-certificates and wget (used for healthcheck)
5+
&& apt-get install -y ca-certificates wget \
6+
&& rm -rf /var/lib/apt/lists/*
7+
8+
FROM --platform=$BUILDPLATFORM rust:1.91 AS prepare
9+
10+
RUN apt-get update && apt-get install -y \
11+
gcc-aarch64-linux-gnu \
12+
gcc-x86-64-linux-gnu \
13+
libc6-dev-amd64-cross \
14+
libc6-dev-arm64-cross
15+
16+
ENV CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc
17+
ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
18+
19+
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
20+
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
21+
22+
RUN rustup target add x86_64-unknown-linux-gnu \
23+
aarch64-unknown-linux-gnu
24+
25+
RUN cargo install cargo-chef
26+
27+
FROM prepare AS planner
28+
29+
WORKDIR /build
30+
COPY . .
31+
RUN cargo chef prepare --recipe-path recipe.json
32+
33+
FROM prepare AS builder
34+
35+
WORKDIR /build
36+
37+
ENV RUST_BACKTRACE=1
38+
ENV SQLX_OFFLINE=true
39+
40+
COPY --from=planner /build/recipe.json recipe.json
41+
42+
ARG TARGETPLATFORM
43+
44+
# Build dependencies for the target platform
45+
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
46+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
47+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
48+
case "$TARGETPLATFORM" in \
49+
"linux/arm64") cargo chef cook --release --target aarch64-unknown-linux-gnu --recipe-path recipe.json ;; \
50+
"linux/amd64") cargo chef cook --release --target x86_64-unknown-linux-gnu --recipe-path recipe.json ;; \
51+
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
52+
esac
53+
54+
# Build application
55+
COPY . .
56+
57+
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
58+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
59+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
60+
case "$TARGETPLATFORM" in \
61+
"linux/arm64") cargo build --release --target aarch64-unknown-linux-gnu && \
62+
cp /build/target/aarch64-unknown-linux-gnu/release/openworkers-logs /build/output ;; \
63+
"linux/amd64") cargo build --release --target x86_64-unknown-linux-gnu && \
64+
cp /build/target/x86_64-unknown-linux-gnu/release/openworkers-logs /build/output ;; \
65+
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
66+
esac
67+
68+
FROM common AS runner
69+
70+
COPY --from=builder /build/output /usr/local/bin/openworkers-logs
71+
72+
CMD ["/usr/local/bin/openworkers-logs"]
73+
74+
EXPOSE 8080

0 commit comments

Comments
 (0)