Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created February 13, 2018 20:52
Show Gist options
  • Save t3knoid/20480c722d2ef3721dfdb71f5f3e3781 to your computer and use it in GitHub Desktop.
Save t3knoid/20480c722d2ef3721dfdb71f5f3e3781 to your computer and use it in GitHub Desktop.
Jenkins groovy script to stop a running job
// Post to stop a running Jenkins job
build = 63
jobName = // Set job name here
jlc=JenkinsLocationConfiguration.get() // Get Jenkins base URL from configuration
jenkinsBase = jlc.getUrl()
addr = "${jenkinsBase}job/${jobName}/${build}/stop" // Stop URL
authString = "frefol:d0882d0d535f52cc87f3ac3694d7ad61".getBytes().encodeBase64().toString() // Use authentication token
println("${addr}")
try {
def connection = addr.toURL().openConnection()
connection.setRequestProperty( "Authorization", "Basic ${authString}" )
connection.setRequestMethod("POST") // Use post to load URL
if( connection.responseCode == 200 ) {
println("Build stopped")
} else {
println "Something bad happened. "
println "${connection.responseCode}: ${connection.responseMessage}"
}
}
catch (Exception ex) {
println (ex.getMessage())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment