{"id":29550,"date":"2015-11-04T16:10:52","date_gmt":"2015-11-04T10:40:52","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=29550"},"modified":"2017-01-29T09:56:40","modified_gmt":"2017-01-29T04:26:40","slug":"jenkins-api-integration-trigger-job-remotely","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/jenkins-api-integration-trigger-job-remotely\/","title":{"rendered":"Jenkins API Integration : Trigger Job Remotely via Jenkins API"},"content":{"rendered":"<p>Jenkins provide continuous integration to software development. Jenkins job builds can be started by various methods, like manual triggering, scheduled cron-like mechanism, dependency builds, and by Jenkins API calls.<\/p>\n<p><strong>This blog will cover Jenkins API Integration : Trigger Job Remotely via Jenkins API.<\/strong><\/p>\n<p>1. \u00a0First, we need Jenkins API TOKEN which will be &#8216;configure&#8217; tab on Jenkins.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-29595\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/11\/d.png\" alt=\"d\" width=\"1309\" height=\"718\" \/><\/p>\n<p>2. \u00a0Generate the URL for Jenkins Job.<\/p>\n<p>Go to Jenkins website and\u00a0add &#8220;<strong>api\/json&#8221;<\/strong>\u00a0to any page.\u00a0If we get a JSON response on the browser, then that page can be accessed remotely. For converting it to readable format, use <strong>&#8220;api\/json?pretty=true&#8221;<\/strong><\/p>\n<p><code><em>jenkins_url\/some_page\/api\/json<\/em><\/code><\/p>\n<p>For example :<br \/>\n<code><\/code><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-29593\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/11\/b.png\" alt=\"b\" width=\"1306\" height=\"154\" \/><\/p>\n<p>3. Make <strong>&#8216;HTTP POST&#8217;<\/strong> request to Jenkins server.<\/p>\n<p>NOTE: Jenkins only supports &#8216;HTTP POST&#8217; request for remote requests.<\/p>\n<p>&#8216;HTTP POST&#8217; request can be of two types.<\/p>\n<p><strong>(A) Without parameters :\u00a0<\/strong><\/p>\n<p>URL FORMAT -&gt;\u00a0<em>jenkins_url\/job\/job_name\/build?token=TOKEN.<\/em><\/p>\n<p>For example :<br \/>\n<code><\/code><\/p>\n<p><strong>(B) Parameterized :<\/strong><\/p>\n<p>URL FORMAT -&gt; jenkins_url\/job\/job_name\/buildWithParameters?token=TOKEN&amp;parameter=VALUE<\/p>\n<p>For Example :<\/p>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nhttp:\/\/jenkins.example.com\/view\/testing\/job\/sample_selenium_test\/buildWithParameters?token=YOUR_TOKEN&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;parameter1=VALUE1&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;parameter2=VALUE2[\/code]<\/p>\n<p>After this, we also need to add <strong>{key:value} pair authorization<\/strong> to the &#8216;HTTP POST&#8217; request. It will be in format<br \/>\n<strong>{auth: &#8220;jenkins_user_email:jenkins_user_password&#8221;}<\/strong>.<\/p>\n<p>Final request will look like..<\/p>\n<p><strong>Curl Request :\u00a0<\/strong><\/p>\n<p><code> curl -v -X POST jenkins_url\/job\/job_name\/buildWithParameters<br \/>\n--data token=your_token<br \/>\n--user jenkins_user_email:jenkins_user_password<br \/>\n--data-urlencode json= '{\"parameter1\":\"value1\", \"parameter2\":\"value2\"}'<br \/>\n<\/code><\/p>\n<p>Finally, we will get the response of &#8216;HTTP POST&#8217; request with property &#8216;statusCode&#8217; (201 for success, else for errors)<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-29592\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/11\/c.png\" alt=\"c\" width=\"1311\" height=\"459\" \/><\/p>\n<p>We have successfully triggered Jenkins job remotely. You may want to check it on Jenkins now.<br \/>\nWe also track the Job Status and Console remotely using Jenkins API which will be covered in my next blog.<br \/>\nStay tuned. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jenkins provide continuous integration to software development. Jenkins job builds can be started by various methods, like manual triggering, scheduled cron-like mechanism, dependency builds, and by Jenkins API calls. This blog will cover Jenkins API Integration : Trigger Job Remotely via Jenkins API. 1. \u00a0First, we need Jenkins API TOKEN which will be &#8216;configure&#8217; tab [&hellip;]<\/p>\n","protected":false},"author":268,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":91},"categories":[1818,1816],"tags":[1682,2725],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/29550"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/268"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=29550"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/29550\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=29550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=29550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=29550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}