Skip to content
Permalink
Browse files
JENKINS-28646 - Add support for removing volumes while removing a con…
…tainer

Add checkbox to remove volumes, default is unchecked
Build steps: RemoveContainer and RemoveAllContainers
Post build steps: Stop and remove container.
  • Loading branch information
npalm committed May 29, 2015
1 parent 3c2295f commit fae73c34bc76116ef64d073427df21275e8e3025
@@ -48,10 +48,12 @@ public String getDisplayName() {
public static class DockerPostBuildStep extends Recorder {

private final String containerIds;
private final boolean removeVolumes;

@DataBoundConstructor
public DockerPostBuildStep(String containerIds) {
this.containerIds = containerIds;
public DockerPostBuildStep(String containerIds, boolean removeVolumes) {
this.containerIds = containerIds;
this.removeVolumes = removeVolumes;
}

public BuildStepMonitor getRequiredMonitorService() {
@@ -81,7 +83,7 @@ public boolean perform(@SuppressWarnings("rawtypes") AbstractBuild build, Launch
}
}

RemoveCommand removeCommand = new RemoveCommand(containerIds, true);
RemoveCommand removeCommand = new RemoveCommand(containerIds, true, removeVolumes);
removeCommand.execute(build, clog);

return true;
@@ -20,8 +20,11 @@
*/
public class RemoveAllCommand extends DockerCommand {

private final boolean removeVolumes;

@DataBoundConstructor
public RemoveAllCommand() {
public RemoveAllCommand(boolean removeVolumes) {
this.removeVolumes = removeVolumes;
}

@Override
@@ -31,7 +34,7 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
List<Container> containers = client.listContainersCmd().withShowAll(true).exec();
for (Container container : containers) {
client.killContainerCmd(container.getId()).exec();
client.removeContainerCmd((container.getId())).exec();
client.removeContainerCmd((container.getId())).withRemoveVolumes(removeVolumes).exec();
console.logInfo("removed container id " + container.getId());
}
}
@@ -1,11 +1,13 @@
package org.jenkinsci.plugins.dockerbuildstep.cmd;

import com.github.dockerjava.api.command.RemoveContainerCmd;
import hudson.Extension;
import hudson.model.AbstractBuild;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.dockerbuildstep.log.ConsoleLogger;
import org.jenkinsci.plugins.dockerbuildstep.util.Resolver;
import org.kohsuke.stapler.DataBoundConstructor;
@@ -26,11 +28,13 @@

private final String containerIds;
private final boolean ignoreIfNotFound;
private final boolean removeVolumes;

@DataBoundConstructor
public RemoveCommand(String containerIds, boolean ignoreIfNotFound) {
public RemoveCommand(String containerIds, boolean ignoreIfNotFound, boolean removeVolumes) {
this.containerIds = containerIds;
this.ignoreIfNotFound = ignoreIfNotFound;
this.removeVolumes = removeVolumes;
}

public String getContainerIds() {
@@ -41,6 +45,10 @@ public boolean getIgnoreIfNotFound() {
return ignoreIfNotFound;
}

public boolean getRemoveVolumes() {
return removeVolumes;
}

@Override
public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLogger console)
throws DockerException {
@@ -57,7 +65,7 @@ public void execute(@SuppressWarnings("rawtypes") AbstractBuild build, ConsoleLo
id = id.trim();
try {
client.killContainerCmd(id).exec();
client.removeContainerCmd(id).exec();
client.removeContainerCmd(id).withRemoveVolumes(removeVolumes).exec();
console.logInfo("removed container id " + id);
} catch (NotFoundException e) {
if (!ignoreIfNotFound) {
@@ -5,4 +5,10 @@
<f:textbox />
</f:entry>

<f:advanced>
<f:entry field="removeVolumes" title="Remove volumes" description="Remove the volumes associated with the container." >
<f:checkbox />
</f:entry>
</f:advanced>

</j:jelly>
@@ -5,4 +5,9 @@
Kill and remove all conatiners
</f:entry>

<f:advanced>
<f:entry field="removeVolumes" title="Remove volumes" description="Remove the volumes associated with the container." >
<f:checkbox />
</f:entry>
</f:advanced>
</j:jelly>
@@ -9,6 +9,10 @@
<f:entry field="ignoreIfNotFound" title="Ignore if not found" description="Do not fail this step if any of the containers are not found." >
<f:checkbox />
</f:entry>

<f:entry field="removeVolumes" title="Remove volumes" description="Remove the volumes associated with the container." >
<f:checkbox />
</f:entry>
</f:advanced>

</j:jelly>

0 comments on commit fae73c3

Please sign in to comment.