2017-10-18 124 views
0

我正在使用連續交付管道構建微軟服務。 stage('package')使serviceImage,但我怎麼得到並使用它在stage('deploy')jenkins管道獲得價值

main.groovy

def call(body) { 
    def config = [ : ] 
    body.resolveStrategy = Closure.DELEGATE_FIRST 
    body.delegate = config 
    body() 


     stage('package') { 

      if (currentBuild.result == null || currentBuild.result == 'SUCCESS') { 
       echo 'Packaging...' 
       sh "docker pull ciregistry.i-counting.cn:8443/pilipa/openjdk:1.8" 
       sh "docker tag ciregistry.i-counting.cn:8443/pilipa/openjdk:1.8 pilipa/openjdk:1.8" 

       sh returnStdout: true, script: 'mkdir -p build/libs & cp -f src/main/docker/Dockerfile build/libs' 
       script { 
        serviceImage = dockerImageBuild { 
         imageName = "${config.serviceName}:${config.tagId}" 
         contextPath = "build/libs/" 
        } 
       } 
      } 
     } 

     stage('deploy') { 

      if (currentBuild.result == null || currentBuild.result == 'SUCCESS') { 
       echo 'Deploying...' 
       script { 
        dockerImageDeploy { 
         imageTag = "${config.tagId}" 
        } 
       } 
      } 
     } 
    } catch (err) { 
     currentBuild.result = 'FAILED' 
     throw err 
    } 

}

package.groovy

def call(body) { 
    def config = [ 
     registry: "https://ciregistry.i-counting.cn:8443", 
    ] 
    body.resolveStrategy = Closure.DELEGATE_FIRST 
    body.delegate = config 
    body() 

    docker.withRegistry(config.registry) { 
     serviceImage = docker.build(config.imageName, config.contextPath) 
    } 
} 

deploy.groovy

def call(body) { 
    def config = [ 
     registry: "https://ciregistry.i-counting.cn:8443", 
    ] 
    body.resolveStrategy = Closure.DELEGATE_FIRST 
    body.delegate = config 
    body() 

    docker.withRegistry(config.registry) { 
     serviceImage.push(config.imageTag) 
     serviceImage.push('latest') 
    } 
} 

回答

0

您需要聲明通話功能內serviceImage變量但出去階段代碼,如下所示:

def call(body) { 


    //Variable for your use 
    def serviceImage = "" 
    def config = [ : ] 
    body.resolveStrategy = Closure.DELEGATE_FIRST 
    body.delegate = config 
    body() 


    stage('package') { 

     if (currentBuild.result == null || currentBuild.result == 'SUCCESS') { 
      echo 'Packaging...' 
      sh "docker pull ciregistry.i-counting.cn:8443/pilipa/openjdk:1.8" 
      sh "docker tag ciregistry.i-counting.cn:8443/pilipa/openjdk:1.8 pilipa/openjdk:1.8" 

      sh returnStdout: true, script: 'mkdir -p build/libs & cp -f src/main/docker/Dockerfile build/libs' 
      script { 
       serviceImage = dockerImageBuild { 
        imageName = "${config.serviceName}:${config.tagId}" 
        contextPath = "build/libs/" 
       } 
      } 
     } 
    } 

    stage('deploy') { 

     //You can use your serviceImage variable 

     if (currentBuild.result == null || currentBuild.result == 'SUCCESS') { 
      echo 'Deploying...' 
      script { 
       dockerImageDeploy { 
        imageTag = "${config.tagId}" 
       } 
      } 
     } 
    } 
} catch (err) { 
    currentBuild.result = 'FAILED' 
    throw err 
}