Skip to content

Commit

Permalink
JENKINS-28600: Support Dockerfiles with non-standard names.
Browse files Browse the repository at this point in the history
  • Loading branch information
johndistasio committed Jun 6, 2015
1 parent 22abdb7 commit 61e55dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
32 changes: 22 additions & 10 deletions src/main/java/com/cloudbees/dockerpublish/DockerBuilder.java
Expand Up @@ -12,6 +12,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 @@ -56,6 +57,7 @@ public class DockerBuilder extends Builder {
private String repoName;
private boolean noCache;
private boolean forcePull;
private String buildContext;
@CheckForNull
private String dockerfilePath;
private boolean skipBuild;
Expand Down Expand Up @@ -126,6 +128,15 @@ public void setForcePull(boolean forcePull) {
this.forcePull = forcePull;
}

public String getBuildContext() {
return buildContext;
}

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

public String getDockerfilePath() {
return dockerfilePath;
}
Expand Down Expand Up @@ -295,33 +306,34 @@ private boolean maybeTagOnly() throws MacroEvaluationException, IOException, Int
}
return executeCmd(result);
}

private boolean buildAndTag() throws MacroEvaluationException, IOException, InterruptedException {
String context = defined(getDockerfilePath()) ?
getDockerfilePath() : ".";
File context = new File(defined(getBuildContext()) ? getBuildContext() : ".");
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=" + new File(context, 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=" + new File(context, 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. 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 61e55dc

Please sign in to comment.