Skip to content

Commit

Permalink
allow to override builder parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
gesellix committed Jan 13, 2016
1 parent fb941d6 commit 581badb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class DockerBuildTask extends DockerTask {
@InputDirectory
@Optional
File buildContextDirectory
@Input
@Optional
def buildParams

def tarOfBuildcontextTask
def targetFile
Expand Down Expand Up @@ -62,8 +65,11 @@ class DockerBuildTask extends DockerTask {
private def configureTarBuildContextTask() {
if (tarOfBuildcontextTask == null) {
targetFile = new File(getTemporaryDir(), "buildContext_${getNormalizedImageName()}.tar.gz")
tarOfBuildcontextTask = project.task([group: getGroup()], "tarBuildcontextFor${name.capitalize()}").doLast {
tarOfBuildcontextTask = project.task([group: getGroup()], "tarBuildcontextFor${name.capitalize()}")
tarOfBuildcontextTask.doFirst {
(targetFile as File).parentFile.mkdirs()
}
tarOfBuildcontextTask.doLast {
BuildContextBuilder.archiveTarFilesRecursively(getBuildContextDirectory(), targetFile)
}
tarOfBuildcontextTask.outputs.file(targetFile.absolutePath)
Expand All @@ -88,7 +94,11 @@ class DockerBuildTask extends DockerTask {
// at this point we need the buildContext
assert getBuildContext()

imageId = getDockerClient().build(getBuildContext())
if (getBuildParams()) {
imageId = getDockerClient().build(getBuildContext(), getBuildParams())
} else {
imageId = getDockerClient().build(getBuildContext())
}
if (getImageName()) {
logger.info "tag $imageId as '${getImageName()}'..."
getDockerClient().tag(imageId, getImageName(), true)
Expand Down
28 changes: 28 additions & 0 deletions src/test/groovy/de/gesellix/gradle/docker/PortFinder.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.gesellix.gradle.docker

class PortFinder {

static int findFreePort() {
ServerSocket socket = null
try {
socket = new ServerSocket(0)
socket.setReuseAddress(true)
int port = socket.getLocalPort()
try {
socket.close()
} catch (IOException ignore) {
// Ignore IOException on close()
}
return port
} catch (IOException ignore) {
} finally {
if (socket != null) {
try {
socket.close()
} catch (IOException ignore) {
}
}
}
throw new IllegalStateException("Could not find a free TCP/IP port")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ class DockerBuildTaskSpec extends Specification {
task.outputs.files.isEmpty()
}

def "delegates to dockerClient with buildContext and buildParams"() {
def inputStream = new FileInputStream(File.createTempFile("docker", "test"))

given:
task.buildContext = inputStream
task.buildParams = [rm: true, dockerfile: './custom.Dockerfile']
task.imageName = "imageName"

when:
task.execute()

then:
1 * dockerClient.build(inputStream, [rm: true, dockerfile: './custom.Dockerfile']) >> "4711"

then:
1 * dockerClient.tag("4711", "imageName", true)

and:
task.outputs.files.isEmpty()
}

// TODO this should become an integration test
def "accepts only task configs with at least one of buildContext or buildContextDirectory"() {
given:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.gesellix.gradle.docker.tasks

import de.gesellix.docker.client.DockerClient
import de.gesellix.gradle.docker.PortFinder
import org.gradle.api.GradleException
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.util.AvailablePortFinder
import spock.lang.Specification

class DockerContainerTaskSpec extends Specification {
Expand Down Expand Up @@ -465,7 +465,7 @@ class DockerContainerTaskSpec extends Specification {

def "healthcheck on already running container - timeout"() {
given:
int port = AvailablePortFinder.createPrivate().nextAvailable
int port = PortFinder.findFreePort()

when:
task.dockerHost = "tcp://127.0.0.1:999"
Expand Down Expand Up @@ -510,26 +510,26 @@ class DockerContainerTaskSpec extends Specification {

def "healthcheck on already running container"() {
given:
int port = 0

Thread server = new Thread() {
boolean initialized = false
int serverPort = 0
boolean stopped = false

public void run() {
ServerSocket ss;
ServerSocket ss
synchronized (this) {
serverPort = AvailablePortFinder.createPrivate().nextAvailable
ss = new ServerSocket(serverPort);
serverPort = PortFinder.findFreePort()
ss = new ServerSocket(serverPort)
initialized = true
notify()
}
while (true) {
Socket client = ss.accept();
while (!stopped) {
ss.accept()
}
}
}

def port
server.start()
synchronized (server) {
if (!server.initialized) {
Expand Down Expand Up @@ -574,6 +574,7 @@ class DockerContainerTaskSpec extends Specification {
task.changed == false

cleanup:
server.stop();
server.stopped = true
server.stop()
}
}

0 comments on commit 581badb

Please sign in to comment.