Skip to content

Commit

Permalink
Merge pull request #23 from johndistasio/JENKINS-28600
Browse files Browse the repository at this point in the history
JENKINS-28600: Support Dockerfiles with non-standard names.
  • Loading branch information
carlossg committed Oct 19, 2015
2 parents ebc78ec + 10b35e5 commit 73c8a2b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
target
work
*.iml
.idea/
.classpath
.project
.settings/
Expand Down
38 changes: 27 additions & 11 deletions src/main/java/com/cloudbees/dockerpublish/DockerBuilder.java
Expand Up @@ -4,7 +4,9 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
Expand All @@ -13,6 +15,7 @@
import hudson.util.FormValidation;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.OutputStream;
Expand Down Expand Up @@ -60,6 +63,8 @@ public class DockerBuilder extends Builder {
private boolean noCache;
private boolean forcePull;
@CheckForNull
private String buildContext;
@CheckForNull
private String dockerfilePath;
private boolean skipBuild;
private boolean skipDecorate;
Expand Down Expand Up @@ -129,13 +134,22 @@ public void setForcePull(boolean forcePull) {
this.forcePull = forcePull;
}

public String getBuildContext() {
return buildContext;
}

@DataBoundSetter
public void setBuildContext(String buildContext) {
this.buildContext = Util.fixEmptyAndTrim(buildContext);
}

public String getDockerfilePath() {
return dockerfilePath;
}

@DataBoundSetter
public void setDockerfilePath(String dockerfilePath) {
this.dockerfilePath = dockerfilePath;
this.dockerfilePath = Util.fixEmptyAndTrim(dockerfilePath);
}

public boolean isSkipBuild() {
Expand Down Expand Up @@ -302,33 +316,35 @@ private boolean maybeTagOnly() throws MacroEvaluationException, IOException, Int
}
return executeCmd(result);
}

private boolean buildAndTag() throws MacroEvaluationException, IOException, InterruptedException {
String context = defined(getDockerfilePath()) ?
getDockerfilePath() : ".";
FilePath context = defined(getBuildContext()) ?
new FilePath(new File(getBuildContext())) : build.getWorkspace();
Iterator<String> i = getNameAndTag().iterator();
Result lastResult = new Result();
if (i.hasNext()) {
lastResult = executeCmd("docker build -t " + i.next()
+ ((isNoCache()) ? " --no-cache=true " : "") + " "
+ ((isForcePull()) ? " --pull=true " : "") + " "
+ context);
+ ((isNoCache()) ? " --no-cache=true " : "") + " "
+ ((isForcePull()) ? " --pull=true " : "") + " "
+ (defined(getDockerfilePath()) ? " --file=" + getDockerfilePath() : "") + " "
+ context);
}
// get the image to save rebuilding it to apply the other tags
String image = getImageBuiltFromStdout(lastResult.stdout);
if (image != null) {
// we know the image name so apply the tags directly
while (lastResult.result && i.hasNext()) {
lastResult = executeCmd("docker tag --force=true " + image + " " + i.next());
}
}
processFingerprints(image);
} else {
// we don't know the image name so rebuild the image for each tag
while (lastResult.result && i.hasNext()) {
lastResult = executeCmd("docker build -t " + i.next()
+ ((isNoCache()) ? " --no-cache=true " : "") + " "
+ ((isForcePull()) ? " --pull=true " : "") + " "
+ context);
+ ((isNoCache()) ? " --no-cache=true " : "") + " "
+ ((isForcePull()) ? " --pull=true " : "") + " "
+ (defined(getDockerfilePath()) ? " --file=" + getDockerfilePath() : "") + " "
+ context);
processFingerprintsFromStdout(lastResult.stdout);
}
}
Expand Down
Expand Up @@ -51,8 +51,13 @@
<f:checkbox />
</f:entry>

<f:entry title="Directory Dockerfile is in" field="dockerfilePath"
description="The project root is where the Dockerfile is looked for by default - if you need another path, enter it here">
<f:entry title="Build Context" field="buildContext"
description="The project root path for the build. Defaults to the workspace root if not specified.">
<f:textbox />
</f:entry>

<f:entry title="Dockerfile Path" field="dockerfilePath"
description="Path to the Dockerfile for this build. File must be relative to build context. Can be used to specify a Dockerfile with a non-standard filename. Uses Docker client default if not specified.">
<f:textbox />
</f:entry>

Expand Down

0 comments on commit 73c8a2b

Please sign in to comment.