-
-
Save abayer/f39f8371e5876f53ab6d7917c8d3d984 to your computer and use it in GitHub Desktop.
Declarative Pipeline Docker-with-cleanup agent type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent { | |
dockerWithCleanup { | |
image "someImage" | |
label "some-label" | |
} | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- dependency needed --> | |
<dependency> | |
<groupId>org.jenkinsci.plugins</groupId> | |
<artifactId>pipeline-model-definition</artifactId> | |
<version>1.1.2</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note to self... the
Docker...Script
is loaded by "magic".