Skip to content

Commit b4b8d1e

Browse files
committed
Multi platform build
1 parent 41398da commit b4b8d1e

4 files changed

Lines changed: 97 additions & 12 deletions

File tree

.dockerignore

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
.vscode
2-
.git
1+
# Ignore everything
2+
*
33

4-
target
4+
# Allow bin and src directories
5+
!/src
6+
!/bin
57

6-
.env
8+
# Allow cargo files
9+
!Cargo.toml
10+
!Cargo.lock
11+
12+
# Allow .sqlx directory
13+
!.sqlx
14+
15+
# Ignore unnecessary files inside allowed directories
16+
# This should go after the allowed directories
17+
**/*~
18+
**/*.log
19+
**/.DS_Store
20+
**/Thumbs.db

.github/workflows/docker.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ name: Build docker image
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches:
6+
- master
7+
- dev
68
tags:
79
- 'v*.*.*'
810
pull_request:
@@ -22,8 +24,9 @@ jobs:
2224
images: |
2325
ghcr.io/${{ github.repository }}
2426
tags: |
25-
type=semver,pattern={{raw}}
2627
type=raw,value=latest,enable={{is_default_branch}}
28+
type=ref,event=branch
29+
type=ref,event=tag
2730
- name: Set up Docker Buildx
2831
uses: docker/setup-buildx-action@v3
2932
- name: Login to GitHub Container Registry
@@ -36,7 +39,9 @@ jobs:
3639
id: docker_build
3740
uses: docker/build-push-action@v5
3841
with:
42+
file: Dockerfile.multi
3943
push: ${{ github.event_name != 'pull_request' }}
4044
tags: ${{ steps.metadata.outputs.tags }}
4145
cache-from: type=gha,scope=main
4246
cache-to: type=gha,mode=max,scope=main
47+
platforms: linux/amd64,linux/arm64

Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
FROM rust:1.84 as builder
1+
FROM rust:1.84 AS builder
22

3-
RUN mkdir -p /build/openworkers-runner
3+
RUN mkdir -p /build
44

55
ENV RUST_BACKTRACE=1
66
ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
77

8-
WORKDIR /build/openworkers-runner
8+
WORKDIR /build
99

10-
COPY . /build/openworkers-runner
10+
COPY . /build
1111

1212
RUN touch $RUNTIME_SNAPSHOT_PATH
1313

1414
RUN --mount=type=cache,target=$CARGO_HOME/git \
1515
--mount=type=cache,target=$CARGO_HOME/registry \
16-
--mount=type=cache,target=/build/openworkers-runner/target \
16+
--mount=type=cache,target=/build/target \
1717
cargo run --release --bin snapshot && \
1818
# Build the runner and copy executable out of the cache so it can be used in the next stage
19-
cargo build --release && cp /build/openworkers-runner/target/release/openworkers-runner /build/output
19+
cargo build --release && cp /build/target/release/openworkers-runner /build/output
2020

2121
FROM debian:bookworm-slim
2222

Dockerfile.multi

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.84 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+
FROM prepare AS builder
26+
27+
RUN mkdir -p /build
28+
29+
ENV RUST_BACKTRACE=1
30+
ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
31+
32+
WORKDIR /build
33+
34+
COPY . /build
35+
36+
RUN touch $RUNTIME_SNAPSHOT_PATH
37+
38+
RUN --mount=type=cache,sharing=locked,target=$CARGO_HOME/git \
39+
--mount=type=cache,sharing=locked,target=$CARGO_HOME/registry \
40+
--mount=type=cache,sharing=locked,target=/build/openworkers-runner/target \
41+
cargo run --release --bin snapshot
42+
43+
FROM builder AS platform
44+
45+
ARG TARGETPLATFORM
46+
47+
RUN echo "Building for $TARGETPLATFORM"
48+
49+
RUN --mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
50+
--mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
51+
--mount=type=cache,id=apt-$TARGETPLATFORM,sharing=locked,target=/build/openworkers-runner/target \
52+
case "$TARGETPLATFORM" in \
53+
"linux/arm64") cargo build --release --target aarch64-unknown-linux-gnu && \
54+
mv /build/target/aarch64-unknown-linux-gnu/release/openworkers-runner /build/output ;; \
55+
"linux/amd64") cargo build --release --target x86_64-unknown-linux-gnu && \
56+
mv /build/target/x86_64-unknown-linux-gnu/release/openworkers-runner /build/output ;; \
57+
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
58+
esac
59+
60+
FROM common AS runner
61+
62+
COPY --from=platform /build/output /usr/local/bin/openworkers-runner
63+
64+
CMD ["/usr/local/bin/openworkers-runner"]
65+
66+
EXPOSE 8080

0 commit comments

Comments
 (0)