简体   繁体   English

使用 Jenkins 管道 docker 插件,如何拉取图像并将新标签推送给它?

[英]Using the Jenkins pipeline docker plugin, how to pull an image and push a new tag to it?

I have an image already in my private repo.我的私人仓库中已经有一张图片。 I need to pull that image, create a tag and push it to the registry.我需要提取该图像,创建一个标签并将其推送到注册表。

What's the bes way to do this using the Jenkins WithRegistry?使用 Jenkins WithRegistry 的最佳方法是什么?

Here's my actual code:这是我的实际代码:

        stage("Applying to docker repo") {
        steps {
            script {
              def imageNameLookup = configs.dockerRegistry.repo + "/"+repo.toLowerCase()+":"+params.versionToTag
              echo 'looking up '+ imageNameLookup
              docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {

                try {
                  image = docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                  image.tag("${deliveryTag}")
                  image.push()
                } catch (Exception e) {
                  echo ' catch 2 '+ e.getMessage()
                }
               }
            }
        }
    }

When running the image.tag(), i get the following error:运行 image.tag() 时,出现以下错误:

bfc9288fe86d: Pull complete Digest: sha256:ee9b01eb62f2f21dcb3bf4af285702c8991d1789e659515fdfa2da2619f1d8b9 Status: Downloaded newer image for repodocker-xxx.xxx.xx/my-api:1.19.0 [Pipeline] echo catch 2 Cannot invoke method tag() on null object bfc9288fe86d: Pull complete Digest: sha256:ee9b01eb62f2f21dcb3bf4af285702c8991d1789e659515fdfa2da2619f1d8b9 Status: Downloaded newer image for repodocker-xxx.xxx.xx/my-api:1.19.0 [Pipeline] echo catch 2 Cannot invoke method tag() on null object

EDIT: I was able to pull the image but when I try to create the tag, I get a new error: No such image:latest编辑:我能够提取图像,但是当我尝试创建标签时,我收到一个新错误:没有这样的图像:最新

I don't need to set the tag latest because I'm tagging another version.我不需要将标签设置为最新,因为我正在标记另一个版本。

 docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {
                try {
                  docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                  sh "docker tag ${repo.toLowerCase()} ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                  sh ""
                 )
                } catch (Exception e) {
                  echo ' catch 2 '+ e.getMessage()
                }

and my new log:和我的新日志:

[Pipeline] sh
+ docker pull repodocker-xxxx.xxx.xx/myapi-api:1.19.0
1.19.0: Pulling from myapi-api
Digest: sha256:ee9b01eb62f2f21dcb3bf4af285702c8991d1789e659515fdfa2da2619f1d8b9
Status: Image is up to date for repodocker-xxxx.xxx.xx/myapi-api:1.19.0
[Pipeline] sh
+ docker tag myapi-api grdocker-xxxx.xx.xx:443/xx.xxx.xxx/myapi-api:testTag20
Error response from daemon: No such image: myapi-api:latest
[Pipeline] echo
 catch 2 script returned exit code 1

EDIT2 was able to do it by doing it this way: EDIT2能够通过这种方式做到这一点:

        stage("Applying to docker repo") {
        steps {
            script {
                docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {
                  docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                  sh "docker tag ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${params.versionToTag} ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                  sh "docker push ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                }
            }
        }
    }

FINAL EDIT Here's the final solution in Jenkins and the docker plugin which was not able to do everything.最终编辑这是 Jenkins 和 docker 插件中的最终解决方案,它无法完成所有操作。

        stage("Applying to docker repo") {
        steps {
            script {
                  docker.withRegistry('https://' + configs.dockerRegistry.url, configs.dockerRegistry.credentialsId) {
                  docker.image(repo.toLowerCase()+":"+params.versionToTag).pull()
                  sh "docker tag ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${params.versionToTag} ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                  sh "docker push ${configs.dockerRegistry.url}/${repo.toLowerCase()}:${deliveryTag}"
                  docker.image(repo.toLowerCase()+":${deliveryTag}").pull()
                }
            }
        }
    }

Have installed docker-engine and your server should have access the registry:已安装 docker-engine 并且您的服务器应该可以访问注册表:

  • console docker login ip_registry:5000控制台docker login ip_registry:5000
  • stage:阶段:

stage('registry') {
            steps {
                sh "docker tag ${imageName} ${registryServer}/${imageName}:latest"
                sh "docker push ${registryServer}/${imageName}:latest"
            }
        }

Try code below to push image with new tag:尝试下面的代码来推送带有新标签的图像:

try {
    image = docker.image(imageNameLookup+":"+params.versionToTag)
    image.pull()
    image.push("${deliveryTag}")
} catch (Exception e) {
    echo ' catch 2 '+ e.getMessage()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用詹金斯管道参数标记Docker镜像? - How to tag docker image with jenkins pipeline parameter? Jenkins Pipeline推Docker图像 - Jenkins Pipeline push Docker image Jenkins Pipeline 推送 Docker 镜像 - Jenkins Pipeline push the Docker Image 如何通过 jenkins 管道在远程服务器上拉取和运行 docker 镜像 - How to pull & run docker image on remote server through jenkins pipeline 如何从一个私有注册表中提取 Docker 映像并将其推送到 Jenkins 管道中的第二个不同的私有注册表 - How do I pull a Docker image from one private registry and push it to a second different private registry in Jenkins pipeline 如何在Jenkins声明式管道中从docker hub推入和拉出 - How do I push and pull from docker hub in Jenkins declarative pipeline 管道中的jenkins docker插件使用-u标志,怎么可能不使用 - jenkins docker plugin in pipeline use -u flag, how is possible to not using Jenkins 声明式管道 - 将 Docker 映像推送到私有 docker hub 存储库 - Jenkins declarative pipeline - push Docker image to private docker hub repo Jenkins Golang 声明式管道:构建 Docker 镜像并推送到 Docker Hub - Jenkins Golang Declarative Pipeline: Build Docker Image and Push to Docker Hub Jenkins 总是从声明性管道代理定义中提取 docker 镜像 - Jenkins always pull docker image from a declarative pipeline agent definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM