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

时间:2019-10-30 15:01:43

标签: docker jenkins jenkins-pipeline

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

使用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:拉完整   摘要:sha256:ee9b01eb62f2f21dcb3bf4af285702c8991d1789e659515fdfa2da2619f1d8b9   状态:为repodocker-xxx.xxx.xx / my-api:1.19.0下载的更新映像   [管道]回声    catch 2无法在空对象上调用方法tag()

编辑: 我能够拉出图像,但是当我尝试创建标签时,出现了一个新错误:没有这样的图像:最新

我不需要设置最新标签,因为我正在标记其他版本。

              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

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}"
                }
            }
        }
    }

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

2 个答案:

答案 0 :(得分:1)

已安装docker-engine,并且您的服务器应有权访问注册表:

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

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

答案 1 :(得分:0)

尝试下面的代码来推送带有新标签的图像:

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