簡體   English   中英

使用 Jenkins 管道 docker 插件,如何拉取圖像並將新標簽推送給它?

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

我的私人倉庫中已經有一張圖片。 我需要提取該圖像,創建一個標簽並將其推送到注冊表。

使用 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

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

已安裝 docker-engine 並且您的服務器應該可以訪問注冊表:

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

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

嘗試下面的代碼來推送帶有新標簽的圖像:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM