81 lines
1.9 KiB
Bash
Executable File
81 lines
1.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
ADMIN_DIR="$ROOT_DIR/frontend/admin"
|
|
WECHAT_DIR="$ROOT_DIR/frontend/wechat"
|
|
BACKEND_DIR="$ROOT_DIR/backend_v1"
|
|
DOCKER_IMAGE="${DOCKER_IMAGE:-rogeecn/quyun}"
|
|
NPM_REGISTRY="${NPM_REGISTRY:-https://npm.hub.ipao.vip}"
|
|
GO_PROXY="${GO_PROXY:-https://go.hub.ipao.vip}" # fallback adds ,direct when exported
|
|
GO_NO_PROXY="${GO_NO_PROXY:-git.ipao.vip}"
|
|
IMAGE_ARCHIVE="${IMAGE_ARCHIVE:-$ROOT_DIR/quyun.v2.tgz}"
|
|
|
|
log() {
|
|
printf '\n==> %s\n' "$1"
|
|
}
|
|
|
|
die() {
|
|
printf 'Error: %s\n' "$1" >&2
|
|
exit 1
|
|
}
|
|
|
|
ensure_bun() {
|
|
if command -v bun >/dev/null 2>&1; then
|
|
return
|
|
fi
|
|
log "Installing bun via npm"
|
|
npm install -g bun || die "Failed to install bun"
|
|
}
|
|
|
|
run_frontend_build() {
|
|
local dir="$1"
|
|
local label="$2"
|
|
|
|
[[ -d "$dir" ]] || die "Missing frontend directory $dir"
|
|
log "Building ${label}"
|
|
pushd "$dir" >/dev/null
|
|
bun install
|
|
bun run build
|
|
popd >/dev/null
|
|
}
|
|
|
|
build_backend() {
|
|
[[ -d "$BACKEND_DIR" ]] || die "Missing backend directory"
|
|
log "Tidying Go modules"
|
|
pushd "$BACKEND_DIR" >/dev/null
|
|
export GOPROXY="${GO_PROXY},direct"
|
|
export GONOPROXY="$GO_NO_PROXY"
|
|
export GONOSUMDB="$GO_NO_PROXY"
|
|
go mod tidy
|
|
log "Building Go binary"
|
|
mkdir -p build
|
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/app .
|
|
popd >/dev/null
|
|
}
|
|
|
|
build_image() {
|
|
log "Building Docker image ${DOCKER_IMAGE}:v2"
|
|
sudo docker build -f Dockerfile.v2 -t "${DOCKER_IMAGE}:v2" "$ROOT_DIR"
|
|
}
|
|
|
|
export_image() {
|
|
log "Exporting image to ${IMAGE_ARCHIVE}"
|
|
sudo docker save "${DOCKER_IMAGE}:v2" | gzip -c > "$IMAGE_ARCHIVE"
|
|
}
|
|
|
|
main() {
|
|
export NPM_CONFIG_REGISTRY="$NPM_REGISTRY"
|
|
export npm_config_registry="$NPM_REGISTRY"
|
|
|
|
ensure_bun
|
|
run_frontend_build "$ADMIN_DIR" "frontend/Admin"
|
|
run_frontend_build "$WECHAT_DIR" "frontend/Wechat"
|
|
build_backend
|
|
build_image
|
|
export_image
|
|
log "Build finished"
|
|
}
|
|
|
|
main "$@"
|