{"id":2050,"date":"2017-12-07T09:22:48","date_gmt":"2017-12-07T09:22:48","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2050"},"modified":"2021-02-27T09:29:25","modified_gmt":"2021-02-27T09:29:25","slug":"script-to-check-the-build-status-of-a-jenkins-job","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/","title":{"rendered":"Script to check the build status of a Jenkins job"},"content":{"rendered":"<p><strong>Using JSON &amp; Curl<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">BUILD_STATUS=$(curl --user USER:TOKEN_VALUE --silent $BUILD_URLapi\/json | jq -r '.result')\r\necho $BUILD_STATUS<\/pre>\n<p><strong>Using jenkins Plugins<\/strong><br \/>\nPlugins Name &#8211; Conditional Build Step<\/p>\n<p>For Implementations, Please check here.<br \/>\nhttps:\/\/stackoverflow.com\/questions\/11125598\/getting-the-build-status-in-post-build-script<\/p>\n<p><strong>Using Python &amp; Json<\/strong><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&lt;\/pre&gt;\r\n#!\/usr\/bin\/python\r\n#\r\n# author: ajs\r\n# license: bsd\r\n# copyright: re2\r\n\r\nimport json\r\nimport sys\r\nimport urllib\r\nimport urllib2\r\n\r\njenkinsUrl = &quot;https:\/\/jenkins.example.com\/job\/&quot;\r\n\r\nif len( sys.argv ) &gt; 1 :\r\njobName = sys.argv&#x5B;1]\r\njobNameURL = urllib.quote(jobName)\r\nelse :\r\nsys.exit(1)\r\n\r\ntry:\r\njenkinsStream = urllib2.urlopen( jenkinsUrl + jobNameURL + &quot;\/lastBuild\/api\/json&quot; )\r\nexcept urllib2.HTTPError, e:\r\nprint &quot;URL Error: &quot; + str(e.code)\r\nprint &quot; (job name &#x5B;&quot; + jobName + &quot;] probably wrong)&quot;\r\nsys.exit(2)\r\n\r\ntry:\r\nbuildStatusJson = json.load( jenkinsStream )\r\nexcept:\r\nprint &quot;Failed to parse json&quot;\r\nsys.exit(3)\r\n\r\nif buildStatusJson.has_key( &quot;result&quot; ):\r\nprint &quot;&#x5B;&quot; + jobName + &quot;] build status: &quot; + buildStatusJson&#x5B;&quot;result&quot;]\r\nif buildStatusJson&#x5B;&quot;result&quot;] != &quot;SUCCESS&quot; :\r\nexit(4)\r\nelse:\r\nsys.exit(5)\r\n\r\nsys.exit(0)\r\n\r\nReference -\r\n&lt;a href=&quot;http:\/\/serverfault.com\/questions\/309848\/how-can...49988bb53ee820fe202a&quot;&gt;serverfault.com\/questions\/309848\/how-can...49988bb53ee820fe202a&lt;\/a&gt;\r\n&lt;pre&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using JSON &amp; Curl BUILD_STATUS=$(curl &#8211;user USER:TOKEN_VALUE &#8211;silent $BUILD_URLapi\/json | jq -r &#8216;.result&#8217;) echo $BUILD_STATUS Using jenkins Plugins Plugins Name [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[428],"tags":[145],"class_list":["post-2050","post","type-post","status-publish","format-standard","hentry","category-jenkins-build-management-tools","tag-jenkins"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Script to check the build status of a Jenkins job - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Script to check the build status of a Jenkins job - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"Using JSON &amp; Curl BUILD_STATUS=$(curl --user USER:TOKEN_VALUE --silent $BUILD_URLapi\/json | jq -r &#039;.result&#039;) echo $BUILD_STATUS Using jenkins Plugins Plugins Name [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-07T09:22:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-27T09:29:25+00:00\" \/>\n<meta name=\"author\" content=\"scmgalaxy K\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"scmgalaxy K\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/\"},\"author\":{\"name\":\"scmgalaxy K\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/fc12ad190f540e79460243c81148d102\"},\"headline\":\"Script to check the build status of a Jenkins job\",\"datePublished\":\"2017-12-07T09:22:48+00:00\",\"dateModified\":\"2021-02-27T09:29:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/\"},\"wordCount\":215,\"commentCount\":0,\"keywords\":[\"Jenkins\"],\"articleSection\":[\"Jenkins\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/\",\"name\":\"Script to check the build status of a Jenkins job - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2017-12-07T09:22:48+00:00\",\"dateModified\":\"2021-02-27T09:29:25+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/fc12ad190f540e79460243c81148d102\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/script-to-check-the-build-status-of-a-jenkins-job\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Script to check the build status of a Jenkins job\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/fc12ad190f540e79460243c81148d102\",\"name\":\"scmgalaxy K\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b902ea8c77ee0a326c112f29d0edf51b7c3e1cd05c4fb92a810177e9c3f12a9?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b902ea8c77ee0a326c112f29d0edf51b7c3e1cd05c4fb92a810177e9c3f12a9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b902ea8c77ee0a326c112f29d0edf51b7c3e1cd05c4fb92a810177e9c3f12a9?s=96&d=mm&r=g\",\"caption\":\"scmgalaxy K\"},\"sameAs\":[\"http:\\\/\\\/www.DevOpsSchool.com\"],\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/scmgalaxy\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Script to check the build status of a Jenkins job - scmGalaxy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/","og_locale":"en_US","og_type":"article","og_title":"Script to check the build status of a Jenkins job - scmGalaxy","og_description":"Using JSON &amp; Curl BUILD_STATUS=$(curl --user USER:TOKEN_VALUE --silent $BUILD_URLapi\/json | jq -r '.result') echo $BUILD_STATUS Using jenkins Plugins Plugins Name [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/","og_site_name":"scmGalaxy","article_published_time":"2017-12-07T09:22:48+00:00","article_modified_time":"2021-02-27T09:29:25+00:00","author":"scmgalaxy K","twitter_card":"summary_large_image","twitter_misc":{"Written by":"scmgalaxy K","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/"},"author":{"name":"scmgalaxy K","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/fc12ad190f540e79460243c81148d102"},"headline":"Script to check the build status of a Jenkins job","datePublished":"2017-12-07T09:22:48+00:00","dateModified":"2021-02-27T09:29:25+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/"},"wordCount":215,"commentCount":0,"keywords":["Jenkins"],"articleSection":["Jenkins"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/","name":"Script to check the build status of a Jenkins job - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2017-12-07T09:22:48+00:00","dateModified":"2021-02-27T09:29:25+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/fc12ad190f540e79460243c81148d102"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/script-to-check-the-build-status-of-a-jenkins-job\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Script to check the build status of a Jenkins job"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/fc12ad190f540e79460243c81148d102","name":"scmgalaxy K","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b902ea8c77ee0a326c112f29d0edf51b7c3e1cd05c4fb92a810177e9c3f12a9?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b902ea8c77ee0a326c112f29d0edf51b7c3e1cd05c4fb92a810177e9c3f12a9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b902ea8c77ee0a326c112f29d0edf51b7c3e1cd05c4fb92a810177e9c3f12a9?s=96&d=mm&r=g","caption":"scmgalaxy K"},"sameAs":["http:\/\/www.DevOpsSchool.com"],"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/scmgalaxy\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/2050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=2050"}],"version-history":[{"count":4,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/2050\/revisions"}],"predecessor-version":[{"id":4950,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/2050\/revisions\/4950"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=2050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=2050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=2050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}