Jenkins Remote access API Example

Jenkins provides machine-consumable remote access API to its functionalities. Currently it comes in three flavors:

XML

JSON with JSONP support

Python

Remote access API is offered in a REST-like style. That is, there is no single entry point for all features, and instead they are available under the “…/api/” URL where “…” portion is the data that it acts on.

For example, if your Jenkins installation sits at http://ci.jruby.org/, visiting http://ci.jruby.org/api/ will show just the top-level API features available – primarily a listing of the configured jobs for this Jenkins instance.

Or if you want to access information about a particular build, e.g. http://ci.jruby.org/job/jruby-base/lastSuccessfulBuild/, then go to http://ci.jruby.org/job/jruby-base/lastSuccessfulBuild/api/ and you’ll see the list of functionalities for that build.

Remote API can be used to do things like these:

Retrieve information from Jenkins for programmatic consumption.

trigger a new build

create/copy jobs

Jobs with parameters, Also see Parameterized Build.

Simple example – sending “String Parameters”:

curl -X POST JENKINS_URL/job/JOB_NAME/build \

–data token=TOKEN \

–data-urlencode json='{“parameter”: [{“name”:”id”, “value”:”123″}, {“name”:”verbosity”, “value”:”high”}]}’

Check Jenkins Job Status via REST API

job_status=`curl https://jenkins/view/job/other-job/lastBuild/api/json | grep “\”result\”:\”SUCCESS\””`

if [ -n "$job_status" ] then # Run your script commands here else echo "BUILD FAILURE: Other build is unsuccessful or status could not be obtained." exit 1 fi

How to restart Jenkins manually?

To restart Jenkins manually, you can use either of the following commands:

(jenkins_url)/safeRestart – Allows all running jobs to complete. New jobs will remain in the queue to run after the restart is complete.

(jenkins_url)/restart – Forces a restart without waiting for builds to complete.

Reference