Skip to content

Commit bd19161

Browse files
feat: Optimize Docker build for cross-platform compilation and caching
1 parent b749478 commit bd19161

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

.github/workflows/CI.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,23 @@ jobs:
3636
- uses: actions/checkout@v4
3737
- name: Set up Docker Buildx
3838
uses: docker/setup-buildx-action@v3
39+
- name: Set up Docker Buildx cache
40+
uses: actions/cache@v3
41+
with:
42+
path: /tmp/.buildx-cache
43+
key: ${{ runner.os }}-buildx-${{ github.sha }}
44+
restore-keys: |
45+
${{ runner.os }}-buildx-
3946
- name: Build and push Docker image
4047
uses: docker/build-push-action@v5
4148
with:
4249
platforms: linux/amd64,linux/arm64
4350
push: false
4451
tags: |
4552
cybuerg/cfspeedtest:${{ github.sha }}
53+
cache-from: type=local,src=/tmp/.buildx-cache
54+
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
55+
- name: Move cache
56+
run: |
57+
rm -rf /tmp/.buildx-cache
58+
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

.github/workflows/release.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ jobs:
5252
- uses: actions/checkout@v4
5353
- name: Set up Docker Buildx
5454
uses: docker/setup-buildx-action@v3
55+
- name: Set up Docker Buildx cache
56+
uses: actions/cache@v3
57+
with:
58+
path: /tmp/.buildx-cache
59+
key: ${{ runner.os }}-buildx-${{ github.sha }}
60+
restore-keys: |
61+
${{ runner.os }}-buildx-
5562
- name: Log in to DockerHub
5663
uses: docker/login-action@v3
5764
with:
@@ -65,3 +72,9 @@ jobs:
6572
tags: |
6673
cybuerg/cfspeedtest:${{ github.ref_name }}
6774
cybuerg/cfspeedtest:latest
75+
cache-from: type=local,src=/tmp/.buildx-cache
76+
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
77+
- name: Move cache
78+
run: |
79+
rm -rf /tmp/.buildx-cache
80+
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

Dockerfile

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
1-
FROM rust:slim-bullseye as builder
1+
FROM --platform=$BUILDPLATFORM rust:slim-bullseye as builder
2+
3+
# Install cross-compilation tools if needed
4+
ARG BUILDPLATFORM
5+
ARG TARGETPLATFORM
6+
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ] && [ "$BUILDPLATFORM" = "linux/amd64" ]; then \
7+
dpkg --add-architecture arm64 && \
8+
apt-get update && \
9+
apt-get install -y --no-install-recommends \
10+
gcc-aarch64-linux-gnu libc6-dev-arm64-cross && \
11+
rustup target add aarch64-unknown-linux-gnu && \
12+
rm -rf /var/lib/apt/lists/*; \
13+
fi
14+
15+
# Set the correct target
16+
ARG RUST_TARGET="x86_64-unknown-linux-gnu"
17+
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
18+
echo "RUST_TARGET=aarch64-unknown-linux-gnu"; \
19+
export RUST_TARGET="aarch64-unknown-linux-gnu"; \
20+
fi
21+
22+
# Create a new empty project for caching dependencies
223
WORKDIR /usr/src/cfspeedtest
324
COPY Cargo.toml Cargo.lock ./
25+
RUN mkdir -p src && \
26+
echo "fn main() {}" > src/main.rs && \
27+
echo "pub fn dummy() {}" > src/lib.rs && \
28+
cargo fetch
29+
30+
# Build the actual application
431
COPY src ./src
5-
RUN cargo install --path .
32+
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
33+
RUSTFLAGS="-C linker=aarch64-linux-gnu-gcc" \
34+
cargo build --release --target aarch64-unknown-linux-gnu && \
35+
cp target/aarch64-unknown-linux-gnu/release/cfspeedtest /usr/local/bin/; \
36+
else \
37+
cargo build --release && \
38+
cp target/release/cfspeedtest /usr/local/bin/; \
39+
fi
640

7-
FROM debian:bullseye-slim
41+
FROM --platform=$TARGETPLATFORM debian:bullseye-slim
842
RUN apt-get update && apt-get install -y --no-install-recommends tini && rm -rf /var/lib/apt/lists/*
9-
COPY --from=builder /usr/local/cargo/bin/cfspeedtest /usr/local/bin/cfspeedtest
43+
COPY --from=builder /usr/local/bin/cfspeedtest /usr/local/bin/cfspeedtest
1044

1145
# tini will be PID 1 and handle signal forwarding and process reaping
1246
ENTRYPOINT ["/usr/bin/tini", "--", "cfspeedtest"]

0 commit comments

Comments
 (0)