1 #!/bin/sh2 # Atomic maintainer release transaction for inf3r (f0rge #187).3 #4 # scripts/release.sh <VERSION> --key <PRIVATE_KEY> [OPTIONS]5 #6 # A release is only published after every stage succeeds:7 # 1. clean tree check and deterministic release tarball8 # 2. signed staging site build (signed release-manifest/SHA256SUMS)9 # 3. signed annotated tag created by `hu13 release-tag` (no ref on failure)10 # 4. final `--publish` build with the tag present11 # 5. tag rebound to the final manifest/SHA256SUMS and verified12 # 6. `git verify-tag`, `release-verify`, `verify-site`, and `verify-hub`13 # 7. optional atomic copy to the publish directory14 #15 # If any step after the first tag fails, the transaction restores the old16 # tag (or deletes a tag that did not exist before) before exiting non-zero.17 # Existing tags are refused unless --replace is explicit; replacing one is18 # never implicit.19 set -eu20 21 usage() {22 cat <<'USAGE'23 usage: scripts/release.sh <VERSION> --key <PRIVATE_KEY> [OPTIONS]24 25 options:26 --repo DIR repository to release (default: .)27 --key FILE Ed25519 private signing key (required)28 --out DIR final built site (default: target/release-hub-<VERSION>)29 --artifacts DIR release-artifact root (default: target/release-artifacts)30 --image DIR OCI layout/index to publish with the release (repeatable)31 --hub URL verify this already-running hub instead of a local one32 --publish-dir DIR atomically copy the verified site to DIR33 --replace permit replacing an existing tag (explicit --force --yes)34 --allow-unattested-ci local preview only: pass --allow-unattested-ci to the publish build35 --preflight build, scan, and verify the staging tree; do not tag or publish36 --sequence N explicit monotonic release sequence for this build37 --dry-run print the transaction and exit without mutation38 39 environment:40 HU13_BIN, HU13_RELEASE_KEY, HU13_RELEASE_REPO, HU13_RELEASE_OUT,41 HU13_RELEASE_ARTIFACTS, HU13_RELEASE_HUB_URL, HU13_RELEASE_PUBLISH_DIR,42 HU13_RELEASE_SEQUENCE, HU13_ALLOW_UNATTESTED_CI=1, HU13_RELEASE_REPLACE=1,43 INF3R_IMAGE_OUT, INF3R_IMAGE_PLATFORMS, INF3R_MAX_IMAGE_BYTES44 USAGE45 }46 47 version=48 repo="${HU13_RELEASE_REPO:-.}"49 key="${HU13_RELEASE_KEY:-}"50 out=51 artifacts="${HU13_RELEASE_ARTIFACTS:-target/release-artifacts}"52 hub="${HU13_RELEASE_HUB_URL:-}"53 publish_dir="${HU13_RELEASE_PUBLISH_DIR:-}"54 replace="${HU13_RELEASE_REPLACE:-0}"55 allow_unattested="${HU13_ALLOW_UNATTESTED_CI:-0}"56 sequence="${HU13_RELEASE_SEQUENCE:-}"57 preflight=058 dry_run=059 image_args=60 image_plan=61 image_out=62 license_args="--license-allow Apache-2.0/MIT"63 64 shell_quote() {65 printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"66 }67 68 add_image() {69 [ -d "$1" ] || {70 echo "release: --image $1 is not a directory" >&271 exit 272 }73 image=$(cd "$1" && pwd)74 image_args="$image_args --image $image"75 image_plan="${image_plan}${image_plan:+ }$image"76 [ -n "$image_out" ] || image_out=$image77 }78 79 while [ "$#" -gt 0 ]; do80 case "$1" in81 -h|--help)82 usage83 exit 084 ;;85 --repo)86 [ "$#" -ge 2 ] || { usage >&2; exit 2; }87 repo=$288 shift 289 ;;90 --key)91 [ "$#" -ge 2 ] || { usage >&2; exit 2; }92 key=$293 shift 294 ;;95 --out)96 [ "$#" -ge 2 ] || { usage >&2; exit 2; }97 out=$298 shift 299 ;;100 --artifacts)101 [ "$#" -ge 2 ] || { usage >&2; exit 2; }102 artifacts=$2103 shift 2104 ;;105 --image)106 [ "$#" -ge 2 ] || { usage >&2; exit 2; }107 add_image "$2"108 shift 2109 ;;110 --hub)111 [ "$#" -ge 2 ] || { usage >&2; exit 2; }112 hub=$2113 shift 2114 ;;115 --publish-dir)116 [ "$#" -ge 2 ] || { usage >&2; exit 2; }117 publish_dir=$2118 shift 2119 ;;120 --replace)121 replace=1122 shift123 ;;124 --allow-unattested-ci)125 allow_unattested=1126 shift127 ;;128 --preflight)129 preflight=1130 shift131 ;;132 --sequence)133 [ "$#" -ge 2 ] || { usage >&2; exit 2; }134 sequence=$2135 shift 2136 ;;137 --dry-run)138 dry_run=1139 shift140 ;;141 -*)142 echo "release: unknown option $1" >&2143 usage >&2144 exit 2145 ;;146 *)147 [ -z "$version" ] || { echo "release: only one VERSION is accepted" >&2; exit 2; }148 version=$1149 shift150 ;;151 esac152 done153 154 [ -n "$version" ] || { usage >&2; exit 2; }155 [ -n "$key" ] || { echo "release: --key or HU13_RELEASE_KEY is required" >&2; exit 2; }156 if ! printf '%s' "$version" | grep -Eq '^v[0-9]+\.[0-9]+(\.[0-9]+)?$'; then157 echo "release: VERSION must look like v4.21" >&2158 exit 2159 fi160 if [ ! -d "$repo/.git" ]; then161 echo "release: $repo is not a git worktree" >&2162 exit 2163 fi164 if [ ! -f "$key" ]; then165 echo "release: signing key $key is missing" >&2166 exit 2167 fi168 repo=$(cd "$repo" && pwd)169 key=$(cd "$(dirname "$key")" && pwd)/$(basename "$key")170 out="${out:-target/release-hub-$version}"171 case "$out" in172 /*) ;;173 *) out="$repo/$out" ;;174 esac175 case "$artifacts" in176 /*) ;;177 *) artifacts="$repo/$artifacts" ;;178 esac179 case "$publish_dir" in180 "") ;;181 /*) ;;182 *) publish_dir="$repo/$publish_dir" ;;183 esac184 allowed="$repo/.f0rge/keys/allowed_signers"185 sequence_arg=186 if [ -n "$sequence" ]; then187 case "$sequence" in188 *[!0-9]*|0) echo "release: --sequence/HU13_RELEASE_SEQUENCE must be a positive integer" >&2; exit 2 ;;189 esac190 sequence_arg="--release-sequence $sequence"191 fi192 epoch=$(git -C "$repo" show -s --format=%ct HEAD)193 head=$(git -C "$repo" rev-parse HEAD)194 record="$repo/.f0rge/releases/$version.md"195 [ -f "$record" ] || { echo "release: $record is missing" >&2; exit 1; }196 rel_sha=$(sed -n 's/^sha:[[:space:]]*//p' "$record" | head -1 | tr -d '\r')197 case "$rel_sha" in198 [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]) ;;199 *) echo "release: $record needs a full 40-hex sha" >&2; exit 1 ;;200 esac201 git -C "$repo" cat-file -e "$rel_sha^{commit}" 2>/dev/null || {202 echo "release: release sha $rel_sha is missing" >&2203 exit 1204 }205 epoch=$(git -C "$repo" show -s --format=%ct "$rel_sha")206 work=207 branch_arg=208 rel_branch=209 src_repo=$repo210 hu13=${HU13_BIN:-}211 if [ -z "$hu13" ]; then212 hu13=$(command -v hu13 2>/dev/null || true)213 fi214 if [ -z "$hu13" ] && [ -x "$repo/../hu13/target/release/hu13" ]; then215 hu13=$repo/../hu13/target/release/hu13216 fi217 [ -n "$hu13" ] && [ -x "$hu13" ] || {218 echo "release: hu13 is not available; set HU13_BIN" >&2219 exit 1220 }221 222 if [ -n "$(git -C "$repo" status --porcelain --untracked-files=no)" ]; then223 echo "release: tracked working tree is dirty; commit the release inputs first" >&2224 exit 1225 fi226 if git -C "$repo" rev-parse --verify "refs/tags/$version" >/dev/null 2>&1; then227 [ "$replace" = 1 ] || {228 echo "release: tag $version already exists; pass --replace only for a reviewed migration" >&2229 exit 1230 }231 fi232 [ -f "$allowed" ] || { echo "release: $allowed is missing" >&2; exit 1; }233 234 cat <<PLAN235 release: version=$version236 release: commit=$rel_sha (record at $head) epoch=$epoch237 release: repo=$repo238 release: source=$src_repo239 release: key=$key240 release: out=$out241 release: artifacts=$artifacts242 release: images=${image_plan:-<none>}243 release: hub=${hub:-<local hu13 serve>}244 release: publish_dir=${publish_dir:-<none>}245 PLAN246 247 if [ "$dry_run" = 1 ]; then248 echo "release: dry-run: no build, tag, or publish was performed"249 exit 0250 fi251 252 old_tag=253 old_tag_hidden=0254 tag_made=0255 cleanup() {256 status=$?257 trap - EXIT INT TERM258 if [ "$status" -ne 0 ] && [ "$old_tag_hidden" = 1 ] && [ "$tag_made" = 0 ] && [ -n "$old_tag" ]; then259 git -C "$repo" update-ref "refs/tags/$version" "$old_tag" 2>/dev/null || true260 echo "release: restored the old tag $version after a failed transaction" >&2261 fi262 if [ "$status" -ne 0 ] && [ "$tag_made" = 1 ]; then263 if [ -n "$old_tag" ]; then264 git -C "$repo" update-ref "refs/tags/$version" "$old_tag" 2>/dev/null || true265 echo "release: rolled back tag $version to $old_tag" >&2266 else267 git -C "$repo" update-ref -d "refs/tags/$version" 2>/dev/null || true268 echo "release: removed tag $version after a failed transaction" >&2269 fi270 fi271 [ -n "${server_pid:-}" ] && kill "$server_pid" 2>/dev/null || true272 [ -n "${tmp:-}" ] && rm -rf "$tmp"273 if [ -n "${work:-}" ]; then274 git -C "$repo" worktree remove --force "$work/src" 2>/dev/null || rm -rf "$work/src"275 [ -n "${rel_branch:-}" ] && git -C "$repo" branch -D "$rel_branch" >/dev/null 2>&1 || true276 rm -rf "$work"277 fi278 exit "$status"279 }280 trap cleanup EXIT INT TERM281 282 if git -C "$repo" rev-parse --verify "refs/tags/$version" >/dev/null 2>&1; then283 old_tag=$(git -C "$repo" rev-parse "refs/tags/$version")284 fi285 286 if [ "$rel_sha" != "$head" ]; then287 work=$(mktemp -d)288 src_repo="$work/src"289 git -C "$repo" worktree add --detach "$src_repo" "$rel_sha" >/dev/null290 rel_branch="inf3r-release-$$"291 git -C "$repo" branch -f "$rel_branch" "$rel_sha" >/dev/null292 branch_arg="--branch $rel_branch"293 mkdir -p "$src_repo/.f0rge/releases" "$src_repo/.f0rge/ci/attestations"294 cp "$record" "$src_repo/.f0rge/releases/$version.md"295 if [ -d "$repo/.f0rge/ci/attestations" ]; then296 cp -a "$repo/.f0rge/ci/attestations/." "$src_repo/.f0rge/ci/attestations/"297 fi298 echo "release: building at release sha $rel_sha (record commit $head)"299 fi300 301 config="$repo/hu13.json"302 config_value() {303 [ -f "$config" ] || return 0304 sed -n "s/^[[:space:]]*\"$1\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$config" | head -1305 }306 # The build command accepts these through its environment. Without this,307 # a release built with `build` loses the public-origin binding and the308 # display name that `publish` would have read from hu13.json.309 public_origin=${HU13_PUBLIC_ORIGIN:-$(config_value public_origin)}310 repo_name=${HU13_REPO_NAME:-$(config_value name)}311 description=${HU13_DESCRIPTION:-$(config_value description)}312 target=${HU13_TARGET:-$(config_value target)}313 case "$target" in314 ""|server|container|hu13-serve) target=hu13-serve ;;315 cloudflare) ;;316 static-adapter|nginx-caddy)317 echo "release: target $target is not a supported hub target" >&2318 exit 1319 ;;320 *)321 echo "release: unsupported target $target" >&2322 exit 1323 ;;324 esac325 [ -n "$public_origin" ] && export HU13_PUBLIC_ORIGIN="$public_origin"326 [ -n "$repo_name" ] && export HU13_REPO_NAME="$repo_name"327 [ -n "$description" ] && export HU13_DESCRIPTION="$description"328 329 echo "release: building binary and archive"330 # 07_release builds the workspace with the remapped paths and stages exactly331 # the binaries and documents that `scripts/release-artifact.sh` packages.332 (cd "$src_repo" && ./.f0rge/ci/steps/07_release.sh "$version" >/dev/null)333 334 # The manifest records RUSTFLAGS; never leak the unremapped prefix there.335 unset RUSTFLAGS CARGO_ENCODED_RUSTFLAGS336 (cd "$src_repo" && SOURCE_DATE_EPOCH="$epoch" ./scripts/release-artifact.sh "$version" "$artifacts/$version")337 [ -x "$hu13" ] || { echo "release: $hu13 is missing after build" >&2; exit 1; }338 339 if [ -z "$image_plan" ]; then340 image_out="${INF3R_IMAGE_OUT:-$repo/target/release-image/$version}"341 rm -rf "$image_out"342 INF3R_IMAGE_SOURCE="${INF3R_IMAGE_SOURCE:-https://inf3r.gl0w.world/repo.git}" \343 HU13_BIN="$hu13" HU13_SIGNING_KEY="$key" \344 "$src_repo/scripts/image-build.sh" "$src_repo" "$version" "$version" "$image_out"345 "$hu13" image verify "$image_out" --allowed "$allowed" >/dev/null346 add_image "$image_out"347 fi348 349 # The PII gate scans text files; the tarball is compressed. Inspect the350 # artifact bytes and the archive members before anything is tagged.351 for artifact in "$artifacts/$version"/*; do352 [ -f "$artifact" ] || continue353 "$hu13" audit artifact "$artifact" --repo "$src_repo" --json >/dev/null || {354 echo "release: artifact PII scan failed for $artifact" >&2355 exit 1356 }357 done358 if [ -n "${HOME:-}" ]; then359 for artifact in "$artifacts/$version"/*; do360 [ -f "$artifact" ] || continue361 case "$artifact" in362 *.tar.gz)363 if tar -xOzf "$artifact" | grep -aFq "$HOME/"; then364 echo "release: $artifact contains builder home path $HOME/" >&2365 exit 1366 fi367 ;;368 esac369 done370 fi371 # `hu13 build` discovers release artifacts from <src>/.f0rge/releases/out;372 # --artifacts-dir only chooses the root it copies from. Stage the tarball373 # there so the signed release manifest records its hash.374 artifact_root="$src_repo/.f0rge/releases/out"375 rm -rf "$artifact_root/$version"376 mkdir -p "$artifact_root"377 cp -a "$artifacts/$version" "$artifact_root/$version"378 379 echo "release: hardware gate"380 archive=$(find "$artifacts/$version" -maxdepth 1 -name '*.tar.gz' -print | head -1)381 [ -n "$archive" ] || { echo "release: the release archive is missing" >&2; exit 1; }382 gate="$artifacts/$version/HARDWARE-GATE.json"383 gate_sig="$gate.sig"384 public_paths=385 while IFS= read -r rel; do386 [ -n "$rel" ] && public_paths="$public_paths $src_repo/$rel"387 done <<EOF388 $(cd "$src_repo" && scripts/gate-public-tree.sh --list)389 EOF390 # shellcheck disable=SC2086 # the public path list is space-separated391 "$src_repo/ops/pii-hardware.sh" --quiet --report "$gate" \392 $public_paths "$archive" "$image_out"393 "$hu13" sign "$gate" --key "$key" --signature "$gate_sig"394 "$hu13" verify "$gate" --signature "$gate_sig" --allowed "$allowed" >/dev/null395 if [ -n "${HOME:-}" ] && grep -aFq "$HOME/" "$gate"; then396 echo "release: HARDWARE-GATE.json contains builder home path $HOME/" >&2397 exit 1398 fi399 cp "$gate" "$gate_sig" "$artifact_root/$version/"400 (cd "$artifacts/$version" && sha256sum HARDWARE-GATE.json HARDWARE-GATE.json.sig >>SHA256SUMS)401 402 if [ -n "$old_tag" ] && [ "$replace" = 1 ]; then403 git -C "$repo" update-ref -d "refs/tags/$version"404 old_tag_hidden=1405 fi406 staging="$out.staging"407 rm -rf "$staging" "$out" "$out.previous"408 echo "release: signing staging manifest"409 (410 cd "$src_repo"411 "$hu13" build . -o "$staging" $branch_arg --target "$target" --signing-key "$key" \412 --artifacts-dir "$artifact_root" --release-record "$record" \413 --source-date-epoch "$epoch" $sequence_arg $image_args $license_args414 )415 416 echo "release: preflight status and artifact inspection"417 "$hu13" release check "$version" --repo "$src_repo" --artifacts-dir "$artifacts" --json >/dev/null || {418 "$hu13" release check "$version" --repo "$src_repo" --artifacts-dir "$artifacts" --json >&2 || true419 echo "release: preflight failed; no tag or publish was created" >&2420 exit 1421 }422 423 if [ "$preflight" = 1 ]; then424 echo "release: preflight verifying staging tree"425 "$hu13" verify-site "$staging" --allowed "$allowed"426 echo "release: preflight OK $version (no tag or publish created)"427 if [ "$old_tag_hidden" = 1 ]; then428 git -C "$repo" update-ref "refs/tags/$version" "$old_tag"429 old_tag_hidden=0430 fi431 tag_made=0432 rm -rf "$staging"433 exit 0434 fi435 436 if [ -n "$old_tag" ]; then437 "$hu13" release-tag "$repo" "$version" --key "$key" \438 --manifest "$staging/release-manifest.json" --sums "$staging/SHA256SUMS" \439 --force --yes440 else441 "$hu13" release-tag "$repo" "$version" --key "$key" \442 --manifest "$staging/release-manifest.json" --sums "$staging/SHA256SUMS"443 fi444 tag_made=1445 446 echo "release: final build"447 if [ "$src_repo" = "$repo" ]; then448 if [ "$allow_unattested" = 1 ]; then449 (cd "$src_repo" && "$hu13" build . -o "$out" $branch_arg --publish --target "$target" --signing-key "$key" \450 --artifacts-dir "$artifact_root" --release-record "$record" \451 --source-date-epoch "$epoch" --allow-unattested-ci $sequence_arg $image_args $license_args)452 else453 (cd "$src_repo" && "$hu13" build . -o "$out" $branch_arg --publish --target "$target" --signing-key "$key" \454 --artifacts-dir "$artifact_root" --release-record "$record" \455 --source-date-epoch "$epoch" $sequence_arg $image_args $license_args)456 fi457 else458 # A release record commit made after the tagged commit cannot be459 # written into the detached source tree without changing the tagged460 # tree. The record copy is intentionally untracked, so the signed461 # build is used and all release checks still run below.462 (cd "$src_repo" && "$hu13" build . -o "$out" $branch_arg --target "$target" --signing-key "$key" \463 --artifacts-dir "$artifact_root" --release-record "$record" \464 --source-date-epoch "$epoch" $sequence_arg $image_args $license_args)465 fi466 467 # The final build contains the tag status and therefore different468 # SHA256SUMS bytes. Rebind the same tag once, still through the atomic469 # release-tag transaction, so the tag covers the bytes that are shipped.470 # shellcheck disable=SC2086471 "$hu13" release-tag "$repo" "$version" \472 --key "$key" \473 --manifest "$out/release-manifest.json" \474 --sums "$out/SHA256SUMS" --force --yes475 476 "$src_repo/scripts/gate-budgets.sh" "$out" "$image_out"477 "$hu13" image verify "$image_out" --allowed "$allowed" >/dev/null478 if [ -n "${HOME:-}" ] && grep -R -aFq "$HOME/" "$out"; then479 echo "release: dist contains builder home path $HOME/" >&2480 exit 1481 fi482 483 echo "release: verifying tag, manifest, site, and hub"484 git -C "$repo" -c "gpg.ssh.allowedSignersFile=$allowed" verify-tag "$version"485 "$hu13" release-verify "$repo" "$version" --dist "$out" --allowed "$allowed"486 "$hu13" verify-site "$out" --allowed "$allowed"487 488 if [ -n "$hub" ]; then489 "$hu13" verify-hub "$hub" --allowed "$allowed" --build-allowed "$allowed"490 else491 tmp=$(mktemp -d)492 mkdir -p "$tmp/inbox"493 "$hu13" serve --www "$out" --inbox-dir "$tmp/inbox" --listen 127.0.0.1:0 \494 >"$tmp/serve.log" 2>&1 &495 server_pid=$!496 i=0497 while [ "$i" -lt 100 ]; do498 grep -q 'listening on' "$tmp/serve.log" && break499 sleep 0.05500 i=$((i + 1))501 done502 addr=$(sed -n 's/.*listening on //p' "$tmp/serve.log" | head -1)503 [ -n "$addr" ] || { echo "release: local server did not start" >&2; cat "$tmp/serve.log" >&2; exit 1; }504 HU13_TRUST_STATE="$tmp/trust.json" "$hu13" verify-hub "http://$addr" --allowed "$allowed" --build-allowed "$allowed"505 fi506 507 if [ -n "$publish_dir" ]; then508 echo "release: publishing to $publish_dir"509 mkdir -p "$(dirname "$publish_dir")"510 tmp_publish="$publish_dir.hu13-publish-$$"511 old_publish="$publish_dir.hu13-old-$$"512 rm -rf "$tmp_publish" "$old_publish"513 cp -aL "$out/." "$tmp_publish/"514 # Keep the old tree until the copy is complete, then swap directories.515 if [ -e "$publish_dir" ]; then mv "$publish_dir" "$old_publish"; fi516 mv "$tmp_publish" "$publish_dir"517 rm -rf "$old_publish"518 fi519 520 tag_made=0521 rm -rf "$staging"522 echo "release: OK $version at $head (tag $(git -C "$repo" rev-parse "refs/tags/$version"))"