jenkins微服务docker部署

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.text.SimpleTemplateEngine
import groovy.text.Template
import groovy.text.TemplateEngine

def gengerateReport(sonarCheckData,outfile,sonarurl){
    // turn sonarCheckData to one html 
    def template_text='''    <tr>
  <td class="tr-title" colspan="7" ><a href="${sonarurl}/dashboard?id=${project_key}">${project_key}</a></td>
 </tr>
 <tr>
  <td></td>
  <td>阻断</td>
  <td>严重</td>
  <td>主要</td>
  <td>次要</td>
  <td>提示</td>
  <td style="font-weight:bold;">总数</td>
 </tr>
    <tr>
  <td>漏洞</td>
  <td>${vulnerabilitys.BLOCKER}</td>
  <td>${vulnerabilitys.CRITICAL}</td>
  <td>${vulnerabilitys.MAJOR}</td>
  <td>${vulnerabilitys.MINOR}</td>
  <td>${vulnerabilitys.INFO}</td>
  <td style="font-weight:bold;">${vulnerabilitys.TOTAL}</td>
 </tr>
    <tr>
  <td>Bugs</td>
  <td>${bugs.BLOCKER}</td>
  <td>${bugs.CRITICAL}</td>
  <td>${bugs.MAJOR}</td>
  <td>${bugs.MINOR}</td>
  <td>${bugs.INFO}</td>
  <td style="font-weight:bold;">${bugs.TOTAL}</td>
 </tr>
    <tr>
  <td>异味</td>
  <td>${code_smells.BLOCKER}</td>
  <td>${code_smells.CRITICAL}</td>
  <td>${code_smells.MAJOR}</td>
  <td>${code_smells.MINOR}</td>
  <td>${code_smells.INFO}</td>
  <td style="font-weight:bold;">${code_smells.TOTAL}</td>
  </tr>
    '''
    def project_key=""
    //def sonarurl=env.sonarurl

    def html_content=""
    for (md in sonarCheckData.keySet()){
        project_key=md
        // get sonarCheckData to html
        TemplateEngine engine=new SimpleTemplateEngine()
        Template template =engine.createTemplate(template_text)
        def bugs=sonarCheckData[md]["BUG"]
        def code_smells=sonarCheckData[md]["CODE_SMELL"]
        def vulnerabilitys=sonarCheckData[md]["VULNERABILITY"]
        def result=template.make(bugs:bugs,code_smells:code_smells,vulnerabilitys:vulnerabilitys,project_key:project_key,sonarurl:sonarurl)
        html_content=html_content+result
    }
    writeFile  encoding: 'UTF-8',file: outfile,text: html_content
}
def getSonarResult(modules,sonarCheckData,sonarurl,auth){
    
    // def sonarurl="http://192.168.10.234:9000/"
    def apipath="/api/issues/search?s=FILE_LINE&resolved=false&types={type}&ps=1&facets=severities&componentKeys={pro}"
    def types=["CODE_SMELL", "BUG", "VULNERABILITY"]
    for ( ml in modules ){
        def sub_map=[:]
        for (t in types){
            def tstr =apipath.replaceAll("\\{type\\}",t).replaceAll("\\{pro\\}",ml)
            def response=httpRequest authentication: "${auth}", contentType: 'APPLICATION_JSON', customHeaders: [[maskValue: false, name: '', value: '']], responseHandle: 'STRING', timeout: 60, url: sonarurl+tstr
            def tstr_json=response.getContent()
            def jsonSlpuer = new JsonSlurper()
            def tstr_list = jsonSlpuer.parseText(tstr_json)

            t_total=tstr_list["total"]
            // println(t_total) //code_smell total
            def ssub_map=[:]
            for (i=0;i<5;i++){
                def t_val=tstr_list["facets"][0]["values"][i]["val"]
                def t_val_count=tstr_list["facets"][0]["values"][i]["count"]
                // println(t_val)  //级别
                // println(t_val_count)
                ssub_map[t_val]=t_val_count
            }
            ssub_map["TOTAL"]=t_total
            sub_map[t]=ssub_map
            // sonarData[ml][t]["TOTAL"]=t_total
        }
        sonarCheckData[ml]=sub_map
    }
    // println(sonarCheckData)
}
def sendMail(subject,recipientList,sonarfile){
    emailext(
        body: """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
\${SCRIPT, template="groovy-html.template"}
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
  <table class="section">
    <tr class="tr-title">
      <td class="td-title" colspan="7">CodeAnalysis(modified modules)</td>
  \${FILE,path="${sonarfile}"}
  </tr>
  </table>
</body>
</html>
""",
            replyTo: '$DEFAULT_REPLYTO',
   subject: subject,
   to: recipientList )
    
}

def getModuels(stage){
    def method=["staticCheck","build"]
    def moduels=""
    if (stage in method){
        datas=readYaml text : params.config
        datas.each{
            if(it.value[stage]==true){
                moduels=moduels+it.key+" "
            }
        }
    }else {
        println(stage+" is not correct param")
    }
    if (moduels.trim().isEmpty()){
        return "empty"
    }
    return moduels
}
def getDockerDeploy(buildModuels){
    //return all moduels and deploy config which need build and deploy to docker
    // println("func getDockerDeploy ,buildModuels are: "+buildModuels)
    datas=readYaml text : params.config
    def dockers=""
    // buildmoduels=getModuels("build")
    buildModuels=buildModuels.trim()
    if(buildModuels=="empty"){
        return "empty"
    }
    for ( moduel in buildModuels.split(" ") ){
        try{
            datas.get(moduel).get("delploy")
        } 
        catch(exc) {
            println("this moduel "+moduel+" not in param.config, no deploy")
            continue
        }
        for ( dm in datas[moduel]["deploy"]){
            if ( dm["machine"]=="docker" ){
                 li=moduel+","+dm["ip"]+","+dm["user"]+","+dm["startArgs"]?:" "
                dockers=dockers+li+";"
            }    
        }
    }
    if (dockers.trim().isEmpty()){
        return "empty"
    }
    return dockers
}
def getHostDeploy(buildModuels){
    // println("func getHostDeploy ,buildModuels are: "+buildModuels)
    datas=readYaml text : params.config
    def hostcfgs=""
    // buildmoduels=getModuels("build")
    buildModuels=buildModuels.trim()
    if(buildModuels=="empty"){
        return "empty"
    }
    for ( moduel in buildModuels.split(" ") ){
        try{
            datas.get(moduel).get("delploy")
        } 
        catch(exec) {
            println("this moduel "+moduel+" not in param.config, no deploy")
            continue
        }
        for ( dm in datas[moduel]["deploy"]){
            if ( dm["machine"]=="host" ){
                li=moduel+","+dm["ip"]+","+dm["dir"]+","+dm["user"]+","+dm["startArgs"]?:" "
                hostcfgs=hostcfgs+li+";"
            }    
        }
    }
    if (hostcfgs.trim().isEmpty()){
        return "empty"
    }
    
    return hostcfgs
}
def getGlobalValue(key){
    datas=readYaml text : params.config
    if ( datas.containsKey("global") ){
        if ( datas["global"].containsKey(key) ){
            return datas["global"][key]
        }
        else {
            error "global does not contain key "+key
            return "empty"
        }
    } 
    else{
        error "params does not contain global params"
        return "empty"
    }
}
def getDependModuel(moduel){
    def dependm=""
    if (moduel=="empty"){
        return ""
    }
    isExist=fileExists 'pom.xml'
    if ( !isExist ){
        error "ERROR: no root pom"
    }
    rootpom=readMavenPom file: 'pom.xml'
    rootmoduels=rootpom.modules
    if(rootmoduels==[]){
        m_moduel=rootpom.artifactId
        if (moduel == m_moduel){
            println("moduel "+moduel+" has no sub moduel")
            return ""
        }else {
            error "moduel "+moduel+" does not exist"
            return ""
        }
    }
    if ( rootmoduels.contains(moduel) ){
        // this indicates the moduel is root's sub moduel
        moduelpom=readMavenPom file: "${moduel}/pom.xml"
    }else{
        // the moduel is sub sub moduel or the moduel not exisit,get the sub sub dir and pom
        strs=moduel.split('-')
        moduelp=strs[0].plus("-").plus(strs[1])
        subExisit=fileExists "${moduelp}/${moduel}/pom.xml"
        if (!subExisit){
            error "moduel "+moduel+" does not exist"
        }
        moduelpom=readMavenPom file: "${moduelp}/${moduel}/pom.xml"
    }
    // get all depend moduels of the moduel,and check if the dependmoduels contain highlander
    dmall=moduelpom.dependencies
    if( ! dmall.groupId.contains("cn.com.highlander") ){
        // the moduel does not contain highlander dependency,return
        return ""
    }
    // if not return, says the moduel contain highlander dependencies,then find out all
    for (ds in dmall){
        if(ds.groupId=="cn.com.highlander"){
            dependm=ds.artifactId+" "+dependm
        }
    }
    return dependm
}
def getDependModuels(buildModuels){
    if (buildModuels=="empty"){
        return ""
    }
    if (buildModuels=="all"){
        return ""
    }
    def dependecies=""
    moduel=""
    for (moduel in buildModuels.trim().split(" ")){
        dependecies=getDependModuel(moduel)
        // println("moduel "+moduel+" depenecies are: "+dependecies)
        if (dependecies==""){
            continue
        }else {
            // get dependecies dependecy 
            dependecies=getDependModuels(dependecies)+" "+dependecies
        }
    }
    // println("moduel: "+moduel+"getDependModuels: "+ dependecies)
    return dependecies
}
def uniqueModuels(moduels){
    // println("befor unique: "+moduels)
    if (moduels=="all"){
        return ""
    }
    
    def String[] mlist=moduels.split(" ")
    for ( int i=0 ; i<mlist.size()-1 ;i++){
          for ( int j=mlist.size()-1 ; j>i; j-- )  {
               if  ( mlist[j].equals(mlist[i]) )  {
                  mlist[j]="";
                } 
            } 
    } 
    def strms=mlist.join(" ")
    return strms
}
def getVar(var){
    return var
}
def buildModuels=""
pipeline{
    agent{
        node {
            label 'master'
        }
    }
    triggers {
        pollSCM 'H 7 * * *'
        gitlab(triggerOnPush: true,
            triggerOnAcceptedMergeRequest: true,
            triggerOnApprovedMergeRequest: true,
            branchFilterType: "RegexBasedFilter",
            sourceBranchRegex: ".*",
            targetBranchRegex: ".*develop",
            secretToken: "xxx")
    }
    options {
        timeout(60)
        timestamps()
        // buildDiscarder logRotator(daysToKeepStr: '10', numToKeepStr: '10')
        disableConcurrentBuilds()
    }
    parameters {
        gitParameter branch: '' , branchFilter: '.*', defaultValue: 'origin/develop', description: 'git branch or tag to build', name: 'branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH_TAG'
        text  name: 'config', defaultValue: '''global:
    project: xx
    gitUrl: xxxxx
    staticCheck: false
    singleModuel: false
    archiv: true
    archivUser: dev
    archivIP: xxxx
    archivDir: xxxxx
    mailTo: xx,xx-xx
xx-common-xx:
    build: true
    deploy:
xx-common-network:
    build: true
    deploy:
xx-admin-xx:
    build: true
    deploy:
xx-auth: 
    build: true
    deploy: 
    - startArgs: CE=dev
      machine: docker
      ip: xxxx
      user: xxxx
hscp-user-api:
    build: false
    deploy: 
hscp-user-server:
    build: false
    deploy: 
hscp-gateway:
    build: true
    deploy: 
    - startArgs: CE=dev
      machine: xxxx
      ip: xxxx
      user: root
hscp-cctv-api:
    build: true
    deploy: 
hscp-cctv-server:
    build: true
    deploy: 
    - startArgs: CE=dev
      machine: docker
      ip: 1xxxx
      user: root
hscp-monitor:
    build: false
    deploy:
bigdata-radar:
    build: false
    deploy: 
hscp-echo:
    build: false
    deploy: 
    - startArgs: CE=dev
      machine: docker
      ip: xxxx
      user: root
hscp-target-api:
    build: true
    deploy: 
hscp-target-server:
    build: true
    deploy:
    - startArgs: CE=dev
      machine: docker
      ip: xxxx
      user: root
hscp-graph-cutting-api:
    build: false
    deploy:
hscp-graph-cutting-server:
    build: true
    deploy:
    - startArgs: CE=dev
      machine: docker
      ip: xxxx
      user: root
hscp-xxyy-server:
    build: false
    deploy: 
hscp-fjport-server:
    build: false
    deploy: 
hscp-admin-service:
    build: true
    deploy:
    - startArgs: CE=dev
      machine: docker
      ip: xxxx
      user: root
      ''', 
     description: 'configuration,do not change the format'
    }
    environment {
        project=getGlobalValue("project")
        gitUrl=getGlobalValue("gitUrl")
        staticCheck=getGlobalValue("staticCheck")
        singleModuel=getGlobalValue("singleModuel")
        ts=sh(returnStdout: true, script: 'date +%Y%m%d%H%M').trim()
        archiv=getGlobalValue("archiv")
        registryIP="xxxx"
        registryPort="8899"
        registryUser="xx"
        registryPwd="xx"
        registry="xx"
        maintermail=",xx"
        mailto=getGlobalValue("mailTo")
        sonarurl="xxx"
        // buildModuels=getModuels("build")
    }
    tools {
        maven 'maven3.6.3'
    }
    stages{
        stage('checkout'){
            steps{
                script{
                    def buildCause=currentBuild.getBuildCauses()[0].shortDescription
                    if ( gitUrl=="empty" ){
                        println("[stage: checkout]>>>global param 'gitUrl' is missing, fail!! ")
                        error '[stage: checkout]>>> gitUrl param missing!!!'
                    } else{
                        if (buildCause.contains("Started by GitLab ") ){
                            tbranch="origin/"+env.gitlabTargetBranch
                            checkout([$class: 'GitSCM', branches: [[name: tbranch]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']],  userRemoteConfigs: [[credentialsId: '97f141ab-1b12-4948-b4b3-dff03cc5e68a', url: env.gitUrl]]])
                            withCredentials([usernamePassword(credentialsId: 'xxxxxxxxxxxxxxxxxxxx', passwordVariable: 'pwd', usernameVariable: 'user')]) {
                                sh(returnStdout: true,script: "git pull origin ${gitlabTargetBranch}")
                                buildModuels=sh(returnStdout: true,script: "git diff ${gitlabBefore} ${gitlabAfter} --name-only | awk -F'/' '{print \$1}' | sort -u |xargs ").trim()
                            }
                            println("[stage:checkout]>>> git change: "+buildModuels)
                            for ( bm in buildModuels.split(" ")){
                                if (bm.trim()=="nginx"){
                                    buildModuels=buildModuels.minus(bm)
                                    continue
                                }
                                def isRootFile=sh(returnStdout: true, script: "set +x;if [ -f $bm ];then echo true;else echo false;fi").trim()
                                if(isRootFile =="true"){
                                    if(buildModuels.trim()==bm.trim()){
                                        buildModuels="empty"
                                        println("gitLab change root file,no moduel file or module deleted ,do not build!!")
                                        return 
                                    }else {
                                        buildModuels=buildModuels.minus(bm)
                                        continue
                                    }
                                }
                                def isExist= fileExists "${bm}/pom.xml"
                                if (!isExist){
                                    println("module ${bm} is deleted or its pom.xml deleted ,no need build")
                                    buildModuels=buildModuels.minus(bm.trim())
                                    continue
                                }                                
                                // 
                                def subModule=sh(returnStdout: true, script: "cat ${bm}/pom.xml|grep '<module>' |awk -F'>' {'print \$2'} |awk -F'<' {'print \$1'} |xargs" ).trim()
                                if ( !subModule.trim().isEmpty() ){
                                    buildModuels=buildModuels.replaceAll(bm,subModule)
                                }
                            }
                        } else{
                            checkout([$class: 'GitSCM', branches: [[name: "${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CleanBeforeCheckout']],  userRemoteConfigs: [[credentialsId: '7xxxxxxa', url: env.gitUrl]]])
                            be = branch.split("/").join(" ")
                            print(be)
                            sh(returnStdout: true,script: "git pull  ${be}")
                            buildModuels=getModuels("build")
                        }
                    }
                    println("checkout:"+buildModuels)
                }
            }
        }
        stage('build') {
            environment{
                buildModuels="${buildModuels}"
            }
            steps {
                script{
                    if (buildModuels.trim()=="empty"){
                        println("[stage: build]>>>no need build moduels")
                        return
                    }
                    sh returnStdout: false, label: 'buidShell', script: '''
                    set +x
                    #echo ${dependModuels}
                    #check if project has multiple modules
                    if [ ${buildModuels} == "all" ]
                    then
                       mvn clean install -f ./pom.xml
                       exit 0
                    fi
                    if [ ${singleModuel} != "true" ];
                    then
                        echo "this is a multiModuels git project"
                        if [ -f pom.xml ];
                        then
                            mvn clean install -N
                        else
                            echo ">>>!!! ERROR:no parent pom"
                            exit 100;
                        fi
                        for moduel in  ${buildModuels}
                        do
                            cd ${WORKSPACE}
                            # check if this moduel is sub moduel.submoduel will cd parent dir,build from parent pom
                            if [ -d ${moduel} ];
                            then
                                mvn clean install -N
                                mvn -T 2 clean install -pl ./${moduel} -am
                            else
                                moduelDir=`find ./ -path "*${moduel}*" -name pom.xml `
                                if [ -n "${moduelDir}" ];then 
                                    cd  `dirname ${moduelDir}`
                                    cd ../
                                    mvn clean install -N
                                    mvn -T 2 clean install -pl ./${moduel} -am
                                else
                                  echo "this moduel ${moduel} not exist"
                                  exit 100
                                fi
                            fi
                        done
                        echo "[stage: build]>>>build moduels are: ${buildModuels}"
                    else
                    # this case indicates the project just one module
                        echo "singleModuel git project build"
                        if [ -f pom.xml ];then
                          mvn -f pom.xml clean package -Dmaven.test.skip=true
                        else
                          echo "no pom.xml"
                          exit 102
                        fi
                    fi
                    '''
                }
            }
        }
        stage('staticCheck'){
   environment{
       buildModuels=getVar(buildModuels)
   }
   when {
    expression {
     return (env.staticCheck == "true")
    }
   }
   steps{
    script{
        if(buildModuels=="empty"){
            println("[stage: staticCheck]>>>no build moduels, no need staticCheck ")
            return
        }
        
        withSonarQubeEnv {    
            sh returnStdout: true, script: '''
            echo 'check'
            #buildModuels=`git diff ${gitlabBefore} ${gitlabAfter} --name-only | awk -F'/' '{print \$1}' | sort -u`
            for moduel in ${buildModuels};
            do
              echo ${moduel}
              cd ${WORKSPACE}
              if [ ${singleModuel} != "true" ];then
                cd `find ./ -name ${moduel} -type d`;
              fi
              mPath=$(pwd)
              if [ -d ${mPath}/src ]
              then
                /var/sonar-scanner-4.0.0.1744-linux/bin/sonar-scanner -Dsonar.projectKey=$moduel_BS_${branch} -Dsonar.projectName=$moduel_BS_${branch} -Dsonar.projectVersion=$BUILD_ID -Dsonar.sources=$mPath/src -Dsonar.java.binaries=$mPath/target -Dsonar.projectBaseDir=$WORKSPACE -Dsonar.language=java -Dproject.settings=/var/sonar-scanner-4.0.0.1744-linux/conf/sonar.properties
              else
                echo "this moduel ${moduel} has no src, no staticCheck"
              fi
            done
            '''
        }
     def modellist = []
     for (m in env.buildModuels.split(" ")){
      modellist.add(m)
     }
     echo '中文乱码'
     def sonarCheckData=[:]
                        getSonarResult(modellist,sonarCheckData,env.sonarurl,"sonar")
                        gengerateReport(sonarCheckData,"${WORKSPACE}/sonardata.html",env.sonarurl)
                        sonarfile="./sonardata.html"
    }
   }
  }
        stage('imageBuild'){
            environment{
       buildModuels=getVar(buildModuels).trim()
       dockercfg=getDockerDeploy(buildModuels).trim()
   } 
            steps {
                script{
                    if (buildModuels=="empty"){
                        println("[stage: imageBuild]>>>no build moduels, skip imageBuild")
                        return
                    }
                    if (dockercfg=="empty"){
                        println("[stage: imageBuild]>>>no need docker depoys, skip imageBuild")
                        return
                    }
                    sh returnStdout: false, label: 'buildImageShell', script: '''
                    set +x
                    rm -f dcfgs
                    builtms=" "
                    docker image prune -f --filter "until=48h"
                    echo $dockercfg|awk -F';' '{ for (i=1;i<=NF;i++) print $i }'>>dcfgs
                    cat dcfgs|while read line
                      do 
                        if [ -z $line ];then break;fi
                        cd ${WORKSPACE}
                        moduel=`echo $line |awk -F',' '{ print $1 } '`
                        if [[ ${builtms/${moduel}//} != ${builtms} ]]
                        then 
                          continue
                        fi
                        ip=`echo $line |awk -F',' '{ print $2 } '`
                        startArgs=`echo $line |awk -F',' '{ print $4 } '`
                        gitTag=`git describe --tags --always`
                        IMAGE_NAME=${registryIP}:${registryPort}/${registry}/${moduel}_${branch}:latest
                        IMAGE_NAME_BAK=${registryIP}:${registryPort}/${registry}/${moduel}_${branch}:${gitTag}
                        if [ ${singleModuel} != "true" ];
                        then
                            if [ -d ${moduel}/target ];then
                              mtdir="${moduel}/target"
                              cd $mtdir
                            else 
                              mtdir=`find ./ -path "*${moduel}/target" -type d`
                              cd $mtdir
                            fi
                        else
                            mtdir="./target"
                            cd $mtdir
                        fi
                        if [  -d ./docker ]; then rm -rf docker;fi
                        mkdir docker
                        artifactId=`ls -lSh ${moduel}*.jar |awk '{ print $NF }'|sed -n '1p'`
                        cp ${artifactId}  ./docker
                        if [ -f  ../Dockerfile ]
                        then
                            cp ../Dockerfile ./docker
                        else
                            # this moduel has no dockerfile,create one
                            cat > Dockerfile << EOF
FROM java:8
VOLUME /tmp
WORKDIR /${moduel}
ENV startArgs=" -Xms1024m -Xmx4096m "
COPY ${artifactId} /${moduel}/${artifactId}
ENTRYPOINT java \\${startArgs} -Djava.security.egd=file:/dev/./urandom -jar /${moduel}/${artifactId}
EOF
                            cp Dockerfile ./docker
                        fi
                        cd docker 
                        docker build -t $IMAGE_NAME .
                        docker build -t $IMAGE_NAME_BAK .
                        # debug comment it 
                        if [ -n ${registryUser} ];then
                          docker login -u ${registryUser} -p ${registryPwd} http://${registryIP}:${registryPort}
                        fi
                        docker push $IMAGE_NAME
                        docker push $IMAGE_NAME_BAK
                        builtms="${builtms},${moduel}"
                        #docker rmi $IMAGE_NAME
                        #docker rmi $IMAGE_NAME_BAK
                    done
                    '''
                }
            }
        }
        stage('ArchivJar'){
            environment{
                buildModuels=getVar(buildModuels)
                archivIP=getGlobalValue("archivIP")
                archivUser=getGlobalValue("archivUser")
                archivDir=getGlobalValue("archivDir")
                gitTag=sh(returnStdout: true, script: 'git describe --tags --always').trim()
            }
            when{
                expression{
                    return (env.archiv=="false")
                }
            }            
            steps{
                script{
                    if(buildModuels=="empty"){
                        println("[stage:ArchivJar]>>>no need build moduels,skip ArchivJar")
                        return
                    }
                    sh returnStdout: true, label: 'archivShell', script: '''
                    set +x
                    ad=${project}_${gitTag}-${ts}
                    archivJars=""
                    if [ -d ${ad} ];
                    then 
                      cd  ${ad};rm -f *.jar
                    else
                      mkdir ${ad}
                    fi
                    for moduel in ${buildModuels};
                    do
                        cd ${WORKSPACE}
                        if [ ${singleModuel} != "true" ];
                        then
                            targetdir=`find ./ -path "*${moduel}/target" -type d`
                        else
                            targetdir="./target"
                        fi
                        cd $targetdir
                        artifactId=`ls -lSh ${moduel}*.jar |awk '{ print $NF }'|sed -n '1p'`;
         if [ -n "${artifactId}" ];
                        then                    
          cp ${artifactId} ${WORKSPACE}/${ad}
             archivJars="${archivJars} ${artifactId}"
                        else
                            echo ">>>!!! no build jar of ${moduel}"
                            exit 1
                        fi
                    done
                    cd ${WORKSPACE}
                    tar -zcvf ${ad}.tar.gz ./${ad}
                    echo "[stage:archivJar]>>> archiv jars are: "
                    echo ${archivJars}
                    '''
                    sshPublisher(publishers: [sshPublisherDesc(configName: "${archivUser}@${archivIP}", transfers: [sshTransfer(cleanRemote: false,  execCommand: "", execTimeout: 200000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "'${archivDir}/'yyyyMMddHHmmss", remoteDirectorySDF: true, removePrefix: '', sourceFiles: "${project}_${gitTag}-${ts}.tar.gz")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                    sh '''
                    rm -f ${project}_${gitTag}-${ts}.tar.gz
                    '''
                }
            }
        }
        stage('deploy'){
            environment{
       buildModuels=getVar(buildModuels)
       dockercfg=getDockerDeploy(buildModuels)
       hostcfg=getHostDeploy(buildModuels)
                gitTag=sh(returnStdout: true,script: "git describe --tags --always").trim()
            }
            parallel {
                stage('deployDocker'){
                    // when{
                    //     expression{
                    //         return (env.dockercfg!="empty")
                    //     }
                    // }                    
                    steps{
                        script{
                            if(dockercfg=="empty"){
                                println("[stage:deploy->deployDocker]>>>no need docker deploys moduels,skip deployDocker")
                                return
                            }
                            // deployDocker shell script
                            sh  returnStdout: true, label: 'mkdeployDockerscript', script: '''
                            #deploy to docker
                            set +x
                            rm -f dcfgs
                            echo $dockercfg|awk -F';' '{ for (i=1;i<=NF;i++) print $i }'>>dcfgs
                            cat dcfgs|while read line
                              do
                                if [ -z $line ];then break;fi
                                moduel=`echo $line |awk -F',' '{ print $1 } '`
                                ip=`echo $line |awk -F',' '{ print $2 } '`
                                startArgs=`echo $line |awk -F',' '{ print $4 } '`
                                user=`echo $line |awk -F',' '{ print $3 } '`

                                IMAGE_NAME=${registryIP}:${registryPort}/${registry}/${moduel}_${branch}:latest
                                echo "status=\\`docker inspect --format '{{.Name}} {{.State.Running}}' ${moduel}\\`">dstart${moduel}.sh
                                echo "if [[ \"\\${status}\" =~ \"true\" ]];then docker stop ${moduel};docker rm \\` docker ps -a |grep ${moduel}before* |awk '{ print \\$1}'\\`; docker rename ${moduel} ${moduel}before-${gitTag}-\\`date +%Y%m%d%H%M\\`;fi ">>dstart${moduel}.sh
                                echo "if [[ \"\\${status}\" =~ \"false\" ]];then docker rm \\` docker ps -a |grep ${moduel}before* |awk '{ print \\$1}'\\`; docker rename ${moduel} ${moduel}before-${gitTag}-\\`date +%Y%m%d%H%M\\`;fi ">>dstart${moduel}.sh
                                echo "docker login -u ${registryUser} -p ${registryPwd} http://${registryIP}:${registryPort}">>dstart${moduel}.sh
                                echo "docker pull ${IMAGE_NAME}">>dstart${moduel}.sh
                                echo "usingImage=\\`docker ps -a|grep ${moduel}before* | awk '{print \\$2}' \\`">>dstart${moduel}.sh
                                echo "echo \\$usingImage ">>dstart${moduel}.sh
                                echo "usingImageId=\\` docker images -q --filter reference=\"\\${usingImage}\"\\`">>dstart${moduel}.sh
                                echo "echo \\$usingImageId ">>dstart${moduel}.sh
                                echo "images=\\`docker images |grep ${registryIP}:${registryPort}/${registry}/${moduel}|grep -v \\"\\${usingImageId}\\" | awk '{ print \\$3 }' \\`">>dstart${moduel}.sh
                                echo "if [ -n \\"\\${images}\\" ];then docker rmi \\${images} ;fi">>dstart${moduel}.sh
                                if [ ${startArgs} == "null" ]
                                then
                                    echo " docker run  --init --cap-add=SYS_PTRACE -v /tmp/logs:/logs  --name ${moduel} --network=host -e CE=apo --hostname=\\$(hostname) -d ${IMAGE_NAME}">>dstart${moduel}.sh
                                else
                                   if [ ${moduel} == "bigdata-radar" ]
                                   then
                                    echo " docker run  --init --cap-add=SYS_PTRACE  --name ${moduel} --network=host -e startArgs=\\"${startArgs}\\" -v /tmp/logs:/logs --hostname=\\$(hostname) -d ${IMAGE_NAME}">>dstart${moduel}.sh
                                   else
                                     echo " docker run  --init --cap-add=SYS_PTRACE  --name ${moduel} --network=host -e \\"${startArgs}\\" -v /tmp/logs:/logs --hostname=\\$(hostname) -d ${IMAGE_NAME}">>dstart${moduel}.sh
                                   fi
                                fi
                                #echo "docker ps | grep ${IMAGE_NAME}" > dcheck${moduel}.sh
                                #echo "if [ \\$? -eq 0 ];then echo '>>>start success,docker config: ${line}';else echo '>>>!!!start failed ,docker config: ${line}';fi">>dcheck${moduel}.sh
                              done
                            '''
                            for ( dcfg in dockercfg.trim().split(";") ){
                                if (dcfg.trim()==""){
                                    continue
                                }
                                def moduel=dcfg.split(",")[0]
                                def ip=dcfg.split(",")[1]
                                def user=dcfg.split(",")[2]
                                sshPublisher(publishers: [sshPublisherDesc(configName: "${user}@${ip}", transfers: [sshTransfer(cleanRemote: false,  execCommand: "cd /tmp/deploy/$BUILD_NUMBER;bash dstart${moduel}.sh;", execTimeout: 200000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "/tmp/deploy/$BUILD_NUMBER", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "dstart${moduel}.sh,dcheck${moduel}.sh")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                            }
                        }  
                    }
                }
                stage('deployHost'){
                    // when{
                    //     expression{
                    //         return (env.hostcfg!="empty")
                    //     }
                    // }                    
                    steps{
                        script{
                            if(hostcfg=="empty"){
                                println("[stage:deploy->delpoyHost]>>>no need host deploys moduels,skip deployHost")
                                return
                            }
                            if (archiv=="false"){
                                sh label: 'gatherJar',script: '''
                                set +x
                                ad=${project}_${gitTag}-${ts}
                                archivJars=""
                                if [ -d ${ad} ];
                                then 
                                  cd  ${ad};rm -f *.jar
                                else
                                  mkdir ${ad}
                                fi
                                for moduel in ${buildModuels};
                                do
                                    cd ${WORKSPACE}
                                    if [ ${singleModuel} != "true" ];
                                    then
                                        targetdir=`find ./ -path "*${moduel}/target" -type d`
                                    else
                                        targetdir="./target"
                                    fi
                                    cd $targetdir
                                    artifactId=`ls -lSh ${moduel}*.jar |awk '{ print $NF }'|sed -n '1p'`;
                     if [ -n "${artifactId}" ];
                                    then                    
                      cp ${artifactId} ${WORKSPACE}/${ad}
                         archivJars="${archivJars} ${artifactId}"
                                    else
                                        echo ">>>!!! no build jar of ${moduel}"
                                        exit 1
                                    fi
                                done
                                '''
                            }
                            // stop service,and move last jar to bakup
                            sh  returnStdout: true, label: 'mkdeployHostscript',script: '''
                            set +x
                            rm -f hcfgs
                            echo $hostcfg|awk -F';' '{ for (i=1;i<=NF;i++) print $i }'>>hcfgs
                            cat hcfgs|while read line
                            do
                                if [ -z $line ];then break ;fi
                                moduel=`echo $line |awk -F',' '{ print $1 } '`
                                ip=`echo $line |awk -F',' '{ print $2 } '`
                                ddir=`echo $line |awk -F',' '{ print $3 } '`
                                startArgs=`echo $line |awk -F',' '{ print $5 } '`
                                cd ${project}_${gitTag}-${ts}
                                artifactID=`ls ${moduel}*.jar`
                                cd ${WORKSPACE}
                                echo "#!/bin/bash" >bak${moduel}.sh
                                echo "res=\\`ps -ef|grep ${moduel}|grep java|grep -v grep\\`">>bak${moduel}.sh
                                echo \'if [ -n "$res"  ] \'>>bak${moduel}.sh
                                echo "then  ">>bak${moduel}.sh
                                echo "  echo '>>>this service running,kill it'">>bak${moduel}.sh
                                #echo "  ps -ef|grep ${moduel}|grep -v grep|awk '{ print $2 }' ${artifactID}">>bak${moduel}.sh
                                echo "  ps -ef|grep ${moduel}|grep -v grep|grep -v \\$0 |awk '{ print \\$2 }' |xargs kill -9">>bak${moduel}.sh
                                echo "fi">>bak${moduel}.sh
                                echo "if [ ! -d ${ddir}/${moduel} ];then mkdir -p ${ddir}/${moduel};fi;">>bak${moduel}.sh
                                echo "cd ${ddir}/${moduel}">>bak${moduel}.sh
                                echo "originjar=\\`ls ${moduel}*.jar\\`">>bak${moduel}.sh
                                echo "for ojar in \\${originjar} ;do mv \\${ojar} \\${ojar}.bak\\`date +%Y%m%d%H%M\\`;done">>bak${moduel}.sh
                                echo " cd ${ddir}/${moduel} ;">start${moduel}.sh
                                echo "nohup java ${startArgs} -jar ${artifactID} >/dev/null 2>starterror.log &">>start${moduel}.sh
                                
                                #echo "res=\\`ps -ef|grep ${moduel}|grep java|grep -v grep\\`">check${moduel}.sh
                                #echo "if [ -z \\"\\${res}\\" ];then echo '>>>!!!start failed,please check log,host config: ${line}';cd ${ddir}/${moduel};cat starterror.log;fi">>check${moduel}.sh
                                #echo "if [ -n \\"\\${res}\\" ];then echo '>>>start success,host config: ${line}';fi;">>check${moduel}.sh
                            done
                            '''
                            for ( hcfg in hostcfg.trim().split(";") ){
                                def moduel=hcfg.split(",")[0]
                                def ip=hcfg.split(",")[1]
                                def ddir=hcfg.split(",")[2]
                                def user=hcfg.split(",")[3]
                                sshPublisher(publishers: [sshPublisherDesc(configName: "${user}@${ip}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "cd /tmp/deploy/$BUILD_NUMBER; bash bak${moduel}.sh", execTimeout: 300000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "/tmp/deploy/$BUILD_NUMBER", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "start${moduel}.sh,bak${moduel}.sh,check${moduel}.sh")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                                sshPublisher(publishers: [sshPublisherDesc(configName: "${user}@${ip}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "cd /tmp/deploy/$BUILD_NUMBER; bash start${moduel}.sh", execTimeout: 300000, flatten: false, makeEmptyDirs: true, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "${ddir}/${moduel}", remoteDirectorySDF: false, removePrefix: "${project}_${gitTag}-${ts}", sourceFiles: "${project}_${gitTag}-${ts}/${moduel}*.jar")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                                // sshPublisher(publishers: [sshPublisherDesc(configName: "${user}@${ip}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "cd /tmp/deploy/$BUILD_NUMBER; bash start${moduel}.sh", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                            }
                        }
                    }
                }
            }
        }
        stage('checkService'){
            environment{
       buildModuels=getVar(buildModuels)
       dockercfg=getDockerDeploy(buildModuels)
       hostcfg=getHostDeploy(buildModuels)
       gitTag=sh(returnStdout: true,script: "git describe --tags --always").trim()
   }
            steps{
                script{
                    if(buildModuels=="empty"||(dockercfg=="empty"&&hostcfg=="empty")){
                        println("[stage:checkService]>>>no need deploy moduels,skip checkService")
                        return
                    }
                    sh 'sleep 10s'
                    def errorlist=[]
                    def suclist=[]
                    if (hostcfg != "empty"){
                        for ( hcfg in hostcfg.trim().split(";") ){
                            // println(hcfg)
                            def moduel=hcfg.split(",")[0]
                            def ip=hcfg.split(",")[1]
                            def ddir=hcfg.split(",")[2]
                            def user=hcfg.split(",")[3]
                            
                            def hresult=sh(returnStatus: true, script:"ssh ${user}@${ip} 'ps -ef|grep ${moduel}|grep java|grep -v grep'")
                            // println("result: "+result)
                            if (hresult!=0){
                                errorlist.push(hcfg)
                            }else {
                                suclist.push(hcfg)
                            }
                            // sshPublisher(publishers: [sshPublisherDesc(configName: "${user}@${ip}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "cd /tmp/deploy/$BUILD_NUMBER; bash check${moduel}.sh", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                        }
                    }
                    if (dockercfg!="empty"){
                        for ( dcfg in dockercfg.trim().split(";") ){
                            if (dcfg.trim()==""){
                                continue
                            }
                            def moduel=dcfg.split(",")[0]
                            def ip=dcfg.split(",")[1]
                            def user=dcfg.split(",")[2]
                            def result=sh(returnStatus: true, script:"ssh ${user}@${ip} 'docker ps | grep ${registryIP}:${registryPort}/${registry}/${moduel}_${branch}:latest'")
                            if (result!=0){
                                errorlist.push(dcfg)
                            } else{
                                suclist.push(dcfg)
                            }
                            // sshPublisher(publishers: [sshPublisherDesc(configName: "${user}@${ip}", transfers: [sshTransfer(cleanRemote: false,  execCommand: "cd /tmp/deploy/$BUILD_NUMBER;bash dcheck${moduel}.sh;", execTimeout: 200000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "/tmp/deploy/$BUILD_NUMBER", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
                        }
                    }
                    println("[stage:checkService]>>>start success services config : (model,deployIp,[deployDir,]deployUser,startArgs)\n"+suclist.join("\n"))
                    if (errorlist.size()>0){
                        println "[stage:checkService]>>>start failed services config : (model,deployIp,[deployDir,]deployUser,startArgs)\n"+errorlist.join("\n")
                        error "[stage:checkService]>>>start failed services config : (model,deployIp,[deployDir,]deployUser,startArgs)\n"+errorlist.join("\n")
                    }
                }
            }
        }
    }  
 post{
        success{
           sendMail('$DEFAULT_SUBJECT',env.mailto+env.maintermail,'./sonardata.html')
        }
        failure{
           sendMail('$DEFAULT_SUBJECT',env.mailto+env.maintermail,'./sonardata.html')
  }
 }
}

 

posted on 2021-08-26 14:40  慢慢前进的蜗牛  阅读(75)  评论(0编辑  收藏  举报

导航