aslan-server/.deploy/aslan-deploy/sync-and-deploy.sh
2026-07-06 16:49:25 +08:00

77 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
DEPLOY_HOST="${DEPLOY_HOST:-43.160.245.220}"
DEPLOY_USER="${DEPLOY_USER:-ubuntu}"
SSH_KEY="${SSH_KEY:-$HOME/.ssh/aslan-deploy-sg-ed25519}"
REMOTE_BASE="${REMOTE_BASE:-/opt/aslan-deploy}"
REMOTE_SRC="$REMOTE_BASE/source/likei-services"
LOCAL_M2_ROOT="${LOCAL_M2_ROOT:-$HOME/.m2/repository}"
REMOTE_M2_ROOT="${REMOTE_M2_ROOT:-/home/$DEPLOY_USER/.m2/repository}"
M2_CACHE_GROUPS="${M2_CACHE_GROUPS:-com/red/circle com/github/sud}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
usage() {
cat <<'EOF'
Usage:
.deploy/aslan-deploy/sync-and-deploy.sh [remote deploy args...]
Examples:
.deploy/aslan-deploy/sync-and-deploy.sh status
.deploy/aslan-deploy/sync-and-deploy.sh --mode preload other
.deploy/aslan-deploy/sync-and-deploy.sh --mode preload other external console
Environment:
DEPLOY_HOST default 43.160.245.220
DEPLOY_USER default ubuntu
SSH_KEY default ~/.ssh/aslan-deploy-sg-ed25519
Git source on deploy host:
ssh -i ~/.ssh/aslan-deploy-sg-ed25519 ubuntu@43.160.245.220 \
'USE_GIT_SOURCE=1 GIT_REF=atyou_prod /opt/aslan-deploy/deploy-likei-services.sh --mode preload other'
EOF
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
ssh_base=(ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new "$DEPLOY_USER@$DEPLOY_HOST")
rsync_ssh="ssh -i $SSH_KEY -o StrictHostKeyChecking=accept-new"
"${ssh_base[@]}" "mkdir -p '$REMOTE_SRC' '$REMOTE_BASE'"
rsync -az --delete \
--exclude '.git/' \
--exclude 'target/' \
--exclude '**/target/' \
--exclude '.idea/' \
--exclude '.DS_Store' \
--exclude 'build.md' \
--exclude 'node_modules/' \
-e "$rsync_ssh" \
"$REPO_ROOT/" "$DEPLOY_USER@$DEPLOY_HOST:$REMOTE_SRC/"
rsync -az \
-e "$rsync_ssh" \
"$SCRIPT_DIR/deploy-likei-services.sh" "$DEPLOY_USER@$DEPLOY_HOST:$REMOTE_BASE/deploy-likei-services.sh"
for cache_group in $M2_CACHE_GROUPS; do
local_cache_path="$LOCAL_M2_ROOT/$cache_group"
remote_cache_parent="$REMOTE_M2_ROOT/$(dirname "$cache_group")"
if [[ ! -d "$local_cache_path" ]]; then
continue
fi
"${ssh_base[@]}" "mkdir -p '$remote_cache_parent'"
rsync -az --delete \
--exclude '*.lastUpdated' \
-e "$rsync_ssh" \
"$local_cache_path" "$DEPLOY_USER@$DEPLOY_HOST:$remote_cache_parent/"
done
printf -v remote_script_q '%q' "$REMOTE_BASE/deploy-likei-services.sh"
printf -v remote_args_q '%q ' "$@"
"${ssh_base[@]}" "chmod +x $remote_script_q && $remote_script_q $remote_args_q"