0

I try to select all current running processes to get the BuildNumber an JobName of the running pipelines to finaly have the possibility to kill them. However, if I use the Jenkins API to collect the BuildNumber and JobName I will get as return:

[(master)]:
[org.jenkinsci.plugins.workflow.job.WorkflowJob@6b41187c[myPipeline], org.jenkinsci.plugins.workflow.job.WorkflowJob@6296243a[myDeploy]]

I assumed that "@6296243a[myDeploy]" is the information I need. But the "6296243a" is not a valid BuildNumber. I really do not know what this number stands for. Maybe the process id. The "myDeploy" is the JobName I need to have.

So my question why I will not get a valid "BuildNumber" here?

import jenkins.model.*
import hudson.model.*
  
def result = []
  
  
def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll { 
  it.isBuilding()
}

println buildingJobs

Finaly I want to use the both information at:

Jenkins.instance
 .getItemByFullName(<JobName>)
 .getBuildByNumber(<BuldNumber>)
 .finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));

if I use here the values BuildNumber: 6296243a JobName: myDeploy it will not work. If I use the BuildNumber from the view (2357) it works well.

So anybody has a hint how to receive the real BuildNumber?

IFThenElse
  • 131
  • 1
  • 10

1 Answers1

0

From the example code, you provided, buildingJobs will have a List of WorkflowJob objects. Hence you are getting a reference to the objects when you print it.

Here is how you can get the Build IDs and Job names of currently running and paused builds.

Jenkins.instance.getAllItems(Job.class).findAll{it.isBuilding()}.each { job ->
      job.getBuilds().each { b ->        
        if(b.isInProgress()) {
            println "Build: " + b.getNumber() + " of Job: " +  job.getName() + " is Running. Hence Killing!!!"
        } 
   }  
}
ycr
  • 12,828
  • 2
  • 25
  • 45