You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Add option to tag artifact version to containerPushToRegistryStep (#2859)
* Add option to tag artifact version to containerPushToRegistryStep * Update vars/containerPushToRegistry.groovy Co-authored-by: breuerjo <36748317+breuerjo@users.noreply.github.com> * commonPipelineEnvironment instead of globalPipelineEnvironment Co-authored-by: breuerjo <36748317+breuerjo@users.noreply.github.com> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
		| @@ -28,7 +28,7 @@ containerPushToRegistry script: this, | ||||
|                         dockerRegistryUrl: 'https://my.target.docker.registry:50000' | ||||
| ``` | ||||
|  | ||||
| **OPTION B:** To push a locally build docker image into the target registry (only possible when a Docker deamon is available on your Jenkins node): | ||||
| **OPTION B:** To push a locally built docker image into the target registry (only possible when a Docker daemon is available on your Jenkins node): | ||||
|  | ||||
| ```groovy | ||||
| containerPushToRegistry script: this, | ||||
|   | ||||
| @@ -161,7 +161,48 @@ class ContainerPushToRegistryTest extends BasePiperTest { | ||||
|         assertThat(dockerMockPushes, hasItem('latest')) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void testBuildImagePushArtifactVersion() throws Exception { | ||||
|         nullScript.commonPipelineEnvironment.setArtifactVersion('1.0.0') | ||||
|         def dockerBuildImage = new ContainerImageMock() | ||||
|         stepRule.step.containerPushToRegistry( | ||||
|             script: nullScript, | ||||
|             dockerRegistryUrl: 'https://testRegistry', | ||||
|             dockerCredentialsId: 'testCredentialsId', | ||||
|             dockerBuildImage: dockerBuildImage, | ||||
|             tagArtifactVersion: true | ||||
|         ) | ||||
|  | ||||
|         assertThat(dockerMock.targetRegistry.url, is('https://testRegistry')) | ||||
|         assertThat(dockerMock.targetRegistry.credentials, is('testCredentialsId')) | ||||
|         assertThat(dockerMock.sourceRegistry, is (null)) | ||||
|         assertThat(dockerMock.image, is('test')) | ||||
|         assertThat(dockerMockPushes, hasItem('default')) | ||||
|         assertThat(dockerMockPushes, not(hasItem('latest'))) | ||||
|         assertThat(dockerMockPushes, hasItem('1.0.0')) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void testBuildImagePushLatestAndArtifactVersion() throws Exception { | ||||
|         nullScript.commonPipelineEnvironment.setArtifactVersion('1.0.0') | ||||
|         def dockerBuildImage = new ContainerImageMock() | ||||
|         stepRule.step.containerPushToRegistry( | ||||
|             script: nullScript, | ||||
|             dockerRegistryUrl: 'https://testRegistry', | ||||
|             dockerCredentialsId: 'testCredentialsId', | ||||
|             dockerBuildImage: dockerBuildImage, | ||||
|             tagArtifactVersion: true, | ||||
|             tagLatest: true | ||||
|         ) | ||||
|  | ||||
|         assertThat(dockerMock.targetRegistry.url, is('https://testRegistry')) | ||||
|         assertThat(dockerMock.targetRegistry.credentials, is('testCredentialsId')) | ||||
|         assertThat(dockerMock.sourceRegistry, is (null)) | ||||
|         assertThat(dockerMock.image, is('test')) | ||||
|         assertThat(dockerMockPushes, hasItem('default')) | ||||
|         assertThat(dockerMockPushes, hasItem('latest')) | ||||
|         assertThat(dockerMockPushes, hasItem('1.0.0')) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void testFromEnv() { | ||||
| @@ -278,6 +319,48 @@ class ContainerPushToRegistryTest extends BasePiperTest { | ||||
|         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:latest')) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void testKubernetesMoveTagArtifactVersion() { | ||||
|         nullScript.commonPipelineEnvironment.setArtifactVersion('1.0.0') | ||||
|         binding.setVariable('docker', null) | ||||
|         shellCallRule.setReturnValue('docker ps -q > /dev/null', 1) | ||||
|  | ||||
|         stepRule.step.containerPushToRegistry( | ||||
|             script: nullScript, | ||||
|             dockerCredentialsId: 'testCredentialsId', | ||||
|             dockerImage: 'testImage:tag', | ||||
|             dockerRegistryUrl: 'https://my.registry:55555', | ||||
|             sourceImage: 'sourceImage:sourceTag', | ||||
|             sourceRegistryUrl: 'https://my.source.registry:44444', | ||||
|             tagArtifactVersion: true | ||||
|         ) | ||||
|  | ||||
|         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(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:1.0.0')) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void testKubernetesMoveTagLatestAndArtifactVersion() { | ||||
|         nullScript.commonPipelineEnvironment.setArtifactVersion('1.0.0') | ||||
|         binding.setVariable('docker', null) | ||||
|         shellCallRule.setReturnValue('docker ps -q > /dev/null', 1) | ||||
|  | ||||
|         stepRule.step.containerPushToRegistry( | ||||
|             script: nullScript, | ||||
|             dockerCredentialsId: 'testCredentialsId', | ||||
|             dockerImage: 'testImage:tag', | ||||
|             dockerRegistryUrl: 'https://my.registry:55555', | ||||
|             sourceImage: 'sourceImage:sourceTag', | ||||
|             sourceRegistryUrl: 'https://my.source.registry:44444', | ||||
|             tagLatest: true, | ||||
|             tagArtifactVersion: true | ||||
|         ) | ||||
|  | ||||
|         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(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:latest')) | ||||
|         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:1.0.0')) | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     void testKubernetesSourceOnly() { | ||||
|         binding.setVariable('docker', null) | ||||
|   | ||||
| @@ -41,7 +41,9 @@ import static com.sap.piper.Prerequisites.checkScript | ||||
|     /** Defines the id of the Jenkins username/password credentials containing the credentials for the source Docker registry. */ | ||||
|     'sourceCredentialsId', | ||||
|     /** Defines if the image should be tagged as `latest`*/ | ||||
|     'tagLatest' | ||||
|     'tagLatest', | ||||
|     /** Defines if the image should be tagged with the artifact version */ | ||||
|     'tagArtifactVersion' | ||||
| ]) | ||||
| @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS | ||||
|  | ||||
| @@ -68,6 +70,7 @@ void call(Map parameters = [:]) { | ||||
|             .mixin(parameters, PARAMETER_KEYS) | ||||
|             .addIfEmpty('sourceImage', script.commonPipelineEnvironment.getValue('containerImage')) | ||||
|             .addIfEmpty('sourceRegistryUrl', script.commonPipelineEnvironment.getValue('containerRegistryUrl')) | ||||
|             .mixin(artifactVersion: script.commonPipelineEnvironment.getArtifactVersion()) | ||||
|             .withMandatoryProperty('dockerCredentialsId') | ||||
|             .withMandatoryProperty('dockerRegistryUrl') | ||||
|             .use() | ||||
| @@ -113,6 +116,8 @@ void call(Map parameters = [:]) { | ||||
|                 config.dockerBuildImage.push() | ||||
|                 if (config.tagLatest) | ||||
|                     config.dockerBuildImage.push('latest') | ||||
|                 if (config.tagArtifactVersion ) | ||||
|                     config.dockerBuildImage.push(config.artifactVersion) | ||||
|             } | ||||
|         } else { | ||||
|             //handling for Kubernetes case | ||||
| @@ -127,6 +132,10 @@ void call(Map parameters = [:]) { | ||||
|                         def latestImage = "${config.dockerImage.split(':')[0]}:latest" | ||||
|                         dockerUtils.moveImage([image: config.sourceImage, registryUrl: config.sourceRegistryUrl], [image: latestImage, registryUrl: config.dockerRegistryUrl, credentialsId: config.dockerCredentialsId]) | ||||
|                     } | ||||
|                     if (config.tagArtifactVersion) { | ||||
|                         def imageName = "${config.dockerImage.split(':')[0]}:${config.artifactVersion}" | ||||
|                         dockerUtils.moveImage([image: config.sourceImage, registryUrl: config.sourceRegistryUrl], [image: imageName, registryUrl: config.dockerRegistryUrl, credentialsId: config.dockerCredentialsId]) | ||||
|                     } | ||||
|                 } else { | ||||
|                     error "[${STEP_NAME}] Running on Kubernetes: Only moving images from one registry to another supported." | ||||
|                 } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user