Skip to content

Commit

Permalink
Remove dependsOn configuration, use CopySpec to evaluate files, fixes
Browse files Browse the repository at this point in the history
#77, fixes #86, fixes #97 (#101)
  • Loading branch information
uschi2000 committed Jan 24, 2017
1 parent 8185c27 commit c135ac0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 40 deletions.
9 changes: 3 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ automatically include outputs of task dependencies in the Docker build context.
be stripped before applying a specific tag; defaults to the empty set
- `dockerfile` (optional) dockerfile to use for building the image; defaults to
`${projectDir}/Dockerfile`
- `dependsOn` (optional) an argument list of tasks that docker builds must depend on;
defaults to the empty set. All task outputs are added to the Docker build context. A standard use of the plugin is to specify `dependsOn distTar` without any additional `files` (see below).
- `files` (optional) an argument list of files to be included in the Docker build context; if this parameter is omitted, all files in `${projectDir}` are included
- `files` (optional) an argument list of files to be included in the Docker build context, evaluated per `Project#files`. For example, `files tasks.distTar.outputs` adds the TAR/TGZ file produced by the `distTar` tasks, and `files tasks.distTar.outputs, 'my-file.txt'` adds the archive in addition to file `my-file.txt` from the project root directory.
- `buildArgs` (optional) an argument map of string to string which will set --build-arg
arguments to the docker build command; defaults to empty, which results in no --build-arg parameters
- `pull` (optional) a boolean argument which defines whether Docker should attempt to pull
Expand All @@ -63,7 +61,7 @@ Canonical configuration for building a Docker image from a distribution archive:
// Assumes that Gradle "distribution" plugin is applied
docker {
name 'hub.docker.com/username/my-app:version'
dependsOn tasks.distTar // adds resulting *.tgz to the build context
files tasks.distTar.outputs // adds resulting *.tgz to the build context
}
```
Expand All @@ -74,8 +72,7 @@ docker {
name 'hub.docker.com/username/my-app:version'
tags 'latest'
dockerfile 'Dockerfile'
dependsOn tasks.distTar
files 'file1.txt', 'file2.txt'
files tasks.distTar.outputs, 'file1.txt', 'file2.txt'
buildArgs([BUILD_VERSION: 'version'])
pull true
}
Expand Down
29 changes: 11 additions & 18 deletions src/main/groovy/com/palantir/gradle/docker/DockerExtension.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package com.palantir.gradle.docker

import org.gradle.api.Project
import org.gradle.api.Task

import com.google.common.base.Preconditions
import com.google.common.base.Strings
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.CopySpec

class DockerExtension {
Project project
Expand All @@ -31,18 +31,20 @@ class DockerExtension {
private String dockerComposeTemplate = 'docker-compose.yml.template'
private String dockerComposeFile = 'docker-compose.yml'
private Set<Task> dependencies = ImmutableSet.of()
private Set<String> files = ImmutableSet.of()
private Set<String> tags = ImmutableSet.of()
private Map<String, String> buildArgs = ImmutableMap.of()
private boolean pull = false

private File resolvedDockerfile = null
private File resolvedDockerComposeTemplate = null
private File resolvedDockerComposeFile = null
private Set<File> resolvedFiles = null

// The CopySpec defining the Docker Build Context files
private final CopySpec copySpec

public DockerExtension(Project project) {
this.project = project
this.copySpec = project.copySpec()
}

public void setName(String name) {
Expand Down Expand Up @@ -75,8 +77,8 @@ class DockerExtension {
return dependencies
}

public void files(String... args) {
this.files = ImmutableSet.copyOf(args)
public void files(Object... files) {
copySpec.from(files)
}

public Set<String> getTags() {
Expand All @@ -99,8 +101,8 @@ class DockerExtension {
return resolvedDockerComposeFile
}

public Set<String> getResolvedFiles() {
return resolvedFiles
public CopySpec getCopySpec() {
return copySpec
}

public void resolvePathsAndValidate() {
Expand All @@ -111,15 +113,6 @@ class DockerExtension {
Preconditions.checkArgument(resolvedDockerfile.exists(), "dockerfile '%s' does not exist.", dockerfile)
resolvedDockerComposeFile = project.file(dockerComposeFile)
resolvedDockerComposeTemplate = project.file(dockerComposeTemplate)

ImmutableSet.Builder<File> builder = ImmutableSet.builder()
for (String file : files) {
def resFile = project.file(file)
Preconditions.checkArgument(resFile.exists(), "file '%s' does not exist.", file)
builder.add(resFile)
}

resolvedFiles = builder.build()
}

public Map<String, String> getBuildArgs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,12 @@ class PalantirDockerPlugin implements Plugin<Project> {
clean.delete dockerDir

prepare.with {
with ext.copySpec
from(ext.resolvedDockerfile) {
rename { fileName ->
fileName.replace(ext.resolvedDockerfile.getName(), 'Dockerfile')
}
}
from ext.dependencies*.outputs
if (ext.resolvedFiles) {
// provide compatibility with optional 'files' parameter:
from ext.resolvedFiles
} else {
// default: copy all files excluding the project buildDir
from(project.projectDir) {
exclude "${project.buildDir.name}"
}
}
into dockerDir
}

Expand All @@ -112,7 +103,7 @@ class PalantirDockerPlugin implements Plugin<Project> {
commandLine buildCommandLine
dependsOn ext.getDependencies()
logging.captureStandardOutput LogLevel.INFO
logging.captureStandardError LogLevel.ERROR
logging.captureStandardError LogLevel.ERROR
}

if (!ext.tags.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class PalantirDockerPluginTests extends AbstractPluginTest {
execCond("docker rmi -f ${id}") || true
}

def 'when no files are specified, then all files from the project directory are added to the docker context'() {
def 'can add files from project directory to build context'() {
given:
String id = 'id9'
String filename = "bar.txt"
Expand All @@ -404,6 +404,7 @@ class PalantirDockerPluginTests extends AbstractPluginTest {
docker {
name '${id}'
files "bar.txt"
}
""".stripIndent()
createFile(filename)
Expand All @@ -417,7 +418,7 @@ class PalantirDockerPluginTests extends AbstractPluginTest {
execCond("docker rmi -f ${id}") || true
}

def 'project files and outputs from dependencies are added to the docker context'() {
def 'when adding a project-dir file and a Tar file, then they both end up (unzipped) in the docker image'() {
given:
String id = 'id10'
createFile('from_project')
Expand Down Expand Up @@ -446,7 +447,7 @@ class PalantirDockerPluginTests extends AbstractPluginTest {
docker {
name '${id}'
dependsOn myTgz
files tasks.myTgz.outputs, 'from_project'
}
""".stripIndent()
when:
Expand Down Expand Up @@ -484,7 +485,7 @@ class PalantirDockerPluginTests extends AbstractPluginTest {
docker {
name '${id}'
dependsOn distTar
files tasks.distTar.outputs
}
""".stripIndent()
when:
Expand All @@ -511,4 +512,3 @@ class PalantirDockerPluginTests extends AbstractPluginTest {
"host:port/v1:1" | "latest" | "host:port/v1:latest"
}
}

0 comments on commit c135ac0

Please sign in to comment.