Jenkins 从另一个阶段重复调用一个阶段

Jenkins 从另一个阶段重复调用一个阶段,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我目前正在写一份工作,将上游docker图像克隆到我的内部docker repo。 我的作业参数页面看起来像 因此,它会获取所有图像,然后生成实际的路径,以显示其外观 pipeline { agent none stages { stage('Create new image paths:tag'){ steps { script { params.IMAGES.sp

我目前正在写一份工作,将上游docker图像克隆到我的内部docker repo。 我的作业参数页面看起来像

因此,它会获取所有图像,然后生成实际的路径,以显示其外观

pipeline {
    agent none
    stages {
        stage('Create new image paths:tag'){
            steps {
                script {
                    params.IMAGES.split("\\r?\\n").each{ oldfullPath ->
                        def (path, tag) = oldfullPath.tokenize( ':' )
                        def app = path.tokenize('/')[-1]
                        println "App is ${app} and Tag is ${tag}"
                        switch(params.REPO) {
                            case "monitoring":
                                image = "eu.gcr.io/<PROJECT>/infra/monitoring"
                                break
                            case "apps":
                                image = "eu.gcr.io/<PROJECT>/infra/apps"
                                break
                        }
                        def newFullPath = "${image}/${app}:${tag}"
                        println "Full old image path is ${oldfullPath}"
                        println "Full new image path is ${newFullPath}"
                        // Pull and Push Image stage should be called here ! Using `${oldfullPath}` and `${newFullPath}`
                    }
                }
            }
        }

正如你所看到的,这是一个完全不同的阶段。因此,我不确定如何从上一个阶段重复调用
拉/推图像阶段。任何帮助都将不胜感激。

这里有很多事情需要处理

首先,如果您只想将Docker图像复制到您的注册表中,可以按如下方式执行:

me@vm:~$ docker pull alpine:latest
latest: Pulling from library/alpine
89d9c30c1d48: Pull complete
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Downloaded newer image for alpine:latest
me@vm:~$ docker tag alpine:latest my-registry.my-company.io/alpine:latest
me@vm:~$ docker push my-registry.my-company.io/alpine:latest
The push refers to a repository [my-registry.my-company.io/alpine]
77cae8ab23bf: Pushed
latest: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
其次,您的解析可以在循环中的容器内完成,而不是在单独的步骤中完成:

stage('Pull and Push Image') {
    agent {
        label "kaniko"
    }
    steps {
        container(name: 'kaniko', shell: '/busybox/sh') {
            withCredentials([
                string(credentialsId: 'infra', variable: 'PASS')
            ]) {
                withEnv(['PATH+EXTRA=/busybox:/kaniko']) {
                    // RUN THE LOOP HERE
                    script {
                    params.IMAGES.split("\\r?\\n").each{ oldfullPath ->
                        def (path, tag) = oldfullPath.tokenize( ':' )
                        def app = path.tokenize('/')[-1]
                        println "App is ${app} and Tag is ${tag}"
                        switch(params.REPO) {
                            case "monitoring":
                                image = "eu.gcr.io/<PROJECT>/infra/monitoring"
                                break
                            case "apps":
                                image = "eu.gcr.io/<PROJECT>/infra/apps"
                                break
                        }
                        def newFullPath = "${image}/${app}:${tag}"
                        println "Full old image path is ${oldfullPath}"
                        println "Full new image path is ${newFullPath}"

                    sh """#!/busybox/sh
                    echo "FROM ${oldfullPath}" | \
                    /kaniko/executor \
                        --dockerfile /dev/stdin \
                        --destination ${newFullPath}
                    """
              // adjust closing brackets as needed 
然后在你的主管道中

        stage('Build all images') {
            steps {
                script {
                    def builders = getBuilders()
                    parallel builders
                }
            }
images_array = ["quay.io/one", "docker.io/two", "docker.io/three"] // or split params as needed

def getBuilders()
{
    def builders = [:]

    images_array.eachWithIndex { it, index ->
        // name the stage
        def name = 'Build image #' + (index + 1)
        builders[name] = {
            stage (name) {
                def my_label = "kaniko" // can choose programmatically if needed
                node(my_label) {
                    timeout(time: 2, unit: 'HOURS') { 
                        try {
                            buildOneImage(it)
                        }
                        catch (err) { println "Failed to build ${it}"; throw err  }
                        finally {  }
                    }
                }
            }
        }
    };
    return builders
}

def buildOneImage(old_path) {
   // replace path as needed, build image
   // use scripted-style pipeline, not the declarative one
}
        stage('Build all images') {
            steps {
                script {
                    def builders = getBuilders()
                    parallel builders
                }
            }