- #!/usr/bin/env bash
- # Build static Linux musl release archives (`linux-amd64`, `linux-arm64`) for
- # Roneo and write an OCI image layout matching docs/PUBLIC-FORGE-RELEASES.md §4.
- #
- # Usage:
- # ci/binaries/linux/build-archives.sh [commit-sha]
- #
- # Output:
- # release/linux/oci OCI image layout containing one image index with two
- # archive manifests:
- # - entry `linux-amd64` -> roneo-<version>-linux-amd64.tar.gz
- # - entry `linux-arm64` -> roneo-<version>-linux-arm64.tar.gz
- set -euo pipefail
- binary_name="roneo"
- layout_dir="${RONEO_RELEASE_LAYOUT:-release/linux/oci}"
- index_type="application/vnd.oci.image.index.v1+json"
- manifest_type="application/vnd.oci.image.manifest.v1+json"
- archive_type="application/vnd.forge.archive.v1"
- empty_type="application/vnd.oci.empty.v1+json"
- gzip_type="application/gzip"
- commit="${1:-}"
- if [ -n "$commit" ] && ! [[ $commit =~ ^[0-9a-f]{40}$ ]]; then
- echo "build-archives.sh: $commit is not a full 40-hex commit SHA" >&2
- exit 2
- fi
- # Extract workspace package version from Cargo.toml (no build metadata '+').
- read_version() {
- awk '
- /^\[workspace\.package\]/ { in_pkg = 1; next }
- /^\[/ { in_pkg = 0 }
- in_pkg && /^version[[:space:]]*=/ {
- gsub(/^[^"]*"/, "")
- gsub(/".*$/, "")
- exit
- }
- ' Cargo.toml
- }
- version=$(read_version)
- if ! [[ $version =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
- echo "build-archives.sh: invalid Cargo.toml workspace version: '${version}'" >&2
- exit 1
- fi
- sha256_of() {
- local sum
- sum=$(sha256sum "$1")
- printf '%s' "${sum%% *}"
- }
- oci_put_file() {
- local blobs=$1 media_type=$2 src=$3 hex size
- hex=$(sha256_of "$src")
- size=$(wc -c <"$src")
- size=${size//[[:space:]]/}
- [ -e "$blobs/$hex" ] || cp "$src" "$blobs/$hex"
- printf '{"digest":"sha256:%s","mediaType":"%s","size":%s}' "$hex" "$media_type" "$size"
- }
- oci_put_json() {
- local blobs=$1 media_type=$2 payload=$3 hex size
- hex=$(printf '%s' "$payload" | sha256sum)
- hex=${hex%% *}
- printf '%s' "$payload" >"$blobs/$hex"
- size=$(wc -c <"$blobs/$hex")
- size=${size//[[:space:]]/}
- printf '{"digest":"sha256:%s","mediaType":"%s","size":%s}' "$hex" "$media_type" "$size"
- }
- build_target() {
- local triple=$1 arch=$2 out_archive=$3
- local bin stage
- echo "build-archives.sh: building ${binary_name} ${version} for ${triple} (linux/${arch})"
- RUSTFLAGS="--remap-path-prefix=${HOME}=/build --remap-path-prefix=$(pwd)=/src -C linker=rust-lld -C link-self-contained=yes -C target-feature=+crt-static -C link-arg=-s -C link-arg=-z -C link-arg=relro -C link-arg=-z -C link-arg=now -C link-arg=-z -C link-arg=noexecstack" \
- cargo build --release --locked --target "$triple" -p roneo-cli --bin "$binary_name"
- bin="target/${triple}/release/${binary_name}"
- test -f "$bin"
- # Verify the binary runs on matching host architecture.
- local host_arch
- host_arch=$(uname -m)
- if { [ "$arch" = "amd64" ] && [ "$host_arch" = "x86_64" ]; } ||
- { [ "$arch" = "arm64" ] && [ "$host_arch" = "aarch64" ]; }; then
- local said
- said=$("$bin" --version)
- if [ "$said" != "${binary_name} ${version}" ]; then
- echo "build-archives.sh: ${triple} binary said '${said}', expected '${binary_name} ${version}'" >&2
- return 1
- fi
- echo "build-archives.sh: verified ${triple} executable: ${said}"
- fi
- stage="${work}/stage-${arch}"
- mkdir -p "$stage"
- cp "$bin" "${stage}/${binary_name}"
- chmod 0755 "${stage}/${binary_name}"
- cp LICENSE "${stage}/LICENSE"
- chmod 0644 "${stage}/LICENSE"
- # Deterministic tarball with binary at root (`roneo`) for direct extraction
- # by both `install.sh` and the `mise` forge backend plugin.
- tar -C "$stage" \
- --owner=0 --group=0 --numeric-owner \
- --sort=name --mtime='@0' \
- -cf - "$binary_name" LICENSE | gzip -n -9 >"$out_archive"
- }
- write_oci_layout() {
- local out=$1
- shift
- local blobs config layer manifest entry entries="" index pair ref_name file title
- if [ -e "$out" ] || [ -L "$out" ]; then
- echo "build-archives.sh: $out already exists; layout must start clean" >&2
- return 1
- fi
- blobs="${out}/blobs/sha256"
- mkdir -p "$blobs"
- config=$(oci_put_json "$blobs" "$empty_type" '{}')
- for pair in "$@"; do
- ref_name=${pair%%=*}
- file=${pair#*=}
- if [ ! -f "$file" ] || [ -L "$file" ]; then
- echo "build-archives.sh: $file is not a regular file" >&2
- return 1
- fi
- title=${file##*/}
- if ! [[ $title =~ ^[A-Za-z0-9._+-]+$ ]]; then
- echo "build-archives.sh: $title is not a plain file name" >&2
- return 1
- fi
- if [[ $title != "${binary_name}-${version}-linux-"* ]]; then
- echo "build-archives.sh: $title does not start with ${binary_name}-${version}-linux-" >&2
- return 1
- fi
- layer=$(oci_put_file "$blobs" "$gzip_type" "$file")
- manifest=$(printf '{"artifactType":"%s","config":%s,"layers":[{"annotations":{"org.opencontainers.image.title":"%s"},%s],"mediaType":"%s","schemaVersion":2}' \
- "$archive_type" "$config" "$title" "${layer#\{}" "$manifest_type")
- entry=$(oci_put_json "$blobs" "$manifest_type" "$manifest")
- entries+=${entries:+,}$(printf '{"annotations":{"org.opencontainers.image.ref.name":"%s"},"artifactType":"%s",%s' \
- "$ref_name" "$archive_type" "${entry#\{}")
- done
- index=$(oci_put_json "$blobs" "$index_type" \
- "$(printf '{"manifests":[%s],"mediaType":"%s","schemaVersion":2}' "$entries" "$index_type")")
- printf '{"manifests":[%s],"mediaType":"%s","schemaVersion":2}' "$index" "$index_type" >"${out}/index.json"
- printf '{"imageLayoutVersion":"1.0.0"}' >"${out}/oci-layout"
- }
- work=$(mktemp -d "${TMPDIR:-/tmp}/roneo-release-linux.XXXXXX")
- trap 'rm -rf "$work"' EXIT INT TERM
- rustup target add x86_64-unknown-linux-musl aarch64-unknown-linux-musl
- amd64_archive="${work}/${binary_name}-${version}-linux-amd64.tar.gz"
- arm64_archive="${work}/${binary_name}-${version}-linux-arm64.tar.gz"
- build_target x86_64-unknown-linux-musl amd64 "$amd64_archive"
- build_target aarch64-unknown-linux-musl arm64 "$arm64_archive"
- rm -rf "$layout_dir"
- write_oci_layout "$layout_dir" \
- "linux-amd64=${amd64_archive}" \
- "linux-arm64=${arm64_archive}"
- echo "build-archives.sh: wrote OCI layout at ${layout_dir}"
- ls -la "${layout_dir}" "${layout_dir}/blobs/sha256"