Skip to content

Commit 08949b7

Browse files
committed
ci: add Dockerfile and GitHub Actions workflow
1 parent 17461fc commit 08949b7

5 files changed

Lines changed: 168 additions & 0 deletions

File tree

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target/
2+
.git/
3+
.github/
4+
.env
5+
.env.*
6+
*.md

.github/workflows/docker.yml

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

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cSpell.words": ["openworkers"]
3+
}

Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM rust:1.91-bookworm AS chef
2+
RUN cargo install cargo-chef
3+
WORKDIR /build
4+
5+
FROM chef AS planner
6+
COPY . .
7+
RUN cargo chef prepare --recipe-path recipe.json
8+
9+
FROM chef AS builder
10+
11+
ENV FEATURES=nats,database
12+
ENV RUST_BACKTRACE=1
13+
ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
14+
15+
RUN touch $RUNTIME_SNAPSHOT_PATH
16+
17+
COPY --from=planner /build/recipe.json recipe.json
18+
19+
RUN --mount=type=cache,target=$CARGO_HOME/git \
20+
--mount=type=cache,target=$CARGO_HOME/registry \
21+
--mount=type=cache,target=/build/target \
22+
cargo chef cook --release --features=$FEATURES --recipe-path recipe.json
23+
24+
COPY . .
25+
26+
RUN --mount=type=cache,target=$CARGO_HOME/git \
27+
--mount=type=cache,target=$CARGO_HOME/registry \
28+
--mount=type=cache,target=/build/target \
29+
cargo build --release --features=$FEATURES && \
30+
cp /build/target/release/task-executor /build/output
31+
32+
FROM debian:bookworm-slim
33+
34+
RUN apt-get update \
35+
&& apt-get install -y ca-certificates \
36+
&& rm -rf /var/lib/apt/lists/*
37+
38+
COPY --from=builder /build/output /usr/local/bin/task-executor
39+
40+
ENTRYPOINT ["/usr/local/bin/task-executor"]

Dockerfile.multi

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
qemu-user-static
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 platform
34+
35+
ARG TARGETPLATFORM
36+
37+
ENV FEATURES=nats,database
38+
ENV RUST_BACKTRACE=1
39+
ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin
40+
41+
WORKDIR /build
42+
43+
RUN touch $RUNTIME_SNAPSHOT_PATH
44+
45+
COPY --from=planner /build/recipe.json recipe.json
46+
47+
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
48+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
49+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
50+
case "$TARGETPLATFORM" in \
51+
"linux/arm64") cargo chef cook --release --features=$FEATURES --target aarch64-unknown-linux-gnu --recipe-path recipe.json ;; \
52+
"linux/amd64") cargo chef cook --release --features=$FEATURES --target x86_64-unknown-linux-gnu --recipe-path recipe.json ;; \
53+
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
54+
esac
55+
56+
COPY . /build
57+
58+
RUN --mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/git \
59+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=$CARGO_HOME/registry \
60+
--mount=type=cache,id=cargo-$TARGETPLATFORM,sharing=locked,target=/build/target \
61+
case "$TARGETPLATFORM" in \
62+
"linux/arm64") \
63+
cargo build --release --features=$FEATURES --target aarch64-unknown-linux-gnu && \
64+
mv /build/target/aarch64-unknown-linux-gnu/release/task-executor /build/output ;; \
65+
"linux/amd64") \
66+
cargo build --release --features=$FEATURES --target x86_64-unknown-linux-gnu && \
67+
mv /build/target/x86_64-unknown-linux-gnu/release/task-executor /build/output ;; \
68+
*) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \
69+
esac
70+
71+
FROM common
72+
73+
COPY --from=platform /build/output /usr/local/bin/task-executor
74+
75+
ENTRYPOINT ["/usr/local/bin/task-executor"]

0 commit comments

Comments
 (0)