0

I am trying to obtain the last build number (for a specific job) from jenkins from a remote script.

I have thus:

<jenkins url>/api/xml?tree=jobs[name,lastBuild[number]]

Producing

<hudson>

<job>
<name>mfg-tools-build-win32</name>
<lastBuild>
<number>220</number>
</lastBuild>
</job>
...
<job>
<name>client-sign</name>
<lastBuild>
<number>103</number>
</lastBuild>
</job>

...

I wan to get the number ("103") of the job where the name is "client-sign".

I tried to restrict the job entries returned:

<jenkins url>/api/xml?tree=jobs[name[contains(.,'luna-dev-sa7-evcs-client-sign')],lastBuild[number]]

But that changed nothing in the output.

Tried using some xpath I found on the hudson doc site:

<jenkins url>/api/xml?xpath=/hudson/job/name/text()[contains(.,%27client-sign%27)]

But that wound up giving me an error indicating two nodes matched (there is one tagged as 'client-sign-copy').

What xpath do I need to pluck the last build #?

Update:

Per request: https://wiki.eclipse.org/Hudson-ci/help/remote_access_api#XPath_Selection

Update 2:

I might not be able to do this. I got further via working with the previous answer (that is now deleted):

<jenkins url>/api/xml?depth=1&xpath=//job[name = "client-sign"]/lastBuild/number/text()

But, that now throws:

primitive XPath result sets forbidden; implement jenkins.security.SecureRequester

Unless that is also wrong, I guess I'm back to screen-scraping. >:-[

Update 3 - Success (but not with XPath)

From this answer, I was able to come up with

<jenkins url>/job/client-sign/lastBuild/buildNumber

And that gives me 103, just as needed. :)

Community
  • 1
  • 1
Jon
  • 1,675
  • 26
  • 57

2 Answers2

0

Too bad alecxe's post was deleted -- he was probably on the right track and we could have tweaked his answer to work.

Try adapting his XPath to the URL you already tried:

http://172.20.18.219:8080/api/xml?xpath=/hudson/job[name=%27client-sign%27]/lastBuild/number/text()
LarsH
  • 27,481
  • 8
  • 94
  • 152
  • See my update 2.. we both came to the same realization, but, it might be that this has been disabled in jenkins, unless there is something else I missed. – Jon Aug 26 '16 at 19:05
0

According to JENKINS-16877, as of Jenkins 1.502, xpath text() (and ".", as in //foo[contains(.,'bar')]) was disabled for security reasons.

I've also found that trying to use [0] where more than one result is returned (to select the first result) doesn't work as expected either.

This means anything other than simple xpath queries cannot be done.

The advice given in the above link is to return the full xml document and query locally with a tool like xmlstarlet or xmllint.

mike
  • 1