Signals an error. Useful if you want to conditionally abort some part of your program. You can also just throw new Exception(), but this step will avoid printing a stack trace
Prints a message to the log and sets the overall build result and the stage result to UNSTABLE. The message will also be associated with the stage result and may be shown in visualizations.
// stop and show status to UNSTABLE
if ( 'UNSTABLE' == currentBuild.result ) {
currentBuild.getRawBuild().getExecutor().interrupt(Result.UNSTABLE)
}
// stop and show status to NOT_BUILT
if ( 'ABORTED' == currentBuild.result ) {
currentBuild.rawBuild.executor.interrupt( Result.NOT_BUILT )
}
import hudson.model.Result
import hudson.model.Run
import jenkins.model.CauseOfInterruption
Run previousBuild = currentBuild.getPreviousBuildInProgress()
while ( previousBuild ) {
if ( previousBuild.isInProgress() && previousBuild.getExecutor() ) {
println ">> aborting previous build #${previousBuild.number}"
def cause = { "interrupted by build #${currentBuild.getId()}" as String } as CauseOfInterruption
previousBuild.getExecutor().interrupt( Result.ABORTED, cause )
}
previousBuilds = currentBuild.getPreviousBuildInProgress()
}
import hudson.model.Result
import hudson.model.Run
import jenkins.model.CauseOfInterruption
def abortPreviousBuilds() {
Run previousBuild = currentBuild.getPreviousBuildInProgress()
while (previousBuild != null) {
if (previousBuild.isInProgress()) {
def executor = previousBuild.getExecutor()
if (executor != null) {
println ">> Aborting older build #${previousBuild.number}"
def cause = { "interrupted by build #${currentBuild.getId()}" as String } as CauseOfInterruption
executor.interrupt(Result.ABORTED, cause)
}
}
previousBuild = previousBuild.getPreviousBuildInProgress()
}
}
import hudson.model.Result
import jenkins.model.CauseOfInterruption
//iterate through current project runs
build.getProject()._getRuns().each { id, run ->
def exec = run.getExecutor()
//if the run is not a current build and it has executor (running) then stop it
if( run!=build && exec!=null ) {
//prepare the cause of interruption
def cause = new CauseOfInterruption() {
public String getShortDescription() {
return "interrupted by build #${build.getId()}"
}
}
exec.interrupt( Result.ABORTED, cause )
}
}
//just for test do something long...
Thread.sleep(10000)
Thread.getAllStackTraces().keySet().each() {
t -> if (t.getName()=="YOUR THREAD NAME" ) { t.interrupt(); } // or t.stop();
}
// and
Jenkins.instance.getItemByFullName("JobName")
.getBuildByNumber(JobNumber)
.finish(
hudson.model.Result.ABORTED,
new java.io.IOException("Aborting build")
);
WorkflowRun run = Jenkins.instance.getItemByFullName("####YOUR_JOB_NAME####")._getRuns()[0]
FlowExecution exec = run.getExecution()
PipelineNodeGraphVisitor visitor = new PipelineNodeGraphVisitor(run)
def flowNodes = visitor.getPipelineNodes()
for (Iterator iterator = flowNodes.iterator(); iterator.hasNext();) {
def node = iterator.next()
if (node.getType() == FlowNodeWrapper.NodeType.STAGE) {
String stageName = node.getDisplayName()
def stageResult = node.getStatus().getResult()
println "Result of stage ${stageName} is ${stageResult}"
}
}
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker
import org.jenkinsci.plugins.workflow.graph.FlowNode
try {
// just for demo, a success step and a failure step
node {
sh 'true'
sh 'false'
}
} finally {
FlowGraphWalker walker = new FlowGraphWalker(currentBuild.rawBuild.getExecution())
for (FlowNode flowNode: walker) {
// do whatever you want with flowNode
echo flowNode.dump()
}
}
import hudson.model.Result
ansiColor( 'xterm' ) {
List r = [ 'SUCCESS', 'UNSTABLE', 'FAILURE', 'NOT_BUILT', 'ABORTED' ]
r.each { b ->
println " ~~> ${b}"
Result base = Result.fromString(b)
( r - b ).each { o ->
Result x = Result.fromString(o)
res = base.isWorseThan(x)
color.echo( "${res ? 'green' : 'red'}", "${base} isWorthThan ${x} : ${res}" )
}
}
} // ansiColor