Add release asset builder
This commit is contained in:
@@ -145,7 +145,7 @@ During every stage:
|
||||
|
||||
## Stage 1 — Shared Release Library And Asset Builder
|
||||
|
||||
**Status: Pending**
|
||||
**Status: Completed**
|
||||
|
||||
### Goal
|
||||
|
||||
|
||||
167
internal/releasecheck/assets_test.go
Normal file
167
internal/releasecheck/assets_test.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package releasecheck
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAssetBuilderRejectsUnsafeDestinations(t *testing.T) {
|
||||
repoRoot := releaseCheckRepoRoot(t)
|
||||
builder := filepath.Join(repoRoot, "scripts", "build-release-assets.sh")
|
||||
nonempty := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(nonempty, "existing"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
notDirectory := filepath.Join(t.TempDir(), "not-a-directory")
|
||||
if err := os.WriteFile(notDirectory, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
linkTarget := t.TempDir()
|
||||
symlink := filepath.Join(t.TempDir(), "linked-output")
|
||||
if err := os.Symlink(linkTarget, symlink); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, args := range [][]string{
|
||||
nil,
|
||||
{"v1.2", t.TempDir()},
|
||||
{"1.2.3", t.TempDir()},
|
||||
{"v01.2.3", t.TempDir()},
|
||||
{"v1.02.3", t.TempDir()},
|
||||
{"v1.2.03", t.TempDir()},
|
||||
{"v1.2.3-rc.1", t.TempDir()},
|
||||
{"v1.2.3+build", t.TempDir()},
|
||||
{"v1.2.3", "relative-output"},
|
||||
{"v1.2.3", "/"},
|
||||
{"v1.2.3", repoRoot},
|
||||
{"v1.2.3", nonempty},
|
||||
{"v1.2.3", notDirectory},
|
||||
{"v1.2.3", symlink},
|
||||
} {
|
||||
command := exec.Command("sh", append([]string{builder}, args...)...)
|
||||
command.Dir = t.TempDir()
|
||||
output, err := command.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("asset builder %q succeeded", args)
|
||||
}
|
||||
if !strings.Contains(string(output), "build-release-assets:") {
|
||||
t.Fatalf("asset builder %q output = %q", args, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssetBuilderBuildsNamedAssetsAndChecksEmbeddedVersion(t *testing.T) {
|
||||
repoRoot := releaseCheckRepoRoot(t)
|
||||
builder := filepath.Join(repoRoot, "scripts", "build-release-assets.sh")
|
||||
binDir := t.TempDir()
|
||||
writeFakeGo(t, filepath.Join(binDir, "go"))
|
||||
outputDir := filepath.Join(t.TempDir(), "assets")
|
||||
|
||||
command := exec.Command("sh", builder, "v1.2.3", outputDir)
|
||||
command.Dir = t.TempDir()
|
||||
command.Env = append(os.Environ(), "PATH="+binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
output, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("asset builder error = %v\n%s", err, output)
|
||||
}
|
||||
entries, err := os.ReadDir(outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
got = append(got, entry.Name())
|
||||
}
|
||||
sort.Strings(got)
|
||||
want := []string{
|
||||
"narratio-v1.2.3-darwin-amd64",
|
||||
"narratio-v1.2.3-darwin-arm64",
|
||||
"narratio-v1.2.3-linux-amd64",
|
||||
"narratio-v1.2.3-linux-arm64",
|
||||
"narratio-v1.2.3-windows-amd64.exe",
|
||||
"narratio-v1.2.3-windows-arm64.exe",
|
||||
}
|
||||
if !slicesEqual(got, want) {
|
||||
t.Fatalf("asset names = %#v, want %#v", got, want)
|
||||
}
|
||||
version := exec.Command(filepath.Join(outputDir, "narratio-v1.2.3-linux-amd64"), "version")
|
||||
var versionOutput bytes.Buffer
|
||||
version.Stdout = &versionOutput
|
||||
if err := version.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := versionOutput.String(); got != "narratio v1.2.3\n" {
|
||||
t.Fatalf("embedded version output = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func releaseCheckRepoRoot(t *testing.T) string {
|
||||
t.Helper()
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("runtime.Caller() failed")
|
||||
}
|
||||
return filepath.Clean(filepath.Join(filepath.Dir(file), "..", ".."))
|
||||
}
|
||||
|
||||
func writeFakeGo(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
const fake = `#!/bin/sh
|
||||
set -eu
|
||||
|
||||
if [ "$1" = env ]; then
|
||||
case $2 in
|
||||
GOOS) printf '%s\n' linux ;;
|
||||
GOARCH) printf '%s\n' amd64 ;;
|
||||
*) exit 1 ;;
|
||||
esac
|
||||
exit 0
|
||||
fi
|
||||
|
||||
output=
|
||||
version=
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case $1 in
|
||||
-o)
|
||||
shift
|
||||
output=$1
|
||||
;;
|
||||
-ldflags)
|
||||
shift
|
||||
case $1 in
|
||||
*gitea.maximumdirect.net/eric/narratio/internal/buildinfo.Version=*)
|
||||
version=${1##*=}
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
[ -n "$output" ]
|
||||
[ -n "$version" ]
|
||||
printf '#!/bin/sh\nprintf "narratio %%s\\n" "%s"\n' "$version" > "$output"
|
||||
chmod +x "$output"
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(fake), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func slicesEqual(left, right []string) bool {
|
||||
if len(left) != len(right) {
|
||||
return false
|
||||
}
|
||||
for index := range left {
|
||||
if left[index] != right[index] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
93
scripts/build-release-assets.sh
Executable file
93
scripts/build-release-assets.sh
Executable 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
46
scripts/release-lib.sh
Executable 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
|
||||
)
|
||||
Reference in New Issue
Block a user