Skip to content

Instantly share code, notes, and snippets.

@or-shachar
Last active July 30, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save or-shachar/d539f1c6fcb0df250222cb86a5b2a339 to your computer and use it in GitHub Desktop.
Save or-shachar/d539f1c6fcb0df250222cb86a5b2a339 to your computer and use it in GitHub Desktop.
How to recursively find the userId that triggered the build in Jenkins
import net.sf.json.JSONException
node {
// Just example of how to use the function
currentBuild.description = find_trigger_user(currentBuild)
}
def find_trigger_user(build){
cause = build.buildCauses.getJSONObject(0)
try{
return cause.getString("userId")
}catch (JSONException e){
println("Not directly triggerred. Checking upstream")
}
possibleUser = null
build.upstreamBuilds.each{
println(build.buildCauses.toString())
possibleUser = find_trigger_user(it)
if (possibleUser != null)
return
}
return possibleUser
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment