fix: 修复一直重启、构建bug
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled

This commit is contained in:
lzh
2026-01-13 22:04:33 +08:00
parent b7664f7dd0
commit 1f03c44a39
2 changed files with 73 additions and 24 deletions

85
Jenkinsfile vendored
View File

@@ -80,24 +80,33 @@ pipeline {
steps {
script {
def services = env.SERVICES_TO_BUILD.split(',') as List
def batchSize = MAX_PARALLEL.toInteger()
echo "🔨 Building ${services.size()} services with parallelism=${batchSize}"
// 分批并行构建
services.collate(batchSize).each { batch ->
echo "📦 Building batch: ${batch.join(', ')}"
def parallelBuilds = [:]
batch.each { service ->
parallelBuilds[service] = {
buildAndPush(service)
}
}
parallel parallelBuilds
echo "🔨 Building ${services.size()} services"
// 串行构建(避免并发问题)
services.each { service ->
def modulePath = getModulePath(service)
echo "🔨 Building ${service}..."
sh """
docker build \
-f docker/Dockerfile.service \
--build-arg DEPS_IMAGE=${DEPS_IMAGE} \
--build-arg MODULE_NAME=${modulePath} \
--build-arg JAR_NAME=${service} \
--build-arg SKIP_TESTS=true \
-t ${REGISTRY}/${service}:${IMAGE_TAG} \
-t ${REGISTRY}/${service}:latest \
.
docker push ${REGISTRY}/${service}:${IMAGE_TAG}
docker push ${REGISTRY}/${service}:latest
"""
echo "✅ ${service} built and pushed"
}
// 清理
sh "docker image prune -f || true"
}
@@ -130,7 +139,47 @@ pipeline {
// 串行部署(保证依赖关系)
sortedServices.each { service ->
deployService(service)
echo "🚀 Deploying ${service}..."
def containerName = getContainerName(service)
def sshKey = '/var/jenkins_home/.ssh/id_rsa'
def sshOpts = "-o StrictHostKeyChecking=no -o ConnectTimeout=10 -i ${sshKey}"
sh """
ssh ${sshOpts} root@${DEPLOY_HOST} '
cd ${DEPLOY_PATH}
echo "Pulling ${service}..."
docker compose -f docker-compose.core.yml pull ${service}
echo "Starting ${service}..."
docker compose -f docker-compose.core.yml up -d ${service}
echo "Waiting for ${service} to be healthy..."
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
STATUS=\$(docker inspect --format="{{.State.Health.Status}}" ${containerName} 2>/dev/null || echo "starting")
if [ "\$STATUS" = "healthy" ]; then
echo "✅ ${service} is healthy"
exit 0
elif [ "\$STATUS" = "unhealthy" ]; then
echo "❌ ${service} is unhealthy"
echo "=== Last 100 lines of logs ==="
docker logs --tail 100 ${containerName}
exit 1
fi
echo "⏳ ${service} status: \$STATUS (\$((i*10))s/200s)"
sleep 10
done
echo "❌ ${service} health check timeout after 200s"
echo "=== Full logs ==="
docker logs ${containerName}
exit 1
'
"""
echo "✅ ${service} deployed successfully"
}
echo "🚀 All services deployed successfully!"