Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to build image with multiple tags #380

Merged
merged 1 commit into from May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,9 +1,7 @@
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 {
Expand Down Expand Up @@ -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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}

task buildImage(type: DockerBuildImage) {
Expand All @@ -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')
Expand Down Expand Up @@ -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) {
Expand All @@ -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'
}
"""
}
}
Expand Up @@ -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
Expand Down Expand Up @@ -95,9 +104,13 @@ class DockerBuildImage extends AbstractDockerRemoteApiTask implements RegistryCr
buildImageCmd = dockerClient.buildImageCmd(getInputDir())
}

if (getTag()) {
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())
Copy link
Collaborator

@cdancy cdancy May 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we allow for both? Maybe something like:

def allTags = [] + getTags()
if (getTag()) { allTags << getTag() }
if (allTags) {
    def tagListString = allTags.collect {"'${it}'"}.join(", ")
    logger.quiet "Using tags ${tagListString} for image."
    buildImageCmd.withTags(allTags)
}

May be a better more succinct way to write this but I can def see folks defining a default tag and then possibly through a doFirst block add additional tags at runtime.

}

if (getNoCache()) {
Expand Down