Static portable build + env-file launcher for deployment

- build.sh: CGO_ENABLED=0 + -ldflags "-s -w" → fully static binary (pure-Go
  SQLite, no C toolchain), runs on any linux/<arch>; note GOARCH cross-compile.
- scripts/hsa-app.sh: load an env file (default /etc/hsa-app/hsa-app.env, or arg/
  $HSA_ENV_FILE), export it, then exec the binary ($HSA_BIN or ./hsa next to it).
  Suitable as a systemd ExecStart.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jean-Michel Tremblay 2026-06-19 08:32:19 -04:00
parent a7566ed166
commit 99b8541da0
2 changed files with 47 additions and 2 deletions

View file

@ -1,6 +1,13 @@
#!/usr/bin/env bash
# Build the hsa binary into the repo root.
#
# CGO_ENABLED=0 makes a fully static binary (the SQLite driver is pure Go, so no
# C toolchain is needed): it runs on any linux/<arch> — glibc or musl/Alpine,
# scratch containers, etc. — with no shared-library dependencies.
# Cross-compile for another arch by setting GOARCH, e.g.:
# GOARCH=arm64 ./scripts/build.sh
set -euo pipefail
cd "$(dirname "$0")/.."
source scripts/go-env.sh
go build -o hsa ./cmd/hsa
echo "Built ./hsa"
CGO_ENABLED=0 go build -ldflags "-s -w" -o hsa ./cmd/hsa
echo "Built ./hsa ($(go env GOOS)/$(go env GOARCH), static)"

38
scripts/hsa-app.sh Executable file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env bash
# Launch the hsa-app binary with its environment loaded from an env file.
#
# Usage:
# hsa-app.sh [ENV_FILE]
#
# ENV_FILE resolves to, in order: the first argument, then $HSA_ENV_FILE, then
# /etc/hsa-app/hsa-app.env. The binary defaults to ./hsa next to this script;
# override with $HSA_BIN (e.g. HSA_BIN=/usr/local/bin/hsa).
#
# Designed as a systemd ExecStart: it exports every KEY=VALUE in the env file and
# then exec's the binary, so the binary becomes the unit's main process (signals
# and exit codes propagate correctly). The env file should use ABSOLUTE paths for
# DB_PATH / STORAGE_DIR / BACKUP_DIR / CONFIG_PATH, since the app resolves relative
# paths from the working directory.
set -euo pipefail
ENV_FILE="${1:-${HSA_ENV_FILE:-/etc/hsa-app/hsa-app.env}}"
HERE="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
BIN="${HSA_BIN:-$HERE/hsa}"
if [[ ! -r "$ENV_FILE" ]]; then
echo "hsa-app: env file not found or unreadable: $ENV_FILE" >&2
exit 1
fi
if [[ ! -x "$BIN" ]]; then
echo "hsa-app: binary not found or not executable: $BIN" >&2
exit 1
fi
# Export every assignment in the env file (KEY=VALUE lines; '#' comments and blank
# lines are fine). set -a marks all subsequently-set vars for export.
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a
exec "$BIN"