我能够从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子系统)连接到Windows的Docker。
答案 0 :(得分:1)
解决方案是在推送图像之前对其进行标记,最终代码:
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),以备将来使用。
答案 1 :(得分:0)
感谢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}"
}
}
}
}
}
}
答案 2 :(得分:0)
对于声明性语法,以下对我有用:
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'
}
}