Skip to content

Commit 2c0c2ea

Browse files
committed
Add Dockerfile for multi-arch builds
1 parent 03bb21a commit 2c0c2ea

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

.github/workflows/docker.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Docker
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- "v*.*.*"
9+
pull_request:
10+
branches:
11+
- main
12+
13+
jobs:
14+
build:
15+
name: Build
16+
runs-on: ubuntu-24.04
17+
steps:
18+
- name: Check Out Repo
19+
uses: actions/checkout@v4
20+
21+
- name: Docker metadata
22+
id: metadata
23+
uses: docker/metadata-action@v5
24+
with:
25+
images: |
26+
ghcr.io/${{ github.repository }}
27+
tags: |
28+
type=raw,value=latest,enable={{is_default_branch}}
29+
type=ref,event=branch
30+
type=ref,event=tag
31+
32+
- name: Set up Docker Buildx
33+
uses: docker/setup-buildx-action@v3
34+
35+
- name: Login to GitHub Container Registry
36+
uses: docker/login-action@v3
37+
with:
38+
registry: ghcr.io
39+
username: ${{ github.repository_owner }}
40+
password: ${{ secrets.GITHUB_TOKEN }}
41+
42+
- name: Build CLI
43+
id: docker_build
44+
uses: docker/build-push-action@v5
45+
with:
46+
file: Dockerfile.multi
47+
push: ${{ github.event_name != 'pull_request' }}
48+
tags: ${{ steps.metadata.outputs.tags }}
49+
cache-from: type=gha
50+
cache-to: type=gha,mode=max
51+
platforms: linux/amd64,linux/arm64

Dockerfile.multi

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

0 commit comments

Comments
 (0)