Skip to content

Commit 2770f8b

Browse files
feat: pre-build Cargo dependencies to speed up serverless function compilation (#99)
* feat: pre-build Cargo dependencies to speed up serverless function compilation * chore: revert bullshit * feat: pre-build crates to same target * fix: getrandom_backend custom * fix: path * feat: build only satellite and sputnik * fix: inlcudes * fix: pin * feat: a pity * docs: todo * feat: try to improve rebuild * docs: update comment
1 parent f58b9bf commit 2770f8b

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

Dockerfile.full

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt update && apt install -y \
88
bash \
99
curl \
1010
build-essential \
11+
jq \
1112
&& rm -rf /var/lib/apt/lists/*
1213

1314
# Create a working user
@@ -44,6 +45,11 @@ COPY --chown=apprunner:apprunner ./kit/setup/bootstrap ./kit/setup/bootstrap
4445
# Install core tools required for building WASM (same as in the main Juno repository)
4546
RUN ./kit/setup/bootstrap
4647

48+
# Pre-build Cargo dependencies to speed up serverless function compilation when used in the actions
49+
COPY --chown=apprunner:apprunner ./kit/deps /home/apprunner/deps
50+
COPY --chown=apprunner:apprunner ./kit/setup/prebuild ./kit/setup/prebuild
51+
RUN ./kit/setup/prebuild
52+
4753
# --- Runtime Stage ---
4854
FROM node:24-slim@sha256:76d0ed0ed93bed4f4376211e9d8fddac4d8b3fbdb54cc45955696001a3c91152
4955

@@ -100,6 +106,12 @@ COPY --from=builder --chown=apprunner:apprunner /juno/target/juno-main/Cargo.loc
100106
COPY --from=builder --chown=apprunner:apprunner /juno/target/juno-main/Cargo.toml /juno/Cargo.toml
101107
COPY --from=builder --chown=apprunner:apprunner /juno/target/juno-main/rust-toolchain.toml /juno/rust-toolchain.toml
102108

109+
# Copy pre-built Cargo dependencies to avoid recompiling them on each CI run
110+
# Particularly useful for Sputnik since its version is always the one bundled
111+
# in the image
112+
COPY --from=builder --chown=apprunner:apprunner /juno/target /juno/target
113+
COPY --from=builder --chown=apprunner:apprunner /home/apprunner/.cargo/registry /home/apprunner/.cargo/registry
114+
103115
# Resolves cargo build error:
104116
# network failure seems to have happened
105117
# if a proxy or similar is necessary `net.git-fetch-with-cli` may help here

kit/deps/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Dummy crate to pre-download and build the latest published junobuild-* dependencies into the
2+
# Cargo target cache, so developers don't have to download them on each CI run.
3+
[package]
4+
name = "deps"
5+
version = "0.0.1"
6+
edition = "2021"
7+
8+
[lib]
9+
10+
[dependencies]
11+
candid = "0.10.20"
12+
ic-cdk = "0.19.0"
13+
ic-cdk-macros = "0.19.0"
14+
serde = "1.0.225"
15+
junobuild-satellite = { version = "0.6.0", default-features = false }
16+
junobuild-macros = "0.4.0"
17+
junobuild-utils = "0.4.0"
18+
junobuild-shared = "0.8.0"
19+
junobuild-collections = "0.5.0"
20+
junobuild-storage = "0.7.0"
21+
junobuild-cdn = "0.7.0"
22+
junobuild-auth = "0.4.0"

kit/deps/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use junobuild_satellite::include_satellite;
2+
3+
include_satellite!();

kit/setup/prebuild

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
JUNO_TARGET_DIR=/juno/target
6+
7+
# Pre-build all cargo dependencies for Sputnik build
8+
cd "$JUNO_MAIN_DIR" \
9+
&& mkdir -p src/satellite/src && touch src/satellite/src/lib.rs \
10+
&& mkdir -p src/sputnik/src && touch src/sputnik/src/lib.rs \
11+
&& mkdir -p src/libs/macros/src && touch src/libs/macros/src/lib.rs \
12+
&& mkdir -p src/libs/satellite/src && touch src/libs/satellite/src/lib.rs \
13+
&& mkdir -p src/libs/shared/src && touch src/libs/shared/src/lib.rs \
14+
&& mkdir -p src/libs/utils/src && touch src/libs/utils/src/lib.rs \
15+
&& mkdir -p src/libs/collections/src && touch src/libs/collections/src/lib.rs \
16+
&& mkdir -p src/libs/storage/src && touch src/libs/storage/src/lib.rs \
17+
&& mkdir -p src/libs/cdn/src && touch src/libs/cdn/src/lib.rs \
18+
&& mkdir -p src/libs/auth/src && touch src/libs/auth/src/lib.rs \
19+
&& CARGO_TARGET_DIR="$JUNO_TARGET_DIR" ./docker/build --only-dependencies --sputnik
20+
21+
# Pre-download latest published junobuild-* dependencies into the Cargo registry cache.
22+
# Note: even though we compile them here, the artifacts won't be reused at runtime because
23+
# Cargo fingerprints are tied to the workspace - the developer's project is a different
24+
# workspace, so everything will recompile. Only the download step is saved.
25+
# TODO: with a third party tool like sscache we might be able to cache the artifacts
26+
# https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/cargo/guide/build-cache.html
27+
# https://github.com/mozilla/sccache
28+
cd /home/apprunner/deps
29+
RUSTFLAGS='--cfg getrandom_backend="custom" -A deprecated' cargo build --target wasm32-unknown-unknown --target-dir "$JUNO_TARGET_DIR" --release

0 commit comments

Comments
 (0)