From 79894bb36f1ed7efb799d98988011fbd556e8218 Mon Sep 17 00:00:00 2001 From: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> Date: Wed, 19 Jun 2019 13:00:56 +0200 Subject: [PATCH] Fix docker versioning with registry port (#686) Fix issue when retrieving version via tag in `FROM` section. We also need to consider that the registry may contain a port. * Update DockerArtifactVersioning.groovy * add error message for missing image tag in FROM line --- .../piper/versioning/DockerArtifactVersioning.groovy | 6 +++++- .../versioning/DockerArtifactVersioningTest.groovy | 12 ++++++++++++ .../DockerArtifactVersioning/Dockerfile_registryPort | 5 +++++ .../Dockerfile_registryPortNoTag | 5 +++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPort create mode 100644 test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPortNoTag diff --git a/src/com/sap/piper/versioning/DockerArtifactVersioning.groovy b/src/com/sap/piper/versioning/DockerArtifactVersioning.groovy index 80faf20f1..6aea1eba0 100644 --- a/src/com/sap/piper/versioning/DockerArtifactVersioning.groovy +++ b/src/com/sap/piper/versioning/DockerArtifactVersioning.groovy @@ -56,7 +56,11 @@ class DockerArtifactVersioning extends ArtifactVersioning { def version = null for (def i = 0; i < lines.size(); i++) { if (lines[i].startsWith('FROM') && lines[i].indexOf(':') > 0) { - version = lines[i].split(':')[1] + def imageParts = lines[i].split(':') + version = imageParts[imageParts.size()-1] + if (version.contains('/')) { + script.error "[${getClass().getSimpleName()}] FROM statement does not contain an explicit image version: ${lines[i]} " + } break } } diff --git a/test/groovy/com/sap/piper/versioning/DockerArtifactVersioningTest.groovy b/test/groovy/com/sap/piper/versioning/DockerArtifactVersioningTest.groovy index 5a068f3d1..b10d984e9 100644 --- a/test/groovy/com/sap/piper/versioning/DockerArtifactVersioningTest.groovy +++ b/test/groovy/com/sap/piper/versioning/DockerArtifactVersioningTest.groovy @@ -51,6 +51,18 @@ class DockerArtifactVersioningTest extends BasePiperTest{ assertTrue(loggingRule.log.contains('[DockerArtifactVersioning] Version from Docker base image tag: 1.2.3')) } + @Test + void testVersioningFromWithRegistryPort() { + DockerArtifactVersioning av = new DockerArtifactVersioning(nullScript, [filePath: 'Dockerfile_registryPort', dockerVersionSource: 'FROM']) + assertEquals('1.2.3', av.getVersion()) + } + + @Test + void testVersioningFromWithMissingTag() { + thrown.expectMessage('FROM statement does not contain an explicit image version') + new DockerArtifactVersioning(nullScript, [filePath: 'Dockerfile_registryPortNoTag', dockerVersionSource: 'FROM']).getVersion() + } + @Test void testVersioningEnv() { av = new DockerArtifactVersioning(nullScript, [filePath: 'Dockerfile', dockerVersionSource: 'TEST']) diff --git a/test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPort b/test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPort new file mode 100644 index 000000000..011d18788 --- /dev/null +++ b/test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPort @@ -0,0 +1,5 @@ +FROM registry:4444/path/image:1.2.3 + +USER root + +ENV TEST 2.3.4 diff --git a/test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPortNoTag b/test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPortNoTag new file mode 100644 index 000000000..3ff5113ec --- /dev/null +++ b/test/resources/versioning/DockerArtifactVersioning/Dockerfile_registryPortNoTag @@ -0,0 +1,5 @@ +FROM registry:4444/path/image + +USER root + +ENV TEST 2.3.4