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.
Has anybody tried getting configuration details using Jenkins API?
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>
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/"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With