Skip to content

Instantly share code, notes, and snippets.

@GuillaumeSmaha
Last active March 18, 2022 02:26
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GuillaumeSmaha/fdef2088f7415c60adf95d44073c3c88 to your computer and use it in GitHub Desktop.
Save GuillaumeSmaha/fdef2088f7415c60adf95d44073c3c88 to your computer and use it in GitHub Desktop.
Functions to get stages executed and build state
import hudson.model.Action
import com.cloudbees.workflow.flownode.FlowNodeUtil
import com.cloudbees.workflow.rest.external.StatusExt
import org.jenkinsci.plugins.workflow.graph.FlowNode
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode
import org.jenkinsci.plugins.workflow.actions.LabelAction
@NonCPS
def convertParameters(parameters) {
def parametersConverted = new ArrayList<hudson.model.ParameterValue>()
for (param in parameters) {
def key = param.key.trim()
if (param.value instanceof Boolean) {
parametersConverted.add(new BooleanParameterValue(key.toString(), param.value))
}
else {
parametersConverted.add(new StringParameterValue(key.toString(), param.value.toString()))
}
}
return parametersConverted
}
def __flowNodeHasLabelAction(FlowNode flowNode){
def actions = flowNode.getActions()
for (Action action: actions){
if (action instanceof LabelAction) {
return true
}
}
return false
}
def __getBuildStages(List<FlowNode> flowNodes, data = [startNodes: [], stages: []]) {
def currentFlowNode = null
for (FlowNode flowNode: flowNodes){
currentFlowNode = flowNode
if (flowNode instanceof StepEndNode) {
def startNode = flowNode.getStartNode()
if (__flowNodeHasLabelAction(startNode)) {
data.startNodes.add(0, startNode)
data.stages.add(0, [name: startNode.getDisplayName(), status: FlowNodeUtil.getStatus(flowNode)])
}
}
else if(flowNode instanceof StepStartNode && __flowNodeHasLabelAction(flowNode) && !data.startNodes.contains(flowNode)) {
data.startNodes.add(0, flowNode)
data.stages.add(0, [name: flowNode.getDisplayName(), status: StatusExt.IN_PROGRESS])
}
}
if (currentFlowNode == null) {
return data.stages
}
return __getBuildStages(currentFlowNode.getParents(), data)
}
def getBuildInformations(build){
def rawBuild = build.getRawBuild()
def execution = rawBuild.getExecution()
def executionHeads = execution.getCurrentHeads()
def data = [
status: build.result,
stages: __getBuildStages(executionHeads)
]
return data
}
def getBuildCurrentStage(build){
def data = getBuildInformations(build)
return data.stages.get(data.stages.size() - 1);
}
stage('Stage 1') {
println "Current build"
println getBuildInformations(currentBuild)
println "Current build stage"
println getBuildCurrentStage(currentBuild)
println "Previous build"
println getBuildInformations(currentBuild.getPreviousBuild())
}
stage('Stage 2') {
println "Current build"
println getBuildInformations(currentBuild)
println "Current build stage"
println getBuildCurrentStage(currentBuild)
def buildOtherPipeline = build job: "Other pipeline", parameters: convertParameters(params), propagate: false
println "Current build for buildOtherPipeline"
println getBuildInformations(buildOtherPipeline)
println "Previous build for buildOtherPipeline"
println getBuildInformations(buildOtherPipeline.getPreviousBuild())
}
@YoooFeng
Copy link

Hi, thanks for your sharing. Do you have any idea about getting all stage names before they have been executed? Your codes could get the stage name IN_PROCESSING and executed SUCCESS, but how could i get the stage names they will be executed after in one build? Thank you!

@Moofasax
Copy link

Hey this is great! Question, if a stage fails and causes the build to fail, do you know how to capture the error message say in the post stage?

@haridsv
Copy link

haridsv commented Feb 6, 2019

Please note, you need to disable script sandbox to use this code.

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