thread-master/deploy/prod/build-release.sh

56 lines
2.2 KiB
Bash
Raw Permalink Normal View History

2026-07-14 08:54:31 +00:00
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_DIR=$(cd "$SCRIPT_DIR/../.." && pwd)
BACKEND_DIR="$ROOT_DIR/apps/backend"
WEB_DIR="$ROOT_DIR/apps/web"
ARTIFACT_DIR="$SCRIPT_DIR/artifacts"
timestamp=$(date -u +%Y%m%dT%H%M%SZ)
revision=$(git -C "$ROOT_DIR" rev-parse --short HEAD 2>/dev/null || printf unknown)
if ! git -C "$ROOT_DIR" diff --quiet --ignore-submodules -- 2>/dev/null || [[ -n $(git -C "$ROOT_DIR" ls-files --others --exclude-standard 2>/dev/null) ]]; then
revision="$revision-dirty"
fi
release_id="harbor-$timestamp-$revision"
stage=$(mktemp -d "/tmp/${release_id}.XXXXXX")
trap 'rm -rf "$stage"' EXIT
mkdir -p "$stage/bin" "$stage/web" "$stage/migrations" "$ARTIFACT_DIR"
if [[ ${SKIP_TESTS:-0} != 1 ]]; then
2026-07-15 15:23:59 +00:00
(cd "$BACKEND_DIR" && go test ./... && go vet ./...)
fi
(cd "$WEB_DIR" && npm ci)
if [[ ${SKIP_TESTS:-0} != 1 ]]; then
(cd "$WEB_DIR" && npm test)
2026-07-14 08:54:31 +00:00
fi
printf '%s\n' "building Linux amd64 gateway and worker"
(cd "$BACKEND_DIR" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags='-s -w' -o "$stage/bin/gateway" .)
(cd "$BACKEND_DIR" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags='-s -w' -o "$stage/bin/worker" ./cmd/worker)
2026-07-15 15:23:59 +00:00
(cd "$BACKEND_DIR" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags='-s -w' -o "$stage/bin/seeder" ./cmd/seeder)
2026-07-14 08:54:31 +00:00
GOBIN="$stage/bin" CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go install -tags mongodb github.com/golang-migrate/migrate/v4/cmd/migrate@v4.18.3
printf '%s\n' "building static web"
2026-07-15 15:23:59 +00:00
(cd "$WEB_DIR" && VITE_API_BASE= npm run build)
2026-07-14 08:54:31 +00:00
cp -a "$WEB_DIR/dist/." "$stage/web/"
cp -a "$BACKEND_DIR/generate/database/mongo/." "$stage/migrations/"
cat > "$stage/release.txt" <<EOF
release=$release_id
revision=$revision
built_at=$timestamp
goos=linux
goarch=amd64
EOF
2026-07-15 15:23:59 +00:00
(cd "$stage" && sha256sum bin/gateway bin/worker bin/seeder bin/migrate web/index.html > manifest.sha256)
2026-07-14 08:54:31 +00:00
find "$stage" -type d -exec chmod 0755 {} +
find "$stage" -type f -exec chmod 0644 {} +
2026-07-15 15:23:59 +00:00
chmod 0755 "$stage/bin/gateway" "$stage/bin/worker" "$stage/bin/seeder" "$stage/bin/migrate"
2026-07-14 08:54:31 +00:00
artifact="$ARTIFACT_DIR/$release_id.tar.gz"
tar -C "$stage" -czf "$artifact" .
sha256sum "$artifact" > "$artifact.sha256"
printf '%s\n' "$artifact"