/** * 容器镜像构建工具类 * 遵循阿里开发手册、大厂CI/CD标准 * * @param config 构建配置 * - services: 服务列表 * - imageRegistry: 镜像仓库地址 * - environment: 环境标识 * - version: 版本号 */ def call(Map config = [:]) { // 默认配置 def services = config.services ?: ['api-gateway', 'auth-service', 'user-service', 'map-service', 'order-service', 'merchant-service', 'platform-service', 'product-service', 'rider-service', 'upload-service', 'marketing-service'] def imageRegistry = config.imageRegistry ?: env.IMAGE_REGISTRY def environment = config.environment ?: env.ENVIRONMENT def version = config.version ?: env.VERSION def startTime = System.currentTimeMillis() echo "📦 构建容器镜像" // 并行构建镜像(每批3个,避免资源争抢) def batchSize = 3 def batches = services.collate(batchSize) def batchNum = 1 batches.each { batch -> echo "🔨 并行构建批次 ${batchNum}: ${batch.join(', ')}" parallel batch.collectEntries { service -> ["build-image-${service}": { def imageTag = "${imageRegistry}/${service}-${environment}:${version}" echo "🔨 构建 ${service} 镜像: ${imageTag}" sh """ cd \${WORKSPACE} cp services/${service}/target/*.jar services/${service}/app.jar 2>/dev/null || true ssh -i /var/lib/jenkins/.ssh/id_rsa -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@localhost "cd \${WORKSPACE} && podman build \\ -f services/${service}/Containerfile \\ -t ${imageTag} \\ services/${service}/" || { echo "使用通用Dockerfile..." ssh -i /var/lib/jenkins/.ssh/id_rsa -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@localhost "cd \${WORKSPACE} && podman build \\ -f infrastructure/compose/Dockerfile.backend \\ -t ${imageTag} \\ --build-arg JAR_FILE=*.jar \\ services/${service}/" } """ echo "✅ ${service} 镜像构建成功" }] } batchNum++ } def duration = (System.currentTimeMillis() - startTime) / 1000 echo "⏱️ 镜像构建总耗时: ${duration}秒" echo "✅ 所有镜像构建成功" }