ci(#90): CI/CD流水线 - lint/test/build镜像/SSH自动部署+健康检查回滚+企微通知
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# 生产部署脚本(Issue #90):拉取镜像 -> 滚动更新 -> 健康检查 -> 失败回滚
|
||||
set -euo pipefail
|
||||
|
||||
COMMIT_SHA="${1:-latest}"
|
||||
REGISTRY="${REGISTRY:-registry.xayunmei.local}"
|
||||
IMAGE="water-management-system"
|
||||
APP_DIR="/opt/wms"
|
||||
HEALTH_URL="http://127.0.0.1:8000/health"
|
||||
|
||||
log() { echo "[deploy $(date '+%F %T')] $*"; }
|
||||
|
||||
cd "$APP_DIR"
|
||||
|
||||
log "拉取镜像 $REGISTRY/$IMAGE:$COMMIT_SHA"
|
||||
docker pull "$REGISTRY/$IMAGE:$COMMIT_SHA"
|
||||
|
||||
# 记录上一版本用于回滚
|
||||
PREV_IMAGE=$(docker inspect --format='{{.Config.Image}}' wms-app 2>/dev/null || echo "")
|
||||
echo "$PREV_IMAGE" > .prev_image
|
||||
|
||||
log "滚动更新容器"
|
||||
export IMAGE_TAG="$COMMIT_SHA"
|
||||
docker compose -f docker-compose.yml -f deploy/production/docker-compose.override.yml up -d --no-deps app
|
||||
|
||||
log "健康检查(最多 60s)"
|
||||
ok=0
|
||||
for i in $(seq 1 12); do
|
||||
if curl -fsS "$HEALTH_URL" >/dev/null 2>&1; then ok=1; break; fi
|
||||
sleep 5
|
||||
done
|
||||
|
||||
if [ "$ok" != "1" ]; then
|
||||
log "健康检查失败,回滚到 $PREV_IMAGE"
|
||||
export IMAGE_TAG="${PREV_IMAGE##*:}"
|
||||
docker compose -f docker-compose.yml -f deploy/production/docker-compose.override.yml up -d --no-deps app
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "部署成功:$COMMIT_SHA"
|
||||
docker image prune -f >/dev/null 2>&1 || true
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CI 结果企业微信机器人通知(Issue #90)。"""
|
||||
import argparse, json, os, sys, urllib.request
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--status", required=True)
|
||||
ap.add_argument("--commit", default="")
|
||||
args = ap.parse_args()
|
||||
webhook = os.environ.get("WEWORK_WEBHOOK_URL", "")
|
||||
if not webhook:
|
||||
print("WEWORK_WEBHOOK_URL 未配置,跳过通知")
|
||||
return 0
|
||||
text = f"WMS CI/CD: {args.status} (commit {args.commit[:8]})"
|
||||
req = urllib.request.Request(
|
||||
webhook,
|
||||
data=json.dumps({"msgtype": "text", "text": {"content": text}}).encode(),
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
print("notify sent:", resp.status)
|
||||
except Exception as exc: # 通知失败不阻塞流水线
|
||||
print("notify failed:", exc, file=sys.stderr)
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user