-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkinsExtractor.groovy
71 lines (69 loc) · 3.46 KB
/
jenkinsExtractor.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import net.praqma.quticon.BuildDataEntry
import org.jenkinsci.plugins.workflow.job.WorkflowRun
// This is dependency to https://github.com/jenkinsci/pipeline-stage-view-plugin
// There is no good reason to extract pipeline stages from WorkflowRun on our own
import com.cloudbees.workflow.rest.external.RunExt
// This method takes a list of job names and a time interval in hours
// and returns list of BuildDataEntry
// NonCPS added to avoid java.io.NotSerializableException's
// Read more here https://github.com/jenkinsci/workflow-cps-plugin#technical-design
@NonCPS
def call(def jobNames, def numberOfHoursBack) {
def buildResults = []
if (jobNames==[]) {
echo "No job names specified for extraction. Check all jobs"
jobNames = Jenkins.instance.getJobNames()
}
for (def jobName: jobNames) {
echo "Looking for the job with the name $jobName"
def job = Jenkins.instance.getItemByFullName(jobName)
if (job == null) {
echo "Job ${jobName} wasn't found. Check your configuration"
continue
}
def builds = job.getBuilds().byTimestamp(System.currentTimeMillis()-numberOfHoursBack*60*60*1000, System.currentTimeMillis()).completedOnly()
echo "Found ${builds.size()} builds matching time criteria for the job ${jobName}"
for (def build: builds) {
// Make sure we remove / from job name since it will be part of URL and might confuse ELK
sanitizedJobName = jobName.replaceAll("/", ".")
// If this is a pipeline then extract pipeline stages as separate entries in addition to the pipeline run itself that will
// be extracted in the next step
if (build instanceof WorkflowRun) {
for (flowNode in RunExt.create(build).getStages()) {
// SUCCESS and FAILURE Verdicts need to be in numeric values
def buildVerdict = getBuildVerdictInteger(flowNode.getStatus().toString())
echo "${buildVerdict} succeeded."
def stage_entry = new BuildDataEntry(job_name: "${sanitizedJobName}.${flowNode.getName().replaceAll("[^a-zA-Z0-9]", "_")}",
verdict: flowNode.getStatus(),
build_number: buildVerdict,
duration: flowNode.getDurationMillis(),
timestamp: flowNode.getStartTimeMillis(),
time_in_queue: 0) // Fixme!
echo "New pipeline stage entry: name ${stage_entry.job_name}, result ${stage_entry.verdict}, number ${stage_entry.build_number}, duration ${stage_entry.duration}, timestamp ${stage_entry.timestamp}, time in queue ${stage_entry.time_in_queue}"
buildResults.add(stage_entry)
}
}
def entry = new BuildDataEntry(job_name: sanitizedJobName,
verdict: build.result,
build_number: build.number,
duration: build.duration,
timestamp: build.getTimeInMillis(),
time_in_queue: build.getStartTimeInMillis() - build.getTimeInMillis())
echo "New entry: name ${entry.job_name}, result ${entry.verdict}, number ${entry.build_number}, duration ${entry.duration}, timestamp ${entry.timestamp}, time in queue ${entry.time_in_queue}"
buildResults.add(entry)
}
}
return buildResults
}
def getBuildVerdictInteger(def verdictString){
echo "${verdictString} into Integer"
def returnInteger = ""
if (verdictString.equalsIgnoreCase("SUCCESS")) {
returnInteger = "0"
}
else if (verdictString.equalsIgnoreCase("FAILURE")) {
returnInteger = "1"
}
echo "Returning ${returnInteger}"
return returnInteger
}