詹金斯管道获得价值

时间:2017-10-18 12:09:56

标签: jenkins groovy jenkins-pipeline pipeline

我正在使用持续交付管道构建微软服务。 stage('package')生成serviceImage,但如何在stage('deploy')获取并使用它?

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')
    }
}

1 个答案:

答案 0 :(得分:0)

你需要在调用函数中声明serviceImage变量,但在stage代码之外,如下所示:

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
}