我能够连接到来自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”变量。
我成功地连接到第一个注册表并提取最新的图像。从Jenkins控制台输出:
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子系统)连接到。
发布于 2019-02-15 21:30:32
解决方案是在推送图像之前标记图像,最后代码:
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 ),以便将来使用。
发布于 2021-04-23 16:04:39
对于声明性语法,以下内容对我起了作用:
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'
}
}发布于 2020-07-09 14:51:04
感谢VictoryShoe的回答!
有一件事很重要,我花了很长时间才发现这个错误:
下面的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}"
}
}
}
}
}
}https://stackoverflow.com/questions/54658137
复制相似问题