trace/.gitea/workflows/deploy.yaml
Lee a6f5ea721d
All checks were successful
Java Maven 3.9.9 & JDK 26 CI/CD Pipeline / build-and-deploy (push) Successful in 8m6s
修复 HK-1 Actions 容器网络
2026-08-19 18:04:33 +08:00

81 lines
3.2 KiB
YAML
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.

name: Java Maven 3.9.9 & JDK 26 CI/CD Pipeline
on:
push:
branches:
- dev # 只有推送到 main 分支时才触发自动部署
env:
IMAGE_NAME: trace-img
CONTAINER_NAME: trace-container
APP_PORT: 8080
jobs:
build-and-deploy:
# 调度我们在之前的 runner 中注册的宿主机 Docker 环境
runs-on: centos-env
steps:
# 替换为以下纯命令形式docker:cli 镜像中默认自带 git
- name: 1. 拉取最新的仓库代码 (纯 Git 命令行模式)
shell: sh
run: |
# 清空当前工作目录,防止历史残留
rm -rf ./*
# 利用 Gitea 自动注入的内置变量,动态克隆当前仓库、当前分支、当前提交
git clone --depth 1 -b ${{ gitea.ref_name }} http://gitea:3000/${{ gitea.repository }} .
git checkout ${{ gitea.sha }}
- name: 2. 触发 Docker 容器内编译与镜像打包
shell: sh
run: |
echo "🚀 开始构建 JDK 26 镜像: ${IMAGE_NAME}:${{ gitea.sha }}"
DOCKER_BUILDKIT=0 docker build --memory=700m -t ${IMAGE_NAME}:${{ gitea.sha }} .
docker tag ${IMAGE_NAME}:${{ gitea.sha }} ${IMAGE_NAME}:latest
- name: 3. 旧容器清理与新容器平滑重启
shell: sh
run: |
echo "🧹 正在清理旧版本的容器..."
docker stop ${CONTAINER_NAME} || true
docker rm ${CONTAINER_NAME} || true
echo "🔄 正在启动全新 JDK 26 容器环境..."
docker run -d \
--name ${CONTAINER_NAME} \
--network tracecd-net \
-p 127.0.0.1:${APP_PORT}:${APP_PORT} \
--restart unless-stopped \
--memory 384m \
--memory-swap 768m \
--cpus 0.75 \
--pids-limit 256 \
--env-file /opt/tracecd/app.env \
-e JAVA_TOOL_OPTIONS="-Xms64m -Xmx256m -XX:+UseSerialGC" \
${IMAGE_NAME}:latest
- name: 4. 服务健康检查 (HTTP 探活)
shell: sh
run: |
echo "🔍 开始对端口 ${APP_PORT} 进行健康状态探活..."
# 循环探活:每隔 5 秒检测一次,最多尝试 15 次(总计 75 秒,给高版本 JVM 充足的预热时间)
for i in 1 2 3 4 5 6; do
# 发送轻量级请求只要服务响应了无论是200还是Spring默认的404/无路由),都证明 Tomcat/Netty 已拉起
if wget -q --spider http://${CONTAINER_NAME}:${APP_PORT}/; then
echo "✅ [SUCCESS] 探活成功Java 26 服务已成功拉起。"
echo "🧹 开始安全地清理历史无用镜像及过期构建缓存..."
docker image prune -f
docker builder prune --filter "until=24h" -f
exit 0
fi
echo "⏳ [WAITING] 服务仍在启动中,等待 5 秒后重试 ($i/6)..."
sleep 5
done
echo "❌ [ERROR] 探活失败!服务在 30 秒内未能建立连接,可能发生了启动闪退。"
echo "===== 以下为容器崩溃前的实时日志 ====="
docker logs ${CONTAINER_NAME}
exit 1