ci: switch prod deploy to server-side git pull and go build
This commit is contained in:
@@ -39,7 +39,7 @@ health_check() {
|
||||
}
|
||||
|
||||
restart_service() {
|
||||
if command -v systemctl >/dev/null 2>&1 && systemctl list-unit-files | grep -q "^${SERVICE_NAME}.service"; then
|
||||
if command -v systemctl >/dev/null 2>&1 && systemctl cat "${SERVICE_NAME}.service" >/dev/null 2>&1; then
|
||||
log "restarting systemd service: ${SERVICE_NAME}"
|
||||
systemctl restart "$SERVICE_NAME"
|
||||
else
|
||||
@@ -58,7 +58,7 @@ create_service_if_needed() {
|
||||
return 0
|
||||
fi
|
||||
|
||||
if systemctl list-unit-files | grep -q "^${SERVICE_NAME}.service"; then
|
||||
if systemctl cat "${SERVICE_NAME}.service" >/dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
|
||||
Executable
+84
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
APP_DIR="${APP_DIR:-/www/wwwroot/wx_service}"
|
||||
DEPLOY_REF="${DEPLOY_REF:-main}"
|
||||
GO_VERSION="${GO_VERSION:-1.23.6}"
|
||||
GO_ROOT="${GO_ROOT:-/usr/local/go}"
|
||||
GO_BIN="${GO_BIN:-${GO_ROOT}/bin/go}"
|
||||
TMP_BUILD_BIN="${TMP_BUILD_BIN:-/tmp/wx_service-${DEPLOY_REF:0:12}}"
|
||||
|
||||
DIST_DIR="${DIST_DIR:-${APP_DIR}/dist}"
|
||||
SERVICE_NAME="${SERVICE_NAME:-wx_service}"
|
||||
RUN_USER="${RUN_USER:-www}"
|
||||
RUN_GROUP="${RUN_GROUP:-www}"
|
||||
PORT="${PORT:-8080}"
|
||||
RELEASE_ID="${RELEASE_ID:-${DEPLOY_REF}}"
|
||||
|
||||
log() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
|
||||
}
|
||||
|
||||
install_go_if_needed() {
|
||||
local need_install="false"
|
||||
|
||||
if [ ! -x "$GO_BIN" ]; then
|
||||
need_install="true"
|
||||
else
|
||||
local current
|
||||
current="$($GO_BIN version 2>/dev/null | awk '{print $3}' | sed 's/^go//')"
|
||||
if [ -z "$current" ] || [ "$current" != "$GO_VERSION" ]; then
|
||||
need_install="true"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$need_install" = "false" ]; then
|
||||
log "go toolchain ok: $($GO_BIN version)"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log "installing go ${GO_VERSION}"
|
||||
local pkg="/tmp/go${GO_VERSION}.linux-amd64.tar.gz"
|
||||
curl -fL "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" -o "$pkg"
|
||||
rm -rf "$GO_ROOT"
|
||||
tar -C /usr/local -xzf "$pkg"
|
||||
rm -f "$pkg"
|
||||
log "go installed: $($GO_BIN version)"
|
||||
}
|
||||
|
||||
if [ ! -d "$APP_DIR/.git" ]; then
|
||||
echo "invalid app repo: $APP_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
install_go_if_needed
|
||||
|
||||
log "syncing repo to ${DEPLOY_REF}"
|
||||
git -C "$APP_DIR" fetch --all --prune
|
||||
git -C "$APP_DIR" reset --hard "$DEPLOY_REF"
|
||||
|
||||
export PATH="$GO_ROOT/bin:$PATH"
|
||||
export GOPROXY="${GOPROXY:-https://goproxy.cn,direct}"
|
||||
|
||||
log "downloading go modules"
|
||||
cd "$APP_DIR"
|
||||
"$GO_BIN" mod download
|
||||
|
||||
log "building binary"
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 "$GO_BIN" build -trimpath -ldflags "-s -w" -o "$TMP_BUILD_BIN" ./cmd/api
|
||||
chmod 755 "$TMP_BUILD_BIN"
|
||||
|
||||
log "publishing binary via deploy_binary.sh"
|
||||
APP_DIR="$APP_DIR" \
|
||||
DIST_DIR="$DIST_DIR" \
|
||||
SOURCE_BIN="$TMP_BUILD_BIN" \
|
||||
SERVICE_NAME="$SERVICE_NAME" \
|
||||
RUN_USER="$RUN_USER" \
|
||||
RUN_GROUP="$RUN_GROUP" \
|
||||
PORT="$PORT" \
|
||||
RELEASE_ID="$RELEASE_ID" \
|
||||
SYNC_CODE="false" \
|
||||
INSTALL_SERVICE="true" \
|
||||
bash "$APP_DIR/scripts/ops/deploy_binary.sh"
|
||||
|
||||
log "source deploy done"
|
||||
Reference in New Issue
Block a user