4

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?

alex
  • 6,818
  • 9
  • 52
  • 103
tech_human
  • 6,592
  • 16
  • 65
  • 107

2 Answers2

7

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>
Dave Bacher
  • 15,652
  • 3
  • 63
  • 86
1

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/"]
Johnson
  • 1,510
  • 8
  • 15