{"id":528,"date":"2017-02-24T12:14:09","date_gmt":"2017-02-24T12:14:09","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2017\/02\/24\/jenkins-remote-access-api-example\/"},"modified":"2021-11-19T06:09:34","modified_gmt":"2021-11-19T06:09:34","slug":"jenkins-remote-access-api-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/jenkins-remote-access-api-example\/","title":{"rendered":"Jenkins Remote access API Example | Jenkins Tutorial"},"content":{"rendered":"<div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3002 size-full\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2017\/02\/jenkins-remote-access-api-e.png\" alt=\"jenkins-remote-access-api-example\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2017\/02\/jenkins-remote-access-api-e.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2017\/02\/jenkins-remote-access-api-e-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/div>\n<div><\/div>\n<div><strong>Jenkins Remote access API Example<\/strong><\/div>\n<div><\/div>\n<div><strong>Jenkins provides machine-consumable remote access API to its functionalities. Currently it comes in three flavors:<\/strong><\/div>\n<div><\/div>\n<div>XML<\/div>\n<div>JSON with JSONP support<\/div>\n<div>Python<\/div>\n<div><\/div>\n<div>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 &#8220;&#8230;\/api\/&#8221; URL where &#8220;&#8230;&#8221; portion is the data that it acts on.<\/div>\n<div><\/div>\n<div>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 \u2013 primarily a listing of the configured jobs for this Jenkins instance.<\/div>\n<div><\/div>\n<div>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&#8217;ll see the list of functionalities for that build.<\/div>\n<div><\/div>\n<div><strong>Remote API can be used to do things like these:<\/strong><\/div>\n<div><\/div>\n<div>Retrieve information from Jenkins for programmatic consumption.<\/div>\n<div>trigger a new build<\/div>\n<div>create\/copy jobs<\/div>\n<div><\/div>\n<div><\/div>\n<div>Jobs with parameters, Also see Parameterized Build.<\/div>\n<div>Simple example &#8211; sending &#8220;String Parameters&#8221;:<\/div>\n<div>curl -X POST JENKINS_URL\/job\/JOB_NAME\/build \\<\/div>\n<div>&nbsp; &#8211;data token=TOKEN \\<\/div>\n<div>&nbsp; &#8211;data-urlencode json='{&#8220;parameter&#8221;: [{&#8220;name&#8221;:&#8221;id&#8221;, &#8220;value&#8221;:&#8221;123&#8243;}, {&#8220;name&#8221;:&#8221;verbosity&#8221;, &#8220;value&#8221;:&#8221;high&#8221;}]}&#8217;<\/div>\n<div><\/div>\n<div><strong>Check Jenkins Job Status via REST API<\/strong><\/div>\n<div><\/div>\n<div>job_status=`curl https:\/\/jenkins\/view\/job\/other-job\/lastBuild\/api\/json | grep &#8220;\\&#8221;result\\&#8221;:\\&#8221;SUCCESS\\&#8221;&#8221;`<\/div>\n<pre>if [ -n \"$job_status\" ] then &nbsp; &nbsp; # Run your script commands here else &nbsp; echo \"BUILD FAILURE: Other build is unsuccessful or status could not be obtained.\" &nbsp; exit 1 fi<\/pre>\n<div><\/div>\n<div><\/div>\n<div><strong>How to restart Jenkins manually?<\/strong><\/div>\n<div><\/div>\n<div>To restart Jenkins manually, you can use either of the following commands:<\/div>\n<div>(jenkins_url)\/safeRestart &#8211; Allows all running jobs to complete. New jobs will remain in the queue to run after the restart is complete.<\/div>\n<div><\/div>\n<div>(jenkins_url)\/restart &#8211; Forces a restart without waiting for builds to complete.<\/div>\n<div><\/div>\n<div><strong>Reference<\/strong><\/div>\n<div><a href=\"https:\/\/wiki.jenkins-ci.org\/display\/JENKINS\/Remote+access+API\" target=\"_blank\" rel=\"noopener\">https:\/\/wiki.jenkins-ci.org\/display\/JENKINS\/Remote+access+API<\/a><\/div>\n<div><\/div>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_87888\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_79515\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_87888\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>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 &#8220;&#8230;\/api\/&#8221; URL&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3002,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[42],"tags":[380,1056,519,145,822,383,1057,1058],"class_list":["post-528","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jenkins","tag-access","tag-api","tag-example","tag-jenkins","tag-jenkins-tutorial","tag-remote","tag-remote-access-api","tag-remote-api"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=528"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/528\/revisions"}],"predecessor-version":[{"id":25773,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/528\/revisions\/25773"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3002"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}