0

I just found that Jenkins jobs via a JSON URL each job has using this format: https://jenkinsurl.net/job/_test/lastBuild/api/json provides info like below.

{"_class":"hudson.model.FreeStyleBuild","actions":[{"_class":"hudson.model.ParametersAction","parameters":[{"_class":"hudson.model.StringParameterValue","name":"build_num","value":""}]},{"_class":"hudson.model.CauseAction","causes":[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user anonymous","userId":"anonymous","userName":"anonymous"}]},{},{},{},{},{},{"_class":"org.jenkinsci.plugins.buildenvironment.actions.BuildEnvironmentBuildAction"},{}],"artifacts":[],"building":false,"description":null,"displayName":"#1","duration":1850,"estimatedDuration":1850,"executor":null,"fullDisplayName":"_test #1","id":"1","keepLog":false,"number":1,"queueId":5753,"result":"SUCCESS","timestamp":1479700053274,"url":"http://jenkinsserver.net/job/_test/1/","builtOn":"node_name","changeSet":{"_class":"hudson.scm.EmptyChangeLogSet","items":[],"kind":null},"culprits":[]}

I'd like to know how I can query those fields with a Groovy script to help me build Dynamic parameters when selecting "Build with Parameters". I already have the Groovy and Dynamic parameter plugins and use them for some simple queries I've been using to generate some parameters, like timestamps not overwritten by Jenkins jobs downstream.

How do I query for these and return their values? For example, if I wanted to return the value of the user that started the build from this segment:

[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user anonymous","userId":"anonymous","userName":"anonymous"}]

This isn't a question about getting the person that triggered the build. Sorry if that's not such a great example, but this one appears to be a bit nested, so presumably a good one to learn with.

I know how to reference the value of parameters and get some info to return to generate them, but not how to use that URL and extract specific info from it to help create a parameter's value before the build begins. Is this possible? If not, what other mechanism could I use?

kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80

1 Answers1

0

You can do something similar to this one: How do I discover the additional causes of my Jenkins build?

job = hudson.model.Hudson.instance.getItem("demo-job")
build = job.getLastBuild()

// get action first
def action = build.getAction(hudson.model.CauseAction.class)    
cause = action.findCause(hudson.model.Cause.UserIdCause)
println cause.userId

http://javadoc.jenkins-ci.org/hudson/model/Cause.UserIdCause.html

The question is abit confusing so not sure if this helps at all. Anyway, you should be able to extract all the information you need from build returned by getLastBuild().

It not possible to extra parameters before the build begins. If that is what you mean.

Community
  • 1
  • 1
MaTePe
  • 936
  • 1
  • 6
  • 11