Allow to build image with multiple tags
Adds tags property to DockerBuildImage task and deprecates tag.
To preserve backward compatibility if tag is defined, tags are ignored.
Fix
MODIFY
src/functTest/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImageFunctionalTest.groovy
src/functTest/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImageFunctionalTest.groovy
package com.bmuschko.gradle.docker.tasks.image
import com.bmuschko.gradle.docker.AbstractFunctionalTest
-import com.bmuschko.gradle.docker.TestPrecondition
import org.gradle.testkit.runner.BuildResult
-import spock.lang.Requires
import spock.lang.Unroll
class DockerBuildImageFunctionalTest extends AbstractFunctionalTest {
@@ -55,13 +53,26 @@ class DockerBuildImageFunctionalTest extends AbstractFunctionalTest {
gradleTaskDefinition << [imageCreationWithBuildArgs(), imageCreationWithLabelParameter()]
}
+ def "can build image with multiple tags"() {
+ buildFile << buildImageWithTags()
+
+ when:
+ BuildResult result = build('buildImageWithTags', 'buildImageWithTag')
+
+ then:
+ result.output.contains("Using tags 'test/image:123', 'registry.com:5000/test/image:123' for image.")
+ result.output.contains("Using tag 'test/image:123' for image.")
+ }
+
+
+
private String imageCreation() {
"""
import com.bmuschko.gradle.docker.tasks.image.Dockerfile
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
task dockerfile(type: Dockerfile) {
- from 'ubuntu:12.04'
+ from 'alpine'
}
task buildImage(type: DockerBuildImage) {
@@ -78,7 +89,7 @@ class DockerBuildImageFunctionalTest extends AbstractFunctionalTest {
import com.bmuschko.gradle.docker.tasks.image.DockerInspectImage
task dockerfile(type: Dockerfile) {
- from 'ubuntu:12.04'
+ from 'alpine'
arg('arg1')
arg('arg2')
@@ -106,7 +117,7 @@ class DockerBuildImageFunctionalTest extends AbstractFunctionalTest {
import com.bmuschko.gradle.docker.tasks.image.DockerInspectImage
task dockerfile(type: Dockerfile) {
- from 'ubuntu:12.04'
+ from 'alpine'
}
task buildImage(type: DockerBuildImage) {
@@ -121,4 +132,27 @@ class DockerBuildImageFunctionalTest extends AbstractFunctionalTest {
}
"""
}
+
+ private String buildImageWithTags() {
+ """
+ import com.bmuschko.gradle.docker.tasks.image.Dockerfile
+ import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
+
+ task dockerfile(type: Dockerfile) {
+ from 'alpine'
+ }
+
+ task buildImageWithTags(type: DockerBuildImage) {
+ dependsOn dockerfile
+ inputDir = file("build/docker")
+ tags = ['test/image:123', 'registry.com:5000/test/image:123']
+ }
+
+ task buildImageWithTag(type: DockerBuildImage) {
+ dependsOn dockerfile
+ inputDir = file("build/docker")
+ tag = 'test/image:123'
+ }
+ """
+ }
}

MODIFY
src/main/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImage.groovy
src/main/groovy/com/bmuschko/gradle/docker/tasks/image/DockerBuildImage.groovy
@@ -36,11 +36,20 @@ class DockerBuildImage extends AbstractDockerRemoteApiTask implements RegistryCr
@Optional
File dockerFile
+ /**
+ * Tags for image.
+ */
+ @Input
+ @Optional
+ Set<String> tags = []
+
/**
* Tag for image.
+ * @deprecated use {@link #tags}
*/
@Input
@Optional
+ @Deprecated
String tag
@Input
@@ -98,6 +107,10 @@ class DockerBuildImage extends AbstractDockerRemoteApiTask implements RegistryCr
if(getTag()) {
logger.quiet "Using tag '${getTag()}' for image."
buildImageCmd.withTag(getTag())
+ } else if (getTags()) {
+ def tagListString = getTags().collect {"'${it}'"}.join(", ")
+ logger.quiet "Using tags ${tagListString} for image."
+ buildImageCmd.withTags(getTags())
}
if (getNoCache()) {

Allow to build image with multiple tags (#380)

Adds tags property to DockerBuildImage task and deprecates tag.
To preserve backward compatibility if tag is defined, tags are ignored.
Fix #286