A code sketch of how to wrap the Jenkins pipeline method node(...) { ... } to have pre and post steps.
This does not work!
| // 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> |