From 0b7c9f8e1f2b1ddf2c2cf717b3f3a65ac6d2f35a Mon Sep 17 00:00:00 2001 From: QP Hou Date: Mon, 5 May 2025 09:34:32 -0700 Subject: [PATCH] build ui in a separate step (#397) --- .github/workflows/build.yml | 6 ++++- .github/workflows/roapi_release.yml | 12 ++++++++++ Dockerfile | 10 +++++++-- roapi/build.rs | 35 ----------------------------- roapi/src/config.rs | 2 ++ 5 files changed, 27 insertions(+), 38 deletions(-) delete mode 100644 roapi/build.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ecdc03d..6908b5e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -44,7 +44,11 @@ jobs: - uses: taiki-e/install-action@v2 with: tool: trunk - - name: Build + - name: Build UI first + run: | + cd roapi-ui + trunk build --release + - name: Build ROAPI with UI run: | mold -run cargo build --features=ui diff --git a/.github/workflows/roapi_release.yml b/.github/workflows/roapi_release.yml index 328d676..60ca21b 100644 --- a/.github/workflows/roapi_release.yml +++ b/.github/workflows/roapi_release.yml @@ -52,6 +52,10 @@ jobs: tool: trunk - name: Install maturin run: pip3 install 'maturin<2' + - name: Build UI first + run: | + cd roapi-ui + trunk build --release - name: Build wheels - x86_64 run: | maturin build -m roapi/Cargo.toml -b bin --target x86_64-apple-darwin --release --out dist --features=database-sqlite,ui @@ -106,6 +110,10 @@ jobs: tool: trunk - name: Install maturin run: pip3 install 'maturin<2' + - name: Build UI first + run: | + cd roapi-ui + trunk build --release - name: Build wheels run: | maturin build -m roapi/Cargo.toml -b bin --release --out dist --target ${{ matrix.platform.target }} --features=${{ matrix.platform.features }},ui @@ -198,6 +206,10 @@ jobs: run: | apt-get update apt-get install -y libssl-dev + - name: Build UI first + run: | + cd roapi-ui + trunk build --release - name: Build Wheels run: | sudo python3 -m pip install 'maturin<2' diff --git a/Dockerfile b/Dockerfile index 83a178d..de02bae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,9 @@ FROM rust:${RUST_VER} AS chef RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash RUN cargo binstall trunk +# Add WebAssembly target for UI build +RUN rustup target add wasm32-unknown-unknown + # We only pay the installation cost once, # it will be cached from the second build onwards RUN cargo binstall cargo-chef @@ -27,18 +30,21 @@ WORKDIR /roapi_src COPY --from=planner /roapi_src/recipe.json recipe.json RUN cargo chef cook --features ${FEATURES} --release --recipe-path recipe.json -# Step 3: Build the release binary +# Step 3: Build the UI and release binary FROM chef AS builder ARG FEATURES WORKDIR /roapi_src COPY ./ /roapi_src COPY --from=cacher /roapi_src/target target COPY --from=cacher /usr/local/cargo /usr/local/cargo +# First build the UI +RUN cd roapi-ui && trunk build --release +# Then build the ROAPI binary with the UI embedded RUN cargo build --release --locked --bin roapi --features ${FEATURES} # Step 4: Assemble the final image FROM debian:bookworm-slim -LABEL org.opencontainers.image.source https://github.com/roapi/roapi +LABEL org.opencontainers.image.source=https://github.com/roapi/roapi RUN apt-get update \ && apt-get install -y libssl-dev ca-certificates \ diff --git a/roapi/build.rs b/roapi/build.rs deleted file mode 100644 index f053754..0000000 --- a/roapi/build.rs +++ /dev/null @@ -1,35 +0,0 @@ -use std::env; -use std::process::Command; - -fn main() { - if env::var("CARGO_FEATURE_UI").is_ok() { - let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); - let manifest_path = std::path::Path::new(&manifest_dir); - let workspace_root = manifest_path - .parent() - .expect("Failed to extract workspace root"); - let ui_crate_path = workspace_root.join("roapi-ui"); - - for entry in &["src", "Cargo.toml", "assets", "index.html", "trunk.toml"] { - println!( - "cargo:rerun-if-changed={}/{}", - ui_crate_path.display(), - entry - ); - } - - let output = Command::new("trunk") - .current_dir(ui_crate_path) - .arg("build") - .arg("--release") - .output() - .expect("Failed to run Trunk build command"); - - if !output.status.success() { - println!("Trunk build failed"); - println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); - println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); - std::process::exit(1); - } - } -} diff --git a/roapi/src/config.rs b/roapi/src/config.rs index 1cf2793..a863876 100644 --- a/roapi/src/config.rs +++ b/roapi/src/config.rs @@ -150,6 +150,8 @@ pub fn get_cmd() -> clap::Command { "snmalloc", #[cfg(feature = "rustls")] "rustls", + #[cfg(feature = "ui")] + "ui", ]; clap::Command::new("roapi")