Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get build configuration details using Jenkins API

I am looking for a way to get the configuration details of a Jenkins job using Jenkins API. Something which is displayed in the command block in the below image.

enter image description here

Has anybody tried getting configuration details using Jenkins API?

like image 980
tech_human Avatar asked Mar 19 '23 09:03

tech_human


2 Answers

You can get the raw XML configuration of a job from the URL: http://jenkins:8080/job/my-job/config.xml

This URL returns the persistent job configuration in XML. The build steps are listed under the builders element, different types of build steps are identified by different elements:

<builders>
  <hudson.tasks.Shell>
    <command>
    # Run my shell command...
    </command>
  </hudson.tasks.Shell>
</builders>
like image 127
Dave Bacher Avatar answered Mar 29 '23 02:03

Dave Bacher


There's no direct way of doing this that I know of, however you can collect the shell execution with the console output API and a little regex magic.

The API endpoint looks like this:

"http://#{server}:#{port}/job/#{job_name}/{build_numer}/logText/progressiveText?start=0"

For this example, let's say your shell command looks like:

bundle install
bundle exec rspec spec/

The console puts a + before every execution command, so the following script would work:

# using rest-client gem for ease of use 
# but you could use net:http and open/uri in the standard library
require 'rest-client'

console_output = RestClient.get 'http://jenkins_server:80/job/my_job/100/logtext/progressiveText?start=0'

console_output.scan(/^\+.+/).each_with_object([]) { |match, array| array << match.gsub('+ ', '') }
#=> ["bundle install", "bundle exec rspec spec/"]
like image 45
Johnson Avatar answered Mar 29 '23 02:03

Johnson