Created
October 9, 2020 12:10
-
-
Save pere3/f1faf4b938d57da3a27249534b93bef9 to your computer and use it in GitHub Desktop.
Groovy script that find long running Jenkins jobs, and POST hook to saltstack reactor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonOutput | |
def runningBuilds = Jenkins.instance.getView('All').getBuilds().findAll() { it.isInProgress() } | |
LinkedHashMap notificationPayload = [:] | |
runningBuilds.each{ value -> | |
String jobName = value.getFullDisplayName() | |
int durationInSeconds = (System.currentTimeMillis() - value.getStartTimeInMillis())/1000 | |
if (durationInSeconds > 3600) { | |
println("Job: ${jobName}, running for more than 1 hour long") | |
println("Running time: " + durationInSeconds) | |
notificationPayload.put(jobName, durationInSeconds + " sec") | |
} | |
} | |
if (notificationPayload) { | |
URL url = new URL ("http://saltstack.local/hook/jenkins/job/buildtime/msg") | |
HttpURLConnection con = (HttpURLConnection)url.openConnection() | |
con.setRequestMethod("POST") | |
con.setRequestProperty("Content-Type", "application/json; utf-8"); | |
con.setRequestProperty("Accept", "application/json"); | |
con.setDoOutput(true); | |
String jsonInputString = JsonOutput.toJson(notificationPayload) | |
OutputStream os = con.getOutputStream(); | |
os.write(JsonOutput.toJson(notificationPayload).getBytes("UTF-8")); | |
os.close(); | |
InputStream input = new BufferedInputStream(con.getInputStream()); | |
String result = org.apache.commons.io.IOUtils.toString(input, "UTF-8"); | |
input.close(); | |
con.disconnect(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment