Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

fix(ci): package runtime from exact Cargo build output (#2953)

zombee066c29a52e3bc
diff --git a/.github/actions/run-in-manylinux/action.yml b/.github/actions/run-in-manylinux/action.yml
index 05382e904..c39d2e304 100644
--- a/.github/actions/run-in-manylinux/action.yml
+++ b/.github/actions/run-in-manylinux/action.yml
@@ -28,7 +28,7 @@ inputs:
Build commands to run inside the container after the prologue. Written
literally — no host-side expansion — so $VARS resolve in the container, the
same as under the quoted heredoc each call site used before. Empty runs the
- prologue alone.
+ prologue alone. BOXLITE_CI_TARGET contains the target input.
required: false
default: ''
@@ -119,5 +119,6 @@ runs:
"${SCCACHE_DOCKER_ARGS[@]}" \
-w /work \
-e CARGO_HOME=/work/.cargo-manylinux \
+ -e "BOXLITE_CI_TARGET=$TARGET" \
"quay.io/pypa/manylinux_2_28_$image_arch" \
bash /tmp/build.sh
diff --git a/.github/workflows/build-runtime.yml b/.github/workflows/build-runtime.yml
index ea16cae6c..820373b99 100644
--- a/.github/workflows/build-runtime.yml
+++ b/.github/workflows/build-runtime.yml
@@ -77,31 +77,23 @@ jobs:
with:
show-sccache-stats: ${{ github.event_name == 'schedule' }}
- - name: Build runtime (Linux)
+ - name: Build and package runtime (Linux)
if: runner.os == 'Linux'
uses: ./.github/actions/run-in-manylinux
with:
target: ${{ matrix.target }}
script: |
- # Build CLI binary (reuses compiled boxlite crate artifacts)
- cargo build -p boxlite-cli --release
+ bash scripts/build/package-runtime.sh "$BOXLITE_CI_TARGET"
- - name: Build runtime (macOS)
+ - name: Build and package runtime (macOS)
if: runner.os == 'macOS'
+ env:
+ BOXLITE_CI_TARGET: ${{ matrix.target }}
run: |
make setup:build runtime
- cargo build -p boxlite-cli --release
+ bash scripts/build/package-runtime.sh "$BOXLITE_CI_TARGET"
command -v sccache &>/dev/null && sccache --show-stats || true
- - name: Package runtime
- run: |
- VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/.*"\(.*\)"/\1/')
- RUNTIME_DIR=$(ls -dt target/release/build/boxlite-*/out/runtime 2>/dev/null | head -1)
- [ -n "$RUNTIME_DIR" ] || { echo "ERROR: Runtime directory not found under target/"; exit 1; }
- cp -a "$RUNTIME_DIR" boxlite-runtime
- tar czf "boxlite-runtime-v${VERSION}-${{ matrix.target }}.tar.gz" boxlite-runtime/
- rm -rf boxlite-runtime
-
- name: Upload runtime artifact
uses: actions/upload-artifact@v4
with:
diff --git a/docs/contributing/development/building.md b/docs/contributing/development/building.md
index 674f5ed3a..139f7590f 100644
--- a/docs/contributing/development/building.md
+++ b/docs/contributing/development/building.md
@@ -125,3 +125,7 @@ scripts/
├── package/ # Packaging scripts
└── common.sh # Shared utilities
```
+
+The Build Runtime workflow packages Cargo's runtime in the build environment,
+including inside manylinux on Linux. It resolves links there and leaves a
+`boxlite-runtime-v<version>-<target>.tar.gz` archive for the runner to upload.
diff --git a/scripts/build/build-runtime.sh b/scripts/build/build-runtime.sh
index 34aaf4f7a..612ade9c0 100755
--- a/scripts/build/build-runtime.sh
+++ b/scripts/build/build-runtime.sh
@@ -224,46 +224,13 @@ collect_libraries() {
cd "$PROJECT_ROOT"
# Build boxlite crate to generate OUT_DIR with bundled libraries
- local build_flag=""
+ local build_flags=()
if [ "$PROFILE" = "release" ]; then
- build_flag="--release"
+ build_flags+=(--release)
fi
- # Build boxlite crate and capture the exact OUT_DIR from cargo's JSON output
- # This is deterministic - no guessing based on directory names or timestamps
- local runtime_src=""
- runtime_src=$(cargo build $build_flag --lib -p boxlite --message-format=json 2>/dev/null | \
- grep -o '"out_dir":"[^"]*"' | \
- tail -1 | \
- cut -d'"' -f4)
-
- if [ -n "$runtime_src" ]; then
- runtime_src="$runtime_src/runtime"
- fi
-
- # Fallback: if JSON parsing failed, find by modification time (newest first)
- if [ -z "$runtime_src" ] || [ ! -d "$runtime_src" ]; then
- local out_dir
- out_dir=$(cargo metadata --format-version 1 2>/dev/null | \
- grep -o '"target_directory":"[^"]*"' | \
- cut -d'"' -f4)
-
- if [ -z "$out_dir" ]; then
- out_dir="$PROJECT_ROOT/target"
- fi
-
- # Sort by modification time (newest first) to get the most recent build
- runtime_src=$(find "$out_dir/$PROFILE/build/boxlite-"*/out/runtime -type d -print0 2>/dev/null | \
- xargs -0 ls -dt 2>/dev/null | head -1)
- fi
-
- if [ -z "$runtime_src" ] || [ ! -d "$runtime_src" ]; then
- print_error "Could not find runtime libraries directory"
- echo "Expected location: $out_dir/$PROFILE/build/boxlite-*/out/runtime"
- exit 1
- fi
-
- RUNTIME_LIBS_DIR="$runtime_src"
+ RUNTIME_LIBS_DIR=$(bash "$SCRIPT_BUILD_DIR/cargo-runtime-dir.sh" \
+ "${build_flags[@]}" --lib -p boxlite)
print_success "Found libraries at: $RUNTIME_LIBS_DIR"
}
diff --git a/scripts/build/cargo-runtime-dir.sh b/scripts/build/cargo-runtime-dir.sh
new file mode 100644
index 000000000..2e957d5d4
--- /dev/null
+++ b/scripts/build/cargo-runtime-dir.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+# Build with the supplied Cargo arguments and print BoxLite's exact runtime path.
+set -euo pipefail
+
+package_id=$(cargo pkgid -p boxlite)
+messages=$(mktemp "${TMPDIR:-/tmp}/boxlite-runtime.XXXXXX")
+trap 'rm -f "$messages"' EXIT
+trap 'exit 129' HUP
+trap 'exit 130' INT
+trap 'exit 143' TERM
+
+# Keep Cargo diagnostics on stderr and preserve its exit status before parsing.
+cargo build "$@" --message-format=json-render-diagnostics > "$messages"
+
+python3 - "$messages" "$package_id" <<'PY'
+import json
+from pathlib import Path
+import sys
+
+try:
+ outputs = []
+ with open(sys.argv[1]) as messages:
+ for line in messages:
+ if not line.strip():
+ continue
+ message = json.loads(line)
+ if not isinstance(message, dict):
+ raise ValueError("expected a Cargo JSON object")
+ if (message.get("reason") == "build-script-executed"
+ and message.get("package_id") == sys.argv[2]):
+ outputs.append(message.get("out_dir"))
+ if len(outputs) != 1 or not isinstance(outputs[0], str):
+ raise ValueError("expected exactly one BoxLite build-script out_dir")
+ runtime = Path(outputs[0]) / "runtime"
+ if not runtime.is_absolute() or not runtime.is_dir():
+ raise ValueError(f"invalid runtime directory: {runtime}")
+ print(runtime)
+except (OSError, ValueError) as error:
+ sys.exit(f"Cannot locate BoxLite runtime from Cargo output: {error}")
+PY
diff --git a/scripts/build/package-runtime.sh b/scripts/build/package-runtime.sh
new file mode 100755
index 000000000..e2d8bcba4
--- /dev/null
+++ b/scripts/build/package-runtime.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+# Build the release CLI and package the runtime reported by Cargo.
+set -euo pipefail
+
+if [[ $# -ne 1 || ! "$1" =~ ^[a-zA-Z0-9][a-zA-Z0-9_-]*$ ]]; then
+ printf 'Usage: %s <release-target>\n' "$0" >&2
+ exit 2
+fi
+
+script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+project_root=$(cd "$script_dir/../.." && pwd)
+cd "$project_root"
+
+version=$(awk -F '"' '/^version = "/ { print $2; exit }' Cargo.toml)
+if [[ ! "$version" =~ ^[0-9][a-zA-Z0-9.+-]*$ ]]; then
+ printf 'Cannot determine the runtime release version from Cargo.toml\n' >&2
+ exit 1
+fi
+archive="$project_root/boxlite-runtime-v${version}-$1.tar.gz"
+rm -f "$archive"
+
+staging=$(mktemp -d "$project_root/.boxlite-runtime.XXXXXX")
+trap 'rm -rf "$staging"' EXIT
+trap 'exit 129' HUP
+trap 'exit 130' INT
+trap 'exit 143' TERM
+
+runtime_dir=$(bash "$script_dir/cargo-runtime-dir.sh" -p boxlite-cli --release)
+for binary in boxlite-shim boxlite-guest guest-mke2fs guest-resize2fs; do
+ if [[ ! -f "$runtime_dir/$binary" || ! -s "$runtime_dir/$binary" || ! -x "$runtime_dir/$binary" ]]; then
+ printf 'Missing or invalid runtime executable: %s\n' "$runtime_dir/$binary" >&2
+ exit 1
+ fi
+done
+
+# Give the archive its release layout without copying the unpacked runtime.
+ln -s "$runtime_dir" "$staging/boxlite-runtime"
+tar -chzf "$staging/runtime.tar.gz" -C "$staging" boxlite-runtime/
+chmod 0644 "$staging/runtime.tar.gz"
+mv "$staging/runtime.tar.gz" "$archive"
diff --git a/tests/scripts/test_package_runtime.py b/tests/scripts/test_package_runtime.py
new file mode 100644
index 000000000..116697ce2
--- /dev/null
+++ b/tests/scripts/test_package_runtime.py
@@ -0,0 +1,86 @@
+"""Exercise runtime release packaging without a native build."""
+
+import json
+import os
+from pathlib import Path
+import shutil
+import stat
+import subprocess
+import tarfile
+import tempfile
+import unittest
+
+
+ROOT = Path(__file__).resolve().parents[2]
+BINARIES = ("boxlite-shim", "boxlite-guest", "guest-mke2fs", "guest-resize2fs")
+
+
+class PackageRuntimeTest(unittest.TestCase):
+ def setUp(self):
+ self.workspace = Path(self.enterContext(tempfile.TemporaryDirectory()))
+ self.scripts = self.workspace / "scripts" / "build"
+ self.scripts.mkdir(parents=True)
+ for name in ("package-runtime.sh", "cargo-runtime-dir.sh"):
+ shutil.copyfile(ROOT / "scripts" / "build" / name, self.scripts / name)
+ (self.workspace / "Cargo.toml").write_text('[workspace.package]\nversion = "0.1.0"\n')
+ self.archive = self.workspace / "boxlite-runtime-v0.1.0-linux-x64-gnu.tar.gz"
+
+ runtime = self.workspace / "out" / "runtime"
+ runtime.mkdir(parents=True)
+ for name in BINARIES:
+ executable = runtime / name
+ executable.write_text("#!/bin/sh\nexit 0\n")
+ executable.chmod(0o755)
+ firmware = runtime.parent / "firmware.so"
+ firmware.write_bytes(b"runtime data")
+ (runtime / "libkrunfw.so").symlink_to(firmware)
+
+ cargo = self.workspace / "cargo"
+ cargo.write_text(
+ '#!/bin/sh\n'
+ 'case "$1" in\n'
+ ' pkgid) printf "%s\\n" "$TEST_PACKAGE_ID" ;;\n'
+ ' build) printf "%s\\n" "$TEST_BUILD_MESSAGE" ;;\n'
+ ' *) exit 2 ;;\n'
+ 'esac\n'
+ )
+ cargo.chmod(0o755)
+ package_id = "path+file:///boxlite#0.1.0"
+ self.env = dict(os.environ, PATH=f"{self.workspace}:{os.environ['PATH']}")
+ self.env["TEST_PACKAGE_ID"] = package_id
+ self.env["TEST_BUILD_MESSAGE"] = json.dumps({
+ "reason": "build-script-executed",
+ "package_id": package_id,
+ "out_dir": str(runtime.parent),
+ })
+
+ def package(self):
+ return subprocess.run(
+ ["bash", str(self.scripts / "package-runtime.sh"), "linux-x64-gnu"],
+ env=self.env, capture_output=True, text=True, timeout=30, umask=0o077,
+ )
+
+ def test_build_leaves_only_a_host_readable_archive(self):
+ before = set(self.workspace.iterdir())
+ result = self.package()
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(set(self.workspace.iterdir()) - before, {self.archive})
+ self.assertEqual(oct(stat.S_IMODE(self.archive.stat().st_mode)), "0o644")
+ with tarfile.open(self.archive) as archive:
+ for executable in BINARIES:
+ member = archive.getmember(f"boxlite-runtime/{executable}")
+ self.assertTrue(member.isfile())
+ self.assertEqual(member.mode, 0o755)
+ firmware = archive.getmember("boxlite-runtime/libkrunfw.so")
+ self.assertTrue(firmware.isfile())
+ with archive.extractfile(firmware) as contents:
+ self.assertEqual(contents.read(), b"runtime data")
+
+ def test_tar_failure_removes_temporary_output(self):
+ tar = self.workspace / "tar"
+ tar.write_text('#!/bin/sh\nprintf partial > "$2"\nexit 23\n')
+ tar.chmod(0o755)
+ before = set(self.workspace.iterdir())
+ result = self.package()
+ self.assertEqual(result.returncode, 23, result.stderr)
+ self.assertEqual(set(self.workspace.iterdir()), before)