Skip to content

Commit 0bd24c8

Browse files
committed
build: switch to using cargo-chef and BuildKit for building Docker image
Also, upgrade Node.js installation method to one not deprecated.
1 parent d40fc66 commit 0bd24c8

3 files changed

Lines changed: 45 additions & 45 deletions

File tree

.dockerignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bindings
2+
build
3+
node_modules
4+
target
5+
Dockerfile
6+
.dockerignore
7+
.env

.github/workflows/workflow.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ jobs:
66
build:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v3
10-
11-
- name: Build Docker image
12-
run: docker build -t qcext-server .
9+
- uses: actions/checkout@v4
10+
11+
- uses: docker/setup-buildx-action@v3
12+
- uses: docker/build-push-action@v5
13+
with:
14+
context: .
15+
cache-from: type=gha
16+
cache-to: type=gha,mode=max
17+
load: true
18+
tags: qcext-server:latest
1319

1420
- name: Save Docker image
1521
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'

Dockerfile

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,25 @@
22
# Rust image #
33
##################
44

5-
FROM rust:1-bookworm AS rust
6-
5+
FROM rust:1-bookworm AS chef
76
WORKDIR /usr/src/qcext-server
7+
RUN cargo install cargo-chef
8+
9+
FROM chef AS planner
10+
COPY . .
11+
RUN cargo chef prepare --recipe-path recipe.json
812

13+
FROM chef AS rust
914
# Run SQLX in offline mode
1015
ENV SQLX_OFFLINE=true
1116

12-
# Use spare registry protocol
13-
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
14-
15-
# Build the dependencies in a separate step to avoid rebuilding all of them
16-
# every time the source code changes. This takes advantage of Docker's layer
17-
# caching, and it works by doing a build using the Cargo.{toml,lock} files with
18-
# dummy source code.
19-
COPY Cargo.lock Cargo.toml /usr/src/qcext-server/
20-
RUN mkdir -p /usr/src/qcext-server/database
21-
COPY database/Cargo.toml database/sqlx-data.json /usr/src/qcext-server/database/
22-
RUN mkdir -p /usr/src/qcext-server/shared
23-
COPY shared/Cargo.toml /usr/src/qcext-server/shared/
24-
RUN mkdir -p /usr/src/qcext-server/src && \
25-
echo "fn main() {}" > /usr/src/qcext-server/src/main.rs
26-
RUN mkdir -p /usr/src/qcext-server/database/src && \
27-
touch /usr/src/qcext-server/database/src/lib.rs
28-
RUN mkdir -p /usr/src/qcext-server/shared/src && \
29-
touch /usr/src/qcext-server/shared/src/lib.rs
30-
RUN cargo fetch
31-
RUN cargo build --release
17+
# Build dependencies - this is the caching Docker layer!
18+
COPY --from=planner /usr/src/qcext-server/recipe.json recipe.json
19+
RUN cargo chef cook --release --recipe-path recipe.json
3220

33-
# Dependencies are now cached, copy the actual source code and do another full
34-
# build. The touch on all the .rs files is needed, otherwise cargo assumes the
35-
# source code didn't change thanks to mtime weirdness.
36-
RUN rm -rf /usr/src/qcext-server/src /usr/src/qcext-server/database/src /usr/src/qcext-server/shared/src
37-
COPY src /usr/src/qcext-server/src
38-
RUN rm -rf /usr/src/qcext-server/src/client
39-
COPY database/src /usr/src/qcext-server/database/src
40-
COPY database/migrations /usr/src/qcext-server/database/migrations
41-
COPY shared/src /usr/src/qcext-server/shared/src
42-
RUN find src -name "*.rs" -exec touch {} \; && \
43-
find database/src -name "*.rs" -exec touch {} \; && \
44-
find shared/src -name "*.rs" -exec touch {} \; && \
45-
cargo build --release
21+
# Dependencies are now cached, build for real.
22+
COPY . .
23+
RUN cargo build --release
4624

4725
##################
4826
# NodeJS image #
@@ -52,13 +30,22 @@ FROM debian:bookworm AS nodejs
5230

5331
WORKDIR /usr/src/qcext-server
5432

33+
# Download and import the Nodesource GPG key
34+
RUN apt-get update && \
35+
apt-get install -y ca-certificates curl gnupg && \
36+
apt-get clean
37+
RUN mkdir -p /etc/apt/keyrings && \
38+
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | \
39+
gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
40+
5541
# Make sure we have npm and nodejs
56-
RUN apt-get update
57-
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y curl
58-
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
59-
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs
60-
RUN node --version
61-
RUN npm --version
42+
ENV NODE_MAJOR=18
43+
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
44+
RUN DEBIAN_FRONTEND=noninteractive \
45+
apt-get update && \
46+
apt-get install nodejs -y && \
47+
apt-get clean
48+
6249

6350
# Next, let's run npm install
6451
COPY package.json package-lock.json /usr/src/qcext-server/

0 commit comments

Comments
 (0)