fix: Fix Jenkinsfile syntax errors and optimize for pre-installed Buildx

Fixes:
-  Fix missing indentation on line 127 (catch block)
-  Fix stage indentation issues
-  Remove automatic Buildx installation since it's pre-installed on server

Optimizations:
-  Use existing Docker Buildx v0.25.0 installation
-  Preserve buildx builder instance across builds for better performance
-  Improved builder instance lifecycle management
-  Better error handling and logging

The pipeline should now parse correctly and utilize the manually installed Buildx.
This commit is contained in:
wangtianqi 2025-06-24 09:02:35 +08:00
parent fea1b828ec
commit 269496438a

42
Jenkinsfile vendored
View File

@ -121,9 +121,9 @@ pipeline {
-Dsonar.login=${SONAR_TOKEN} \
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
"""
echo "✅ SonarQube代码扫描完成"
} catch (Exception e) { echo "⚠️ SonarQube扫描失败继续构建流程: ${e.getMessage()}"
echo "✅ SonarQube代码扫描完成"
} catch (Exception e) {
echo "⚠️ SonarQube扫描失败继续构建流程: ${e.getMessage()}"
}
}
}
@ -156,21 +156,18 @@ pipeline {
sh 'docker image prune -f || true'
echo "开始构建Docker镜像: ${IMAGE_NAME}:${IMAGE_TAG}"
// 启用Docker Buildx并创建构建器
// 启用Docker Buildx并创建构建器
sh '''
# 检查并安装buildx如果需要
if ! docker buildx version; then
echo "Installing Docker Buildx..."
DOCKER_BUILDX_VERSION=v0.12.1
curl -L https://github.com/docker/buildx/releases/download/${DOCKER_BUILDX_VERSION}/buildx-${DOCKER_BUILDX_VERSION}.linux-amd64 -o docker-buildx
chmod +x docker-buildx
mkdir -p ~/.docker/cli-plugins
mv docker-buildx ~/.docker/cli-plugins/
fi
echo "✅ 使用已安装的 Docker Buildx $(docker buildx version)"
# 创建并使用新的构建器实例
docker buildx create --name jenkins-builder --use --bootstrap || docker buildx use jenkins-builder
# 创建并使用新的构建器实例(如果不存在)
if ! docker buildx inspect jenkins-builder >/dev/null 2>&1; then
echo "创建新的构建器实例..."
docker buildx create --name jenkins-builder --use --bootstrap
else
echo "使用现有构建器实例..."
docker buildx use jenkins-builder
fi
# 验证构建器状态
docker buildx inspect --bootstrap
@ -423,14 +420,13 @@ EOF
echo message
}
}
unstable {
unstable {
echo '⚠️ 构建不稳定,可能存在测试失败'
}
cleanup {
cleanup {
script {
try {
// 清理Docker构建缓存防止磁盘空间不足
try { // 清理Docker构建缓存防止磁盘空间不足
sh '''
# 清理buildx缓存
rm -rf /tmp/.buildx-cache* || true
@ -438,8 +434,8 @@ EOF
# 清理未使用的Docker资源
docker system prune -f || true
# 移除构建器(如果存在)
docker buildx rm jenkins-builder || true
# 保留构建器实例以提高后续构建速度
echo "保留构建器实例jenkins-builder以优化后续构建"
'''
// 清理工作空间