blob: 25395e67a33acfad5a87c9cfeea0966389bf3ab1 [file] [log] [blame]
Jakub Josef4edd7432017-05-10 17:58:56 +02001package com.mirantis.mk
Jakub Josef93f08e22017-06-05 19:14:53 +02002import com.cloudbees.groovy.cps.NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +02003
4/**
5 *
6 * Jenkins common functions
7 *
8 */
9
10/**
11 * Get Jenkins job running builds
12 * @param jobName job name
13 * @return list of running builds
14 */
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020015@NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +020016def getJobRunningBuilds(jobName){
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020017 def job = Jenkins.instance.items.find{it -> it.name.equals(jobName)}
18 if(job){
19 return job.builds.findAll{build -> build.isBuilding()}
20 }
21 return []
Jakub Josef93f08e22017-06-05 19:14:53 +020022}
23
24@NonCPS
25def getRunningBuilds(job){
26 return job.builds.findAll{build -> build.isBuilding()}
27}
28
29@NonCPS
30def killStuckBuilds(maxSeconds, job){
31 def common = new com.mirantis.mk.Common()
32 def result = true
33 def runningBuilds = getRunningBuilds(job)
34 def jobName = job.name
35 for(int j=0; j < runningBuilds.size(); j++){
36 int durationInSeconds = (System.currentTimeMillis() - runningBuilds[j].getTimeInMillis())/1000.0
37 if(durationInSeconds > maxSeconds){
38 result = false
39 def buildId = runningBuilds[j].id
40 common.infoMsg("Aborting ${jobName}-${buildId} which is running for ${durationInSeconds}s")
41 try{
42 runningBuilds[j].finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer"));
43 result = true
44 }catch(e){
45 common.errorMsg("Error occured during aborting build: Exception: ${e}")
46 }
47 }
48 }
49 return result
Jakub Josef4edd7432017-05-10 17:58:56 +020050}