def git_url="https://gitlab.365jft.com/root/jft-node-platform.git"
def credentialsId_val="5a1987eb-5b29-4886-b6d5-fc8d7ba94bbe"   //git凭据
def harbor_url="172.30.156.70:8081"
def harbor_pro_name="jft"
def harbor_auth="29244d80-2457-4753-9912-04bd9233569c"


def project_version = "${project_version}".split(',') // 替换为要检查的属性名
def checkArray=new String[1]
def versionArray=new String[1]
def proStr = ''
def versionStr = ''
def tag= "latest"   //版本
def operate = "${operate}"
def operateType= "web"
def kube_space="jft-cloud"
def part = "${part}"
def gray = "${gray}"
def proArray=["jft-node-platform"]

if (operate == "release") {
  node {
    stage('pull code') {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']],
        extensions: [], userRemoteConfigs: [[credentialsId: "${credentialsId_val}",
        url: "${git_url}"]]])

        def n=0
        for(int i = 0;i < project_version.size(); i++){
          if (i % 2 == 0){
            checkArray[n] = project_version[i];
            versionArray[n] = project_version[i+1];
            n++
          }
        }

        for(int i = 0; i < checkArray.size(); i++){
          if (checkArray[i]=="true"){
            def pro = proArray[i]
            def ver = versionArray[i]

            proStr = proStr + pro
            proStr = proStr + ','
            versionStr = versionStr + ver
            versionStr = versionStr + ','
          }
        }
        proStr = proStr.substring(0 , proStr.size() -1 )
        versionStr = versionStr.substring(0 , versionStr.size() - 1)
       echo 'pull code over--------------------------------------------------------------------------------------------'
    }
    stage('build images') {
            for(int i = 0; i < checkArray.size(); i++){
              if (checkArray[i] == "true") {
                def pro = proArray[i]
                def version = versionArray[i]
                sh "docker build -t ${pro}:${tag} ./"
                sh "docker tag ${pro}:latest ${harbor_url}/${harbor_pro_name}/${pro}:${version}"
              }
            }
           echo 'build images over--------------------------------------------------------------------------------------'
        }

    stage('push images') {
      withCredentials([usernamePassword(credentialsId: "${harbor_auth}",
      passwordVariable: 'pwd', usernameVariable: 'account')]) {
         sh "docker login -u ${account} -p ${pwd} ${harbor_url}"
         for(int i = 0;i < checkArray.size(); i++){
           if (checkArray[i]=="true"){
             def pro = proArray[i]
             def version = versionArray[i]
             sh "docker push ${harbor_url}/${harbor_pro_name}/${pro}:${version}"
           }
         }
      }
        echo 'push images over------------------------------------------------------------------------------------------'
    }


    stage('clean images') {
       for(int i = 0;i < checkArray.size(); i++){
         if (checkArray[i] == "true"){
           def pro = proArray[i]
           def version = versionArray[i]
           sh "docker rmi ${pro}:${tag}"
           sh "docker rmi ${harbor_url}/${harbor_pro_name}/${pro}:${version}"
         }
       }
       echo 'clean images over------------------------------------------------------------------------------------------'
    }

    stage('remote deploy') {
    echo 'kube_space------------------------------------------------------------------------------------------'+"${kube_space}"
        sshPublisher(publishers: [sshPublisherDesc(configName: 'jft-m0',
        transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/deploy/deploy.sh $proStr $versionStr $operateType $kube_space $harbor_pro_name $gray $part",
        execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
        patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
        usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
        echo 'deploy over--------------------------------------------------------------------------------------------------------'
    }
  }
}

if (operate == "delete") {
     node{
        stage('clean harbor'){
             def n=0
             for(int i = 0;i < project_version.size(); i++){
               if (i % 2 == 0){
                 checkArray[n] = project_version[i];
                 versionArray[n] = project_version[i+1];
                 n++
               }
             }

             for(int i = 0; i < checkArray.size(); i++){
               if (checkArray[i]=="true"){
               def pro = proArray[i]
               def ver = versionArray[i]
               echo '-------------------------------------------------------------pro===='+pro
               def response = sh returnStdout: true, script: 'curl  -u "admin:hbrLyjft@xggq!" -X DELETE "http://172.30.156.70:8081/api/v2.0/projects/odd-job/repositories/"'+"${pro}"
               proStr = proStr + pro
               proStr = proStr + ','
               versionStr = versionStr + ver
               versionStr = versionStr + ','
               }
             }
             proStr = proStr.substring(0 , proStr.size() -1 )
             versionStr = versionStr.substring(0 , versionStr.size() - 1)
           echo 'clean kube--------------------------------------------------------------------------------------------------------'
        }
        stage('clean kube') {
          sshPublisher(publishers: [sshPublisherDesc(configName: 'jft-m0',
          transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/deploy/delete.sh $proStr $versionStr $operateType $kube_space $harbor_pro_name $gray $part",
          execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
          patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
          usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
          echo 'clean kube--------------------------------------------------------------------------------------------------------'
          sleep(40)
        }
        stage('clean images jft-n0') {
          sshPublisher(publishers: [sshPublisherDesc(configName: 'jft-n0',
          transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/deploy/images-delete.sh $proStr $versionStr $harbor_pro_name",
          execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
          patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
          usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
          echo 'clean images jft-n0--------------------------------------------------------------------------------------------------------'
        }
        stage('clean images jft-n1') {
          sshPublisher(publishers: [sshPublisherDesc(configName: 'jft-n1',
          transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/deploy/images-delete.sh $proStr $versionStr $harbor_pro_name",
          execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
          patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
          usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
          echo 'clean images jft-n1--------------------------------------------------------------------------------------------------------'
        }
        stage('clean images jft-n2') {
          sshPublisher(publishers: [sshPublisherDesc(configName: 'jft-n2',
          transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/deploy/images-delete.sh $proStr $versionStr $harbor_pro_name",
          execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false,
          patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')],
          usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
          echo 'clean images jft-n2--------------------------------------------------------------------------------------------------------'
        }
     }
}
