Browse Source

流水线优化

dujian 1 week ago
parent
commit
26ba8dffbd
1 changed files with 109 additions and 62 deletions
  1. 109 62
      docs/jenkins/Jenkinsfile-uat-build-deploy.groovy

+ 109 - 62
docs/jenkins/Jenkinsfile-uat-build-deploy.groovy

@@ -27,6 +27,70 @@ def filterHarborPushScope(List allServices, String scope) {
     error("Unknown HARBOR_PUSH_SCOPE: ${scope}")
 }
 
+def pushOneHarborImage(def script, Map svc, String reg, String proj, String latestTag, String buildTag,
+                      String baseImage, String dockerfile, String workspace) {
+    def jarName = "${svc.module}-1.0.0.jar"
+    def imageLatest = "${reg}/${proj}/${svc.repo}:${latestTag}"
+    def imageBuild = "${reg}/${proj}/${svc.repo}:${buildTag}"
+    def withLibFlag = svc.withLib ? 'true' : 'false'
+    script.sh """
+        set -e
+        export DOCKER_BUILDKIT=1
+        test -f ${workspace}/${svc.module}/target/${jarName}
+        cd ${workspace}/${svc.module}
+        rm -rf .jenkins_docker_ctx && mkdir -p .jenkins_docker_ctx/lib
+        cp -f target/${jarName} .jenkins_docker_ctx/${jarName}
+        if [ "${withLibFlag}" = "true" ] && [ -d target/lib ]; then
+          cp -rf target/lib/. .jenkins_docker_ctx/lib/
+        else
+          touch .jenkins_docker_ctx/lib/.keep
+        fi
+        cd .jenkins_docker_ctx
+        if docker pull ${imageLatest} 2>/dev/null; then
+          echo ">>> archive previous ${latestTag} -> ${buildTag}"
+          docker tag ${imageLatest} ${imageBuild}
+          docker push ${imageBuild}
+        fi
+        docker build -f ${workspace}/${dockerfile} \\
+          --build-arg BASE_IMAGE=${baseImage} \\
+          --build-arg JAR_FILE=${jarName} \\
+          --build-arg SERVER_PORT=${svc.port} \\
+          --build-arg WITH_LIB=${svc.withLib} \\
+          -t ${imageLatest} .
+        docker push ${imageLatest}
+        echo ">>> pushed ${imageLatest} (archived prior latest as ${buildTag} if any)"
+        docker rmi ${imageLatest} 2>/dev/null || true
+        cd ${workspace}/${svc.module}
+        rm -rf .jenkins_docker_ctx
+    """
+}
+
+def deployOneUatService(def script, String moduleName, String dirName, String workspace) {
+    def sourceJar = "${workspace}/${moduleName}/target/${moduleName}-1.0.0.jar"
+    def sourceLib = "${workspace}/${moduleName}/target/lib"
+    def targetDir = "/app_deploy_uat/${dirName}"
+    script.sh """
+        set -e
+        echo ">>> Deploy module: ${moduleName}"
+        if [ -f "${sourceJar}" ]; then
+            mkdir -p "${targetDir}"
+            if [ -d "${sourceLib}" ]; then
+                rm -rf "${targetDir}/lib"
+                cp -rf "${sourceLib}" "${targetDir}"
+            fi
+            cp -f "${sourceJar}" "${targetDir}/"
+            if docker ps -a --format '{{.Names}}' | grep -wq "${dirName}"; then
+                docker restart "${dirName}"
+                echo ">>> [${dirName}] restarted"
+            else
+                echo ">>> [${dirName}] container missing, jar copied only"
+            fi
+        else
+            echo ">>> [${dirName}] jar missing, skip"
+        fi
+    """
+}
+
 /** Delete oldest uat-build-* tags in Harbor, keep newest KEEP. Never deletes uat-latest or current build tag. */
 def pruneHarborUatTags(def script, String reg, String proj, List repoNames, int keepCount, String tagPrefix, String currentBuildTag, String latestTag) {
     if (repoNames == null || repoNames.isEmpty() || keepCount < 1) {
@@ -87,6 +151,7 @@ pipeline {
 
     options {
         buildDiscarder(logRotator(numToKeepStr: '15'))
+        disableConcurrentBuilds()
         timestamps()
         timeout(time: 90, unit: 'MINUTES')
     }
@@ -128,6 +193,11 @@ pipeline {
         )
         string(name: 'HARBOR_KEEP_TAG_COUNT', defaultValue: '10', trim: true,
                 description: 'How many uat-build-* tags to keep per repository')
+        booleanParam(
+                name: 'HARBOR_PUSH_PARALLEL',
+                defaultValue: true,
+                description: 'Parallel docker build/push per service (faster; needs enough CPU/disk on agent)'
+        )
     }
 
     environment {
@@ -139,6 +209,7 @@ pipeline {
         UAT_HARBOR_LATEST_TAG = 'uat-latest'
         UAT_HARBOR_BUILD_TAG = "uat-build-${env.BUILD_NUMBER}"
         DOCKERFILE_JAVA = 'docs/jenkins/produ/docker/Dockerfile.java-service'
+        MAVEN_LOCAL_REPO = '/var/jenkins_home/.m2/repository'
     }
 
     stages {
@@ -170,6 +241,14 @@ pipeline {
                     writeFile file: 'settings.xml', text: """<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
+    <mirrors>
+        <mirror>
+            <id>aliyun-central</id>
+            <mirrorOf>central</mirrorOf>
+            <name>Aliyun Central</name>
+            <url>https://maven.aliyun.com/repository/central</url>
+        </mirror>
+    </mirrors>
     <profiles>
         <profile>
             <id>repo-mix</id>
@@ -243,9 +322,11 @@ pipeline {
                             set -e
                             mvn -version
                             unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy no_proxy NO_PROXY || true
-                            export MAVEN_OPTS="-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true"
-                            mkdir -p /var/jenkins_home/.m2/repository
-                            mvn clean package -DskipTests -s settings.xml ${updateFlag} -e -Dmaven.repo.local=/var/jenkins_home/.m2/repository
+                            export MAVEN_OPTS="-Xms512m -Xmx2048m -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true"
+                            mkdir -p ${MAVEN_LOCAL_REPO}
+                            mvn clean package -DskipTests -s settings.xml ${updateFlag} -e \\
+                              -T 1C -Dmaven.artifact.threads=8 \\
+                              -Dmaven.repo.local=${MAVEN_LOCAL_REPO}
                         """
                     }
                 }
@@ -289,40 +370,25 @@ pipeline {
                             df -h /var/lib/docker 2>/dev/null || df -h / || true
                             docker system prune -f --filter until=48h 2>/dev/null || true
                         """
-                        harborServices.each { svc ->
-                            def jarName = "${svc.module}-1.0.0.jar"
-                            def imageLatest = "${reg}/${proj}/${svc.repo}:${latestTag}"
-                            def imageBuild = "${reg}/${proj}/${svc.repo}:${buildTag}"
-                            def withLibFlag = svc.withLib ? 'true' : 'false'
-                            sh """
-                                set -e
-                                test -f ${WORKSPACE}/${svc.module}/target/${jarName}
-                                cd ${WORKSPACE}/${svc.module}
-                                rm -rf .jenkins_docker_ctx && mkdir -p .jenkins_docker_ctx/lib
-                                cp -f target/${jarName} .jenkins_docker_ctx/${jarName}
-                                if [ "${withLibFlag}" = "true" ] && [ -d target/lib ]; then
-                                  cp -rf target/lib/. .jenkins_docker_ctx/lib/
-                                else
-                                  touch .jenkins_docker_ctx/lib/.keep
-                                fi
-                                cd .jenkins_docker_ctx
-                                if docker pull ${imageLatest} 2>/dev/null; then
-                                  echo ">>> archive previous ${latestTag} -> ${buildTag}"
-                                  docker tag ${imageLatest} ${imageBuild}
-                                  docker push ${imageBuild}
-                                fi
-                                docker build -f ${WORKSPACE}/${dockerfile} \\
-                                  --build-arg BASE_IMAGE=${baseImage} \\
-                                  --build-arg JAR_FILE=${jarName} \\
-                                  --build-arg SERVER_PORT=${svc.port} \\
-                                  --build-arg WITH_LIB=${svc.withLib} \\
-                                  -t ${imageLatest} .
-                                docker push ${imageLatest}
-                                echo ">>> pushed ${imageLatest} (archived prior latest as ${buildTag} if any)"
-                                docker rmi ${imageLatest} 2>/dev/null || true
-                                cd ${WORKSPACE}/${svc.module}
-                                rm -rf .jenkins_docker_ctx
-                            """
+                        if (params.HARBOR_PUSH_PARALLEL) {
+                            def pushBranches = [:]
+                            harborServices.each { svc ->
+                                def s = svc
+                                pushBranches[s.repo] = {
+                                    pushOneHarborImage(
+                                        this, s, reg, proj, latestTag, buildTag,
+                                        baseImage, dockerfile, env.WORKSPACE,
+                                    )
+                                }
+                            }
+                            parallel pushBranches
+                        } else {
+                            harborServices.each { svc ->
+                                pushOneHarborImage(
+                                    this, svc, reg, proj, latestTag, buildTag,
+                                    baseImage, dockerfile, env.WORKSPACE,
+                                )
+                            }
                         }
                         if (params.HARBOR_PRUNE_OLD_TAGS == true) {
                             def keepN = (params.HARBOR_KEEP_TAG_COUNT ?: '10').trim() as int
@@ -353,35 +419,16 @@ pipeline {
                             'alien-store-platform:store-platform-uat',
                     ]
 
-                    for (item in services) {
+                    def deployBranches = [:]
+                    services.each { item ->
                         def parts = item.split(':')
                         def moduleName = parts[0]
                         def dirName = parts[1]
-                        def sourceJar = "${env.WORKSPACE}/${moduleName}/target/${moduleName}-1.0.0.jar"
-                        def sourceLib = "${env.WORKSPACE}/${moduleName}/target/lib"
-                        def targetDir = "/app_deploy_uat/${dirName}"
-
-                        sh """
-                            set -e
-                            echo ">>> Deploy module: ${moduleName}"
-                            if [ -f "${sourceJar}" ]; then
-                                mkdir -p "${targetDir}"
-                                if [ -d "${sourceLib}" ]; then
-                                    rm -rf "${targetDir}/lib"
-                                    cp -rf "${sourceLib}" "${targetDir}"
-                                fi
-                                cp -f "${sourceJar}" "${targetDir}/"
-                                if docker ps -a --format '{{.Names}}' | grep -wq "${dirName}"; then
-                                    docker restart "${dirName}"
-                                    echo ">>> [${dirName}] restarted"
-                                else
-                                    echo ">>> [${dirName}] container missing, jar copied only"
-                                fi
-                            else
-                                echo ">>> [${dirName}] jar missing, skip"
-                            fi
-                        """
+                        deployBranches[dirName] = {
+                            deployOneUatService(this, moduleName, dirName, env.WORKSPACE)
+                        }
                     }
+                    parallel deployBranches
                 }
             }
         }