Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Last active April 3, 2021 08:31
Show Gist options
  • Save t3knoid/cae23d005c721b4abd63181db68d38bb to your computer and use it in GitHub Desktop.
Save t3knoid/cae23d005c721b4abd63181db68d38bb to your computer and use it in GitHub Desktop.
A Groovy script that will store the build number of a Jenkins job's last build into a variable
def address = "http://{jenkinsBase}/job/${jobName}/lastBuild/buildNumber"
try {
println("${address}")
def urlInfo = address.toURL()
response = urlInfo.openConnection()
if (response.getResponseCode() != 200) {
throw new Exception("Unable to connect to " + address) // Throw an exception to get out of loop if response is anything but 200
}
buildNum= response.getInputStream().getText()
println("buildnum=${buildNum}")
}
catch (Exception ex) {
println (ex.getMessage())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment