1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-10-30 23:57:50 +02:00

fix(containerPushToRegistry): Add possibility to copy image from private registry (#4247)

* fix(containerPushToRegistry): Add possibility to copy image from private registry

* Clean up
This commit is contained in:
Vyacheslav Starostin
2023-02-24 15:08:05 +06:00
committed by GitHub
parent dcc688f9be
commit 7e7182d3fe
2 changed files with 20 additions and 10 deletions

View File

@@ -37,11 +37,19 @@ class DockerUtils implements Serializable {
def targetImageFullName = targetDockerRegistry + target.image
if (!withDockerDaemon()) {
script.withCredentials([
script.usernamePassword(credentialsId: source.credentialsId, passwordVariable: 'src_password', usernameVariable: 'src_userid'),
script.usernamePassword(credentialsId: target.credentialsId, passwordVariable: 'password', usernameVariable: 'userid')
]) {
skopeoMoveImage(sourceImageFullName, script.src_userid, script.src_password, targetImageFullName, script.userid, script.password)
if (source.credentialsId) {
script.withCredentials([
script.usernamePassword(credentialsId: source.credentialsId, passwordVariable: 'src_password', usernameVariable: 'src_userid'),
script.usernamePassword(credentialsId: target.credentialsId, passwordVariable: 'password', usernameVariable: 'userid')
]) {
skopeoMoveImage(sourceImageFullName, script.src_userid, script.src_password, targetImageFullName, script.userid, script.password)
}
} else {
script.withCredentials([
script.usernamePassword(credentialsId: target.credentialsId, passwordVariable: 'password', usernameVariable: 'userid')
]) {
skopeoMoveImage(sourceImageFullName, '', '', targetImageFullName, script.userid, script.password)
}
}
}
//else not yet implemented here - available directly via containerPushToRegistry
@@ -49,7 +57,11 @@ class DockerUtils implements Serializable {
}
private void skopeoMoveImage(sourceImageFullName, sourceUserId, sourcePassword, targetImageFullName, targetUserId, targetPassword) {
script.sh "skopeo copy --src-tls-verify=false --src-creds=${BashUtils.quoteAndEscape(sourceUserId)}:${BashUtils.quoteAndEscape(sourcePassword)} --dest-tls-verify=false --dest-creds=${BashUtils.quoteAndEscape(targetUserId)}:${BashUtils.quoteAndEscape(targetPassword)} docker://${sourceImageFullName} docker://${targetImageFullName}"
if (sourceUserId && sourcePassword) {
script.sh "skopeo copy --src-tls-verify=false --src-creds=${BashUtils.quoteAndEscape(sourceUserId)}:${BashUtils.quoteAndEscape(sourcePassword)} --dest-tls-verify=false --dest-creds=${BashUtils.quoteAndEscape(targetUserId)}:${BashUtils.quoteAndEscape(targetPassword)} docker://${sourceImageFullName} docker://${targetImageFullName}"
} else {
script.sh "skopeo copy --src-tls-verify=false --dest-tls-verify=false --dest-creds=${BashUtils.quoteAndEscape(targetUserId)}:${BashUtils.quoteAndEscape(targetPassword)} docker://${sourceImageFullName} docker://${targetImageFullName}"
}
}

View File

@@ -294,10 +294,9 @@ class ContainerPushToRegistryTest extends BasePiperTest {
skopeoImage: 'skopeo:latest',
sourceImage: 'sourceImage:sourceTag',
sourceRegistryUrl: 'https://my.source.registry:44444',
sourceCredentialsId: 'testCredentialsId'
)
assertThat(shellCallRule.shell, hasItem('skopeo copy --src-tls-verify=false --src-creds=\'registryUser\':\'********\' --dest-tls-verify=false --dest-creds=\'registryUser\':\'********\' docker://my.source.registry:44444/sourceImage:sourceTag docker://my.registry:55555/testImage:tag'))
assertThat(shellCallRule.shell, hasItem('skopeo copy --src-tls-verify=false --dest-tls-verify=false --dest-creds=\'registryUser\':\'********\' docker://my.source.registry:44444/sourceImage:sourceTag docker://my.registry:55555/testImage:tag'))
assertThat(dockerRule.dockerParams.dockerImage, is('skopeo:latest'))
}
@@ -376,10 +375,9 @@ class ContainerPushToRegistryTest extends BasePiperTest {
dockerRegistryUrl: 'https://my.registry:55555',
sourceImage: 'sourceImage:sourceTag',
sourceRegistryUrl: 'https://my.source.registry:44444',
sourceCredentialsId: 'testCredentialsId'
)
assertThat(shellCallRule.shell, hasItem('skopeo copy --src-tls-verify=false --src-creds=\'registryUser\':\'********\' --dest-tls-verify=false --dest-creds=\'registryUser\':\'********\' docker://my.source.registry:44444/sourceImage:sourceTag docker://my.registry:55555/sourceImage:sourceTag'))
assertThat(shellCallRule.shell, hasItem('skopeo copy --src-tls-verify=false --dest-tls-verify=false --dest-creds=\'registryUser\':\'********\' docker://my.source.registry:44444/sourceImage:sourceTag docker://my.registry:55555/sourceImage:sourceTag'))
}
@Test