簡體   English   中英

如何從一個私有注冊表中提取 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 連接到兩個私有注冊表,我可以拉出我想要的圖像,但是我不知道如何將相同的圖像推送到不同的存儲庫。

請注意,我使用的是腳本化管道語法,因為據我所知,聲明性語法不支持推/拉或自定義注冊表。 我也不熟悉 Groovy 語法。

這是我的 Jenkinsfile 到目前為止的內容:

node {
    checkout scm

    docker.withRegistry('https://private-registry-1', 'credentials-1') {
        def image = docker.image('my-image:tag')
        image.pull()

        docker.withRegistry('https://private-registry-2', 'credentials-2') {
            image.push()
        }
    }
}

我將第二個“withRegistry()”方法放在第一個方法中,以便我可以使用定義的“image”變量。

我成功連接到第一個注冊表並拉取最新的鏡像。 從詹金斯控制台輸出:

Login Succeeded
[Pipeline] {
[Pipeline] sh
+ docker pull private-registry-1/my-image:tag
tag: Pulling from my-image
Digest: sha256:XXXXX
Status: Image is up to date for private-registry-1/my-image:tag

但是,這是連接到第二個注冊表后的相關錯誤片段:

...
Login Succeeded
[Pipeline] {
[Pipeline] sh
+ docker tag my-image:tag private-registry-2/my-image:tag
Error response from daemon: No such image: my-image:tag
...

我在本地 Windows 機器上使用 Jenkins 容器。 它通過我的 Ubuntu 終端(Linux 的 Windows 子系統)連接到 Docker for Windows。

解決方案是在推送之前標記圖像,最終代碼:

node {
    checkout scm

    stage 'Pull latest image from private-registry-1'

    def image
    docker.withRegistry('https://private-registry-1', 'credentials-1') {
        image = docker.image('my-image:tag')
        image.pull()
    }

    stage 'Push image to private-registry-2'

    // SOLUTION START
    sh 'docker tag private-registry-1/my-image:tag private-registry-2/my-image:tag'
    image = docker.image('private-registry-2/my-image:tag')
    // SOLUTION END

    docker.withRegistry('https://private-registry-2', 'credentials-2') {
        image.push()
    }
}

我不喜歡如何通過“sh”手動完成標記,但我找不到通過內置 Docker 語法來完成的方法。 我還需要參數化圖像名稱和標簽 (my-image:tag) 以備將來使用。

感謝 VictoryShoe 的回答!

有一件事很重要,我花了很長時間才找出錯誤:

  • "docker.withRegistry(' https: //private-registry-x', 'credentials-x')" -> 不要忘記在registryURL前面加上"https://"
  • 在命令行“噓‘泊塢窗標簽私人注冊表-1 /我的形象是:標簽私營注冊表-2 /我的形象是:標簽’”:在registryURL的面前,請不要添加任何“// https”開頭

以下 jenkinsfile 對我有用:

PS:在我的用例中,我從 DockerHub 拉取源映像,標記此映像,然后將此目標映像推送到私人公司映像注冊表。

def registryCredentials1 = "cridentialIdOfJenkinsForRegistry1"
def registryCredentials2 = "cridentialIdOfJenkinsForRegistry2"
def protocol = "https://"
def registryURL1 = "registry.hub.docker.com"
def registryURL2= "harbor.mycompany.xx.yy.com"

pipeline {
    agent any

    parameters {
         string(name: 'sourceImageName', defaultValue: '', description: 'Source-Image-Name, name-schema is like user/foo, e.g. jenkins/jenkins')
         string(name: 'sourceImageTag', defaultValue: '', description: 'Source-Image-Tag, e.g. lts')
         string(name: 'targetImageName', defaultValue: '', description: 'Target-Image-Name, name-schema is like user/foo, e.g. jenkins/jenkins')
         string(name: 'targetImageTag', defaultValue: '', description: 'Target-Image-Tag, e.g. lts')
    }

    stages {

        stage('Pull source-image from Registry 1 & tag the image') {
            steps {
                script {
                    //pull source-image from registry 1
                    docker.withRegistry(protocol + registryURL1, registryCredentials1) {
                        docker.image("${params.sourceImageName}:${params.sourceImageTag}").pull()
                    }

                    //tag the image
                    sh "docker tag ${registryURL1}/${params.sourceImageName}:${params.sourceImageTag} ${registryURL2}/${params.targetImageName}:${params.targetImageTag}"
                }
            }
        }

        stage('push target-image to Registry 2') {
            steps {
                script {
                    //push target-image to registry 2
                    docker.withRegistry(protocol + registryURL2, registryCredentials2) {
                        sh "docker push ${registryURL2}/${params.targetImageName}:${params.targetImageTag}"
                    }
                }
            }
        }
    }
}

對於聲明性語法,以下對我有用:

pipeline {
    agent {
        docker {
            label 'service'
            alwaysPull false
            registryUrl "${my-private-docker-registry_url_with_https}"
            registryCredentialsId "${jenkins_credential_id_for_login}"
            image 'lambci/lambda:build-python3.7'
            args '-v /var/run/docker.sock:/var/run/docker.sock --network host'
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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