Files
opc-manager/scripts/deploy.sh

85 lines
2.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# OPC Manager 标准部署脚本
# 用法: ./scripts/deploy.sh <version> [message]
# 示例: ./scripts/deploy.sh v0.1.1 "修复首页样式"
set -euo pipefail
VERSION="${1:-}"
MESSAGE="${2:-Release $VERSION}"
if [ -z "$VERSION" ]; then
echo "用法: ./scripts/deploy.sh <version> [message]"
echo "示例: ./scripts/deploy.sh v0.1.1"
exit 1
fi
TAG="opc-manager-$VERSION"
REMOTE_HOST="${OPC_REMOTE_HOST:-business}"
REMOTE_PATH="${OPC_REMOTE_PATH:-/opt/opc-manager}"
SERVICE_NAME="${OPC_SERVICE_NAME:-opc-manager.service}"
HEALTH_URL="${OPC_HEALTH_URL:-https://opc.yxcowork.vip/api/health}"
HOME_URL="${OPC_HOME_URL:-https://opc.yxcowork.vip}"
echo "=== OPC Manager 部署 $VERSION ==="
# 1. 确保在项目根目录
cd "$(dirname "$0")/.."
# 2. 更新 VERSION_LOG.md
echo "" >> VERSION_LOG.md
echo "## $TAG - $(date +%F)" >> VERSION_LOG.md
echo "" >> VERSION_LOG.md
echo "- $MESSAGE" >> VERSION_LOG.md
echo "" >> VERSION_LOG.md
echo "VERSION_LOG.md 已更新"
# 3. Git commit
git add -A
if git diff --cached --quiet; then
echo "没有需要提交的变更,跳过 commit"
else
git commit -m "release: $TAG - $MESSAGE"
echo "已提交: release: $TAG"
fi
# 4. Git tag轻量 tag只打在 OPC 独立仓库)
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Tag $TAG 已存在,跳过"
else
git tag "$TAG"
echo "已打 tag: $TAG"
fi
# 5. Git push
echo "推送代码和 tag..."
git push origin main
git push origin "$TAG"
# 6. 服务器同步
echo "同步到服务器 $REMOTE_HOST..."
ssh "$REMOTE_HOST" "cd $REMOTE_PATH && git fetch origin && git reset --hard origin/main"
# 7. 重启服务
echo "重启 $SERVICE_NAME..."
ssh "$REMOTE_HOST" "sudo systemctl restart $SERVICE_NAME && sleep 2"
ssh "$REMOTE_HOST" "systemctl status $SERVICE_NAME --no-pager" | head -6
# 8. 健康检查
echo ""
echo "健康检查..."
sleep 2
HEALTH_RESULT=$(curl -s "$HEALTH_URL")
echo "$HEALTH_RESULT"
HOME_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$HOME_URL")
echo "首页 HTTP 状态: $HOME_CODE"
if [ "$HOME_CODE" = "200" ]; then
echo ""
echo "=== 部署 $VERSION 完成 ==="
else
echo ""
echo "!!! 警告:首页返回 $HOME_CODE,请检查 !!!"
fi