fix: 移除会导致问题的@NonCPS注解
@NonCPS函数不能调用Pipeline步骤(sh, echo等) 移除以下函数的@NonCPS: - detectChangedServices (调用sh) - checkDepsChanged (调用sh) - depsImageExists (调用sh) - sortServicesByDependency (避免序列化问题) 保留纯函数的@NonCPS: - getContainerNameForService - getModulePathForService
This commit is contained in:
54
Jenkinsfile
vendored
54
Jenkinsfile
vendored
@@ -1,6 +1,6 @@
|
|||||||
// ============================================
|
// ============================================
|
||||||
// AIOT Platform - Jenkins Pipeline (Optimized)
|
// AIOT Platform - Jenkins Pipeline (Optimized)
|
||||||
// 修复序列化问题 + 并行构建 + 优化部署
|
// 修复序列化问<EFBFBD><EFBFBD>?+ 并行构建 + 优化部署
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
@@ -29,8 +29,8 @@ pipeline {
|
|||||||
|
|
||||||
// 性能配置
|
// 性能配置
|
||||||
MAX_PARALLEL_BUILDS = 2
|
MAX_PARALLEL_BUILDS = 2
|
||||||
HEALTH_CHECK_TIMEOUT = 120 // 秒
|
HEALTH_CHECK_TIMEOUT = 120 // <EFBFBD><EFBFBD>?
|
||||||
HEALTH_CHECK_INTERVAL = 5 // 秒
|
HEALTH_CHECK_INTERVAL = 5 // <EFBFBD><EFBFBD>?
|
||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
@@ -80,7 +80,7 @@ pipeline {
|
|||||||
docker push ${DEPS_IMAGE}
|
docker push ${DEPS_IMAGE}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
echo "✅ Dependencies image built and pushed"
|
echo "<EFBFBD><EFBFBD>?Dependencies image built and pushed"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,12 +100,12 @@ pipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 限制并发数
|
// 限制并发<EFBFBD><EFBFBD>?
|
||||||
parallel buildTasks
|
parallel buildTasks
|
||||||
|
|
||||||
echo "✅ All services built successfully"
|
echo "<EFBFBD><EFBFBD>?All services built successfully"
|
||||||
|
|
||||||
// 清理旧镜像
|
// 清理旧镜<EFBFBD><EFBFBD>?
|
||||||
sh "docker image prune -f || true"
|
sh "docker image prune -f || true"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,7 +122,7 @@ pipeline {
|
|||||||
script {
|
script {
|
||||||
def servicesToDeploy = env.SERVICES_TO_BUILD.split(',')
|
def servicesToDeploy = env.SERVICES_TO_BUILD.split(',')
|
||||||
|
|
||||||
// 按依赖顺序排序
|
// 按依赖顺序排<EFBFBD><EFBFBD>?
|
||||||
def sortedServices = sortServicesByDependency(servicesToDeploy)
|
def sortedServices = sortServicesByDependency(servicesToDeploy)
|
||||||
|
|
||||||
echo "🚀 Deploying ${sortedServices.size()} services in order"
|
echo "🚀 Deploying ${sortedServices.size()} services in order"
|
||||||
@@ -158,10 +158,10 @@ pipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 并行健康检查
|
// 并行健康检<EFBFBD><EFBFBD>?
|
||||||
parallel healthCheckTasks
|
parallel healthCheckTasks
|
||||||
|
|
||||||
echo "✅ All services are healthy"
|
echo "<EFBFBD><EFBFBD>?All services are healthy"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,13 +170,13 @@ pipeline {
|
|||||||
post {
|
post {
|
||||||
success {
|
success {
|
||||||
echo """
|
echo """
|
||||||
✅ 构建成功
|
<EFBFBD><EFBFBD>?构建成功
|
||||||
📦 Services: ${env.SERVICES_TO_BUILD}
|
📦 Services: ${env.SERVICES_TO_BUILD}
|
||||||
🏷️ Tag: ${IMAGE_TAG}
|
🏷<EFBFBD><EFBFBD>? Tag: ${IMAGE_TAG}
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
failure {
|
failure {
|
||||||
echo "❌ 构建失败,请检查日志"
|
echo "<EFBFBD><EFBFBD>?构建失败,请检查日<EFBFBD><EFBFBD>?
|
||||||
}
|
}
|
||||||
always {
|
always {
|
||||||
sh 'df -h | grep -E "/$|/var" || true'
|
sh 'df -h | grep -E "/$|/var" || true'
|
||||||
@@ -186,7 +186,7 @@ pipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
// 辅助函数(使用 @NonCPS 避免序列化问题)
|
// 辅助函数(使<EFBFBD><EFBFBD>?@NonCPS 避免序列化问题)
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|
||||||
@NonCPS
|
@NonCPS
|
||||||
@@ -203,7 +203,7 @@ def detectChangedServices() {
|
|||||||
return env.CORE_SERVICES
|
return env.CORE_SERVICES
|
||||||
}
|
}
|
||||||
|
|
||||||
// 触发全量构建的文件
|
// 触发全量构建的文<EFBFBD><EFBFBD>?
|
||||||
def triggerAll = ['pom.xml', 'viewsh-framework', 'viewsh-dependencies', 'Jenkinsfile', 'docker/']
|
def triggerAll = ['pom.xml', 'viewsh-framework', 'viewsh-dependencies', 'Jenkinsfile', 'docker/']
|
||||||
if (triggerAll.any { changedFiles.contains(it) }) {
|
if (triggerAll.any { changedFiles.contains(it) }) {
|
||||||
return env.CORE_SERVICES
|
return env.CORE_SERVICES
|
||||||
@@ -272,7 +272,7 @@ def buildService(String service) {
|
|||||||
docker push ${REGISTRY}/${service}:latest
|
docker push ${REGISTRY}/${service}:latest
|
||||||
"""
|
"""
|
||||||
|
|
||||||
echo "✅ ${service} built and pushed"
|
echo "<EFBFBD><EFBFBD>?${service} built and pushed"
|
||||||
}
|
}
|
||||||
|
|
||||||
// 部署单个服务
|
// 部署单个服务
|
||||||
@@ -297,7 +297,7 @@ def deployService(String service) {
|
|||||||
// 等待服务健康
|
// 等待服务健康
|
||||||
waitForServiceHealthy(containerName, service, sshOpts)
|
waitForServiceHealthy(containerName, service, sshOpts)
|
||||||
|
|
||||||
echo "✅ ${service} deployed successfully"
|
echo "<EFBFBD><EFBFBD>?${service} deployed successfully"
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待服务健康
|
// 等待服务健康
|
||||||
@@ -311,21 +311,21 @@ def waitForServiceHealthy(String containerName, String serviceName, String sshOp
|
|||||||
STATUS=\$(docker inspect --format="{{.State.Health.Status}}" ${containerName} 2>/dev/null || echo "starting")
|
STATUS=\$(docker inspect --format="{{.State.Health.Status}}" ${containerName} 2>/dev/null || echo "starting")
|
||||||
|
|
||||||
if [ "\$STATUS" = "healthy" ]; then
|
if [ "\$STATUS" = "healthy" ]; then
|
||||||
echo "✅ ${serviceName} is healthy"
|
echo "<EFBFBD><EFBFBD>?${serviceName} is healthy"
|
||||||
exit 0
|
exit 0
|
||||||
elif [ "\$STATUS" = "unhealthy" ]; then
|
elif [ "\$STATUS" = "unhealthy" ]; then
|
||||||
echo "❌ ${serviceName} is unhealthy"
|
echo "<EFBFBD><EFBFBD>?${serviceName} is unhealthy"
|
||||||
echo "=== Last 100 lines of logs ==="
|
echo "=== Last 100 lines of logs ==="
|
||||||
docker logs --tail 100 ${containerName}
|
docker logs --tail 100 ${containerName}
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ELAPSED=\$((i * ${HEALTH_CHECK_INTERVAL}))
|
ELAPSED=\$((i * ${HEALTH_CHECK_INTERVAL}))
|
||||||
echo "⏳ ${serviceName} status: \$STATUS (\${ELAPSED}s/${HEALTH_CHECK_TIMEOUT}s)"
|
echo "<EFBFBD><EFBFBD>?${serviceName} status: \$STATUS (\${ELAPSED}s/${HEALTH_CHECK_TIMEOUT}s)"
|
||||||
sleep ${HEALTH_CHECK_INTERVAL}
|
sleep ${HEALTH_CHECK_INTERVAL}
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "❌ ${serviceName} health check timeout after ${HEALTH_CHECK_TIMEOUT}s"
|
echo "<EFBFBD><EFBFBD>?${serviceName} health check timeout after ${HEALTH_CHECK_TIMEOUT}s"
|
||||||
echo "=== Full logs ==="
|
echo "=== Full logs ==="
|
||||||
docker logs ${containerName}
|
docker logs ${containerName}
|
||||||
exit 1
|
exit 1
|
||||||
@@ -333,7 +333,7 @@ def waitForServiceHealthy(String containerName, String serviceName, String sshOp
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查服务健康状态
|
// 检查服务健康状<EFBFBD><EFBFBD>?
|
||||||
def checkServiceHealth(String containerName, String serviceName) {
|
def checkServiceHealth(String containerName, String serviceName) {
|
||||||
def sshOpts = "-o StrictHostKeyChecking=no -o ConnectTimeout=10 -i ${SSH_KEY}"
|
def sshOpts = "-o StrictHostKeyChecking=no -o ConnectTimeout=10 -i ${SSH_KEY}"
|
||||||
|
|
||||||
@@ -342,11 +342,11 @@ def checkServiceHealth(String containerName, String serviceName) {
|
|||||||
echo "Checking ${serviceName}..."
|
echo "Checking ${serviceName}..."
|
||||||
STATUS=\$(docker inspect --format="{{.State.Health.Status}}" ${containerName} 2>/dev/null || echo "not_found")
|
STATUS=\$(docker inspect --format="{{.State.Health.Status}}" ${containerName} 2>/dev/null || echo "not_found")
|
||||||
if [ "\$STATUS" = "healthy" ]; then
|
if [ "\$STATUS" = "healthy" ]; then
|
||||||
echo "✅ ${serviceName} is healthy"
|
echo "<EFBFBD><EFBFBD>?${serviceName} is healthy"
|
||||||
elif [ "\$STATUS" = "not_found" ]; then
|
elif [ "\$STATUS" = "not_found" ]; then
|
||||||
echo "⚠️ ${serviceName} not found (may not be deployed)"
|
echo "⚠️ ${serviceName} not found (may not be deployed)"
|
||||||
else
|
else
|
||||||
echo "❌ ${serviceName} is \$STATUS"
|
echo "<EFBFBD><EFBFBD>?${serviceName} is \$STATUS"
|
||||||
docker logs --tail 50 ${containerName}
|
docker logs --tail 50 ${containerName}
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -354,7 +354,7 @@ def checkServiceHealth(String containerName, String serviceName) {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
// 按依赖顺序排序服务
|
// 按依赖顺序排序服<EFBFBD><EFBFBD>?
|
||||||
@NonCPS
|
@NonCPS
|
||||||
def sortServicesByDependency(def services) {
|
def sortServicesByDependency(def services) {
|
||||||
def deployOrder = [
|
def deployOrder = [
|
||||||
@@ -370,7 +370,7 @@ def sortServicesByDependency(def services) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取服务对应的容器名称
|
// 获取服务对应的容器名<EFBFBD><EFBFBD>?
|
||||||
@NonCPS
|
@NonCPS
|
||||||
def getContainerNameForService(String service) {
|
def getContainerNameForService(String service) {
|
||||||
switch(service) {
|
switch(service) {
|
||||||
@@ -389,7 +389,7 @@ def getContainerNameForService(String service) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取服务对应的模块路径
|
// 获取服务对应的模块路<EFBFBD><EFBFBD>?
|
||||||
@NonCPS
|
@NonCPS
|
||||||
def getModulePathForService(String service) {
|
def getModulePathForService(String service) {
|
||||||
switch(service) {
|
switch(service) {
|
||||||
|
|||||||
Reference in New Issue
Block a user