Skip to content

Instantly share code, notes, and snippets.

@abayer
Last active July 22, 2019 14:03
Show Gist options
  • Save abayer/f39f8371e5876f53ab6d7917c8d3d984 to your computer and use it in GitHub Desktop.
Save abayer/f39f8371e5876f53ab6d7917c8d3d984 to your computer and use it in GitHub Desktop.
Declarative Pipeline Docker-with-cleanup agent type
// This would be in src/main/java/whatever/DockerPipelineWithCleanup.java
package whatever;
import hudson.Extension;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.AbstractDockerAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class DockerPipelineWithCleanup extends AbstractDockerAgent<DockerPipelineWithCleanup> {
private String image;
@DataBoundConstructor
public DockerPipelineWithCleanup(@Nonnull String image) {
this.image = image;
}
public @Nonnull String getImage() {
return image;
}
// ordinal is to make sure this doesn't take priority over the built-in agent types.
@Extension(ordinal = -100) @Symbol("dockerWithCleanup")
public static class DescriptorImpl extends DeclarativeAgentDescriptor<DockerPipelineWithCleanup> {
}
}
// This would be in src/main/resources/whatever/DockerPipelineWithCleanupScript.groovy
package whatever
import hudson.model.Result
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.AbstractDockerPipelineScript
import org.jenkinsci.plugins.workflow.cps.CpsScript
public class DockerPipelineWithCleanupScript extends AbstractDockerPipelineScript<DockerPipelineWithCleanup> {
public DockerPipelineWithCleanupScript(CpsScript s, DockerPipelineWithCleanup a) {
super(s, a)
}
@Override
public Closure runImage(Closure body) {
return {
if (!Utils.withinAStage()) {
script.stage(SyntheticStageNames.agentSetup()) {
try {
script.getProperty("docker").image(describable.image).pull()
} catch (Exception e) {
script.getProperty("currentBuild").result = Result.FAILURE
Utils.markStageFailedAndContinued(SyntheticStageNames.agentSetup())
throw e
}
}
}
try {
// CLEANUP
script.sh("whatever you need to cleanup the images")
// CLEANUP
script.getProperty("docker").image(describable.image).inside(describable.args, {
body.call()
})
} catch (Exception e) {
script.getProperty("currentBuild").result = Result.FAILURE
throw e
} finally {
// CLEANUP
script.sh("whatever you need to cleanup the images")
// CLEANUP
}
}
}
}
pipeline {
agent {
dockerWithCleanup {
image "someImage"
label "some-label"
}
}
...
}
<!-- dependency needed -->
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>1.1.2</version>
</dependency>
@docwhat
Copy link

docwhat commented Apr 7, 2017

Note to self... the Docker...Script is loaded by "magic".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment