Add release asset builder

This commit is contained in:
2026-08-30 19:10:26 +00:00
parent c812fe3655
commit 23dc4e2078
4 changed files with 307 additions and 1 deletions

93
scripts/build-release-assets.sh Executable file
View File

@@ -0,0 +1,93 @@
#!/bin/sh
set -eu
script_dir=$(CDPATH= cd "$(dirname "$0")" && pwd -P)
# shellcheck source=release-lib.sh
. "$script_dir/release-lib.sh"
tool_name=build-release-assets
if [ "$#" -ne 2 ]; then
narratio_release_fail "$tool_name" 'usage: scripts/build-release-assets.sh VERSION OUTPUT_DIR'
fi
release_version=$1
output_dir=$2
if ! narratio_release_validate_version "$release_version"; then
narratio_release_fail "$tool_name" "invalid stable version: $release_version"
fi
case $output_dir in
/*) ;;
*) narratio_release_fail "$tool_name" "output directory must be absolute: $output_dir" ;;
esac
repo_root=$(narratio_release_repo_root "$0") || narratio_release_fail "$tool_name" 'cannot locate repository root'
if ! cd "$repo_root"; then
narratio_release_fail "$tool_name" "cannot enter repository root: $repo_root"
fi
if [ "$output_dir" = / ]; then
narratio_release_fail "$tool_name" 'output directory must not be /'
fi
if [ -L "$output_dir" ]; then
narratio_release_fail "$tool_name" "output directory must not be a symlink: $output_dir"
fi
if [ -e "$output_dir" ]; then
if [ ! -d "$output_dir" ]; then
narratio_release_fail "$tool_name" "output path must be a directory: $output_dir"
fi
resolved_output_dir=$(CDPATH= cd "$output_dir" && pwd -P) || narratio_release_fail "$tool_name" "cannot access output directory: $output_dir"
if [ "$resolved_output_dir" = "$repo_root" ]; then
narratio_release_fail "$tool_name" 'output directory must not be the repository root'
fi
for existing_entry in "$output_dir"/* "$output_dir"/.[!.]* "$output_dir"/..?*; do
if [ -e "$existing_entry" ] || [ -L "$existing_entry" ]; then
narratio_release_fail "$tool_name" "output directory must be empty: $output_dir"
fi
done
else
if ! mkdir "$output_dir"; then
narratio_release_fail "$tool_name" "cannot create output directory: $output_dir"
fi
fi
temporary_smoke_binary=
cleanup_temporary_smoke_binary() {
if [ -n "$temporary_smoke_binary" ]; then
rm -f "$temporary_smoke_binary"
fi
}
trap cleanup_temporary_smoke_binary 0 HUP INT TERM
build_flags="-s -w -X gitea.maximumdirect.net/eric/narratio/internal/buildinfo.Version=$release_version"
host_os=$(go env GOOS)
host_arch=$(go env GOARCH)
host_asset=
for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64; do
target_os=${target%%/*}
target_arch=${target#*/}
suffix=
if [ "$target_os" = windows ]; then
suffix=.exe
fi
asset_path="$output_dir/narratio-$release_version-$target_os-$target_arch$suffix"
CGO_ENABLED=0 GOOS="$target_os" GOARCH="$target_arch" go build -trimpath -ldflags "$build_flags" -o "$asset_path" ./cmd/narratio
if [ "$target_os" = "$host_os" ] && [ "$target_arch" = "$host_arch" ]; then
host_asset=$asset_path
fi
done
if [ -z "$host_asset" ]; then
temporary_smoke_binary="$output_dir/.narratio-release-version-smoke"
CGO_ENABLED=0 go build -trimpath -ldflags "$build_flags" -o "$temporary_smoke_binary" ./cmd/narratio
host_asset=$temporary_smoke_binary
fi
reported_version=$("$host_asset" version)
if [ "$reported_version" != "narratio $release_version" ]; then
narratio_release_fail "$tool_name" "release binary reported unexpected version: $reported_version"
fi
cleanup_temporary_smoke_binary
temporary_smoke_binary=

46
scripts/release-lib.sh Executable file
View File

@@ -0,0 +1,46 @@
#!/bin/sh
# Shared helpers for Narratio's checked-in release scripts. This file is
# intentionally source-only and has no effect when loaded.
narratio_release_fail() {
tool_name=$1
shift
printf '%s: %s\n' "$tool_name" "$*" >&2
exit 1
}
narratio_release_validate_version() {
version=$1
case $version in
v*.*.*) ;;
*) return 1 ;;
esac
components=${version#v}
if [ "$components" = "$version" ]; then
return 1
fi
previous_ifs=$IFS
IFS=.
set -- $components
IFS=$previous_ifs
if [ "$#" -ne 3 ]; then
return 1
fi
for component in "$@"; do
case $component in
0 | [1-9]*) ;;
*) return 1 ;;
esac
case $component in
*[!0-9]*) return 1 ;;
esac
done
}
narratio_release_repo_root() (
script_path=$1
script_dir=$(CDPATH= cd "$(dirname "$script_path")" && pwd -P) || return 1
CDPATH= cd "$script_dir/.." && pwd -P
)