{"id":1660,"date":"2020-03-13T17:45:51","date_gmt":"2020-03-13T14:45:51","guid":{"rendered":"https:\/\/artem.services\/?p=1656"},"modified":"2020-03-13T17:51:24","modified_gmt":"2020-03-13T14:51:24","slug":"1660","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1660&lang=en","title":{"rendered":"Jenkins &#8212; Check for the existence of a variable"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-819\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo.png\" alt=\"\" width=\"1280\" height=\"412\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo.png 1280w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-300x97.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-768x247.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-1024x330.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-954x307.png 954w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>If a variable is used in <strong>Jenkins Pipeline<\/strong> that is created on the basis of a <strong>Webhook<\/strong> or etc, then a task will fail with a manual start. To avoid this, you can add a check for the existence of a variable and set its value.<\/p>\n<pre class=\"brush: groovy; title: ; notranslate\" title=\"\">\r\npipeline {\r\n  agent any\r\n    stage('Check ENV') {\r\n      when { expression { env.GIT_COMMIT_ID == null } }\r\n      steps {\r\n        script {\r\n          echo &quot;COMMIT ID IS NOT SET&quot;\r\n          env.GIT_COMMIT_ID = sh(script: 'git log --format=&quot;%H&quot; -n 1', returnStdout: true).trim()\r\n        }\r\n      }\r\n    }\r\n  }\r\n}  \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In this example, the presence of the variable &quot;<strong>GIT_COMMIT_ID<\/strong>&quot; is checked, and if it is absent, a script is executed that sets this variable with the value of the last <strong>HASH<\/strong> commit for this branch. This check should be performed only after the &quot;<strong>checkout<\/strong>&quot; stage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; If a variable is used in Jenkins Pipeline that is created on the basis of a Webhook or etc, then a task will fail with a manual start. To avoid this, you can add a check for the existence of a variable and set its value. &nbsp; In this example, the presence of the &hellip; <a href=\"https:\/\/artem.services\/?p=1660&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;Jenkins &#8212; Check for the existence of a variable&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[613],"tags":[615,1319,617,641],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1660"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1660"}],"version-history":[{"count":3,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1660\/revisions"}],"predecessor-version":[{"id":1663,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1660\/revisions\/1663"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}