LOGIN
Sign In or Register
Avatar
Not Registered Yet?

Join Now! It's FREE. Get full access and benefit from this site

Reset My password - Remind Me My username

Username
Password
Remember me

Jenkins Remote access API Example

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
https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
 
Rate this item
(1 Vote)
Rajesh Kumar

.

Total, Over 12 years of extensive experience in the Software Configuration Management domain having depth knowledge of DevOps, Continuous Integration and Delivery, Configuration Management, Build and Installer, Release Management and Application Management.