docker - 使用 Jenkins 管道 docker 插件,如何拉取图像并向其推送新标签?

标签 docker jenkins jenkins-pipeline

我的私有(private)仓库中已经有一张图片。我需要提取该图像,创建一个标签并将其推送到注册表。

使用 Jenkins WithRegistry 的最佳方法是什么?

这是我的实际代码:

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

运行 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



编辑:
我能够提取图像,但是当我尝试创建标签时,我收到一个新错误:没有这样的图像:最新

我不需要将标签设置为最新,因为我正在标记另一个版本。

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

和我的新日志:
[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

编辑2
能够通过这种方式做到这一点:
        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}"
                }
            }
        }
    }

最终编辑
这是 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()
                }
            }
        }
    }

最佳答案

已安装 docker-engine 并且您的服务器应该可以访问注册表:

  • 控制台docker login ip_registry:5000
  • 舞台:
  • 
    stage('registry') {
                steps {
                    sh "docker tag ${imageName} ${registryServer}/${imageName}:latest"
                    sh "docker push ${registryServer}/${imageName}:latest"
                }
            }
    

    关于docker - 使用 Jenkins 管道 docker 插件,如何拉取图像并向其推送新标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58628261/

    相关文章:

    docker - PHP的composer软件包管理器的容器显示消息/错误:docker-compose的 “docker_composer_1 exited with code 0”

    docker - Jenkins 使用 `.inside()`做什么来运行docker?

    ssl - 面对 Jenkins 访问问题 ERR_SSL_VERSION_OR_CIPHER_MISMATCH

    jenkins - 即使上一个作业在 Jenkins 构建流程中失败,也按顺序运行下一个作业

    java - 是否可以更改 Artifactory pro/Jenkins 中的许可证违规设置?

    docker - Jenkinsfile 中的 "withRun"和 "inside"有什么区别?

    jenkins - 如何构建如蓝海测试版项目页面所示的管道

    docker - 人工oss docker 5.0.0升级至5.1.0现在无法启动

    Jenkins - 如何暂停排队作业的运行并让新构建优先

    docker - 如何在 Docker 中将 VOLUME 更改为 COPY?