build stage
[!NOTE|label:references:]
show build stages details
import org.jenkinsci.plugins.workflow.job.*
import org.jenkinsci.plugins.workflow.flow.*
import io.jenkins.blueocean.rest.impl.pipeline.*
import org.jenkinsci.plugins.workflow.cps.*
import org.jenkinsci.plugins.workflow.graph.FlowNode;
final String JOB_NAME = '/marslo/sandbox'
final int BUILD_NUMBER = 17
WorkflowRun run = Jenkins.instance
.getItemByFullName( JOB_NAME )
.getBuildByNumber( BUILD_NUMBER )
PipelineNodeGraphVisitor visitor = new PipelineNodeGraphVisitor(run)
List<FlowNodeWrapper> flowNodes = visitor.getPipelineNodes()
flowNodes.each {
println """
${it.getDisplayName()} :
getRun() : ${it.getRun()}
getResult : ${it.status.getResult()}
getState : ${it.status.getState()}
getType : ${it.getType()}
getId : ${it.getId()}
isActive : ${it.node.active}
searchUrl : ${it.node.getSearchUrl()}
getUrl : ${Jenkins.instance.getRootUrl() + it.node.getUrl()}
iconColor : ${it.node.getIconColor()}
"""
// println """
// getError : ${it.node.getError()}
// getAction : ${it.node.getActions()}
// getDisplayFunctionName : ${it.node.getDisplayFunctionName()}
// getTypeDisplayName : ${it.node.getTypeDisplayName()}
// getTypeFunctionName : ${it.node.getTypeFunctionName()}
// it.node.metaClass.methods.name : ${it.node.metaClass.methods*.name.sort().unique()}
// it.getClass() : ${it.getClass()}
// """
println " parents : " + it.getParents().collect { p ->
[
'name' : p.displayName,
'status' : p.status.getResult(),
'id' : p.id,
'active' : p.node.active
]
}.flatten()
println '--------------'
}
- result
get parent stage ID
import org.jenkinsci.plugins.workflow.job.*
import org.jenkinsci.plugins.workflow.flow.*
import org.jenkinsci.plugins.workflow.cps.*
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import io.jenkins.blueocean.rest.impl.pipeline.*
import io.jenkins.blueocean.rest.model.*
import io.jenkins.blueocean.rest.model.BlueRun.*
def withFlowNodes( String name, int buildNumber, Closure body ) {
WorkflowRun run = Jenkins.instance
.getItemByFullName( name )
.getBuildByNumber( buildNumber )
PipelineNodeGraphVisitor visitor = new PipelineNodeGraphVisitor( run )
List<FlowNodeWrapper> flowNodes = visitor.getPipelineNodes()
body( flowNodes )
}
def isStageFinished( String keyword, String job, int buildNumber, String type = 'parallel' ) {
withFlowNodes ( job, buildNumber ) { flowNodes ->
List<String> parentIds = flowNodes.findAll {
it.displayName.startsWith(keyword) && it.getType() == FlowNodeWrapper.NodeType.valueOf( type.toUpperCase() )
}.collectMany { it.parents.collect{ p -> p.id } }.unique()
flowNodes.findAll { parentIds.contains( it.id ) }.every { it.status.getState() == BlueRun.BlueRunState.FINISHED }
} // withFlowNodes
}
get stage build status via parent stage name
import org.jenkinsci.plugins.workflow.job.*
import org.jenkinsci.plugins.workflow.flow.*
import org.jenkinsci.plugins.workflow.cps.*
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import io.jenkins.blueocean.rest.impl.pipeline.*
import io.jenkins.blueocean.rest.model.*
import io.jenkins.blueocean.rest.model.BlueRun.*
@NonCPS
def on( String job, int buildNumber ) {
[
isBuilding : { ->
isBuilding( job, buildNumber )
}
stageStatus : { String keyword, String type = 'parallel', String parentStage = 'Parallel' ->
stageStatus ( keyword, job, buildNumber, type, parentStage )
}
]
}
Boolean isBuilding( String job, int buildNumber ) {
Jenkins.instance
.getItemByFullName( job )
.getBuildByNumber( buildNumber )
.isInProgress()
}
def withFlowNodes( String name, int buildNumber, Closure body ) {
WorkflowRun run = Jenkins.instance
.getItemByFullName( name )
.getBuildByNumber( buildNumber )
PipelineNodeGraphVisitor visitor = new PipelineNodeGraphVisitor( run )
List<FlowNodeWrapper> flowNodes = visitor.getPipelineNodes()
body( flowNodes )
}
def stageStatus( String keyword ,
String job ,
int buildNumber ,
String type = 'parallel' ,
String parentStage = 'Parallel'
) {
if ( ! isBuilding(job, buildNumber) ) {
println( "pipeline ${job} #${buildNumber} haven't started yet" )
return false
}
withFlowNodes ( job, buildNumber ) { flowNodes ->
List<String> parentIds = flowNodes.findAll {
it.displayName.startsWith(keyword) && it.getType() == FlowNodeWrapper.NodeType.valueOf( type.toUpperCase() )
}.collectMany { it.parents.findAll { p -> p.displayName == parentStage }
.collect { p -> p.id }
}.unique()
return parentIds
? flowNodes.findAll { parentIds.contains( it.id ) }.collect { it.status.getState() }
: false
} // withFlowNodes
}
// call
on( BUILD_NAME, BUILD_NUMBER ).stageStatus( stageName )