Skip to content

Commit

Permalink
Add --pull and --no-cache options to Build/Publish Docker Image-Step (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ybroeker authored and pjdarton committed Nov 19, 2019
1 parent da798bf commit debd444
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import hudson.model.Descriptor;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.accmod.Restricted;

import java.io.Serializable;
import java.util.List;
Expand All @@ -28,6 +30,8 @@ public class DockerBuildImageAction implements Action, Serializable, Cloneable,

public final boolean cleanupWithJenkinsJobDelete;
public final boolean pushOnSuccess;
public /* almost final */ boolean noCache;
public /* almost final */ boolean pull;

@Deprecated
public DockerBuildImageAction(String containerHost,
Expand Down Expand Up @@ -56,6 +60,30 @@ public DockerBuildImageAction(String containerHost,
this.tags = tags;
}

/**
* For internal use only, use {@link #DockerBuildImageAction(String, String, List, boolean, boolean)} instead.
*/
@Restricted(NoExternalUse.class)
public DockerBuildImageAction(String containerHost,
String containerId,
List<String> tags,
boolean cleanupWithJenkinsJobDelete,
boolean pushOnSuccess,
boolean noCache,
boolean pull) {
this(containerHost, containerId, tags, cleanupWithJenkinsJobDelete, pushOnSuccess);
setNoCache(noCache);
setPull(pull);
}

public void setPull(boolean pull) {
this.pull = pull;
}

public void setNoCache(boolean noCache) {
this.noCache = noCache;
}

public String getIconFileName() {
return "/plugin/docker-plugin/images/24x24/docker.png";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.remoting.RemoteInputStream;
Expand All @@ -41,6 +42,7 @@
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -126,6 +128,8 @@ public class DockerBuilderPublisher extends Builder implements Serializable, Sim
public final boolean cleanupWithJenkinsJobDelete;

public final String cloud;
public /* almost final */ boolean noCache;
public /* almost final */ boolean pull;

@DataBoundConstructor
public DockerBuilderPublisher(String dockerFileDirectory,
Expand Down Expand Up @@ -182,6 +186,24 @@ public DockerRegistryEndpoint getFromRegistry() {
return fromRegistry;
}

public boolean isNoCache() {
return noCache;
}

@DataBoundSetter
public void setNoCache(boolean noCache) {
this.noCache = noCache;
}

public boolean isPull() {
return pull;
}

@DataBoundSetter
public void setPull(boolean pull) {
this.pull = pull;
}

public static List<String> filterStringToList(String str) {
if (str == null) return Collections.<String>emptyList();

Expand Down Expand Up @@ -288,7 +310,9 @@ boolean run() throws IOException, InterruptedException {
log("Docker Build Response : " + imageId);

// Add an action to the build
run.addAction(new DockerBuildImageAction(dockerApi.getDockerHost().getUri(), imageId, tagsToUse, cleanupWithJenkinsJobDelete, pushOnSuccess));
Action action = new DockerBuildImageAction(dockerApi.getDockerHost().getUri(), imageId, tagsToUse, cleanupWithJenkinsJobDelete, pushOnSuccess, noCache, pull);

run.addAction(action);
run.save();

if (pushOnSuccess) {
Expand Down Expand Up @@ -346,6 +370,8 @@ public void onNext(BuildResponseItem item) {
final String imageId;
try(final DockerClient client = getClientWithNoTimeout()) {
imageId = client.buildImageCmd(tar)
.withNoCache(noCache)
.withPull(pull)
.withBuildAuthConfigs(auths)
.exec(resultCallback)
.awaitImageId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@
<f:checkbox default="false"/>
</f:entry>

<f:entry title="${%Disable caching}" field="noCache">
<f:checkbox default="false"/>
</f:entry>

<f:entry title="${%Pull base image}" field="pull">
<f:checkbox default="false"/>
</f:entry>

</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
If set, builds the image with <code>--no-cache</code> which disables caching of layers.
See the docker <a href="https://docs.docker.com/engine/reference/commandline/build/">build command</a> for more information.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
If set, builds the image with <code>--pull</code> to pull the latest version of the base image, instead of using the local one.
See the docker <a href="https://docs.docker.com/engine/reference/commandline/build/">build command</a> for more information.
</div>

0 comments on commit debd444

Please sign in to comment.