{"id":1210,"date":"2023-09-28T00:19:27","date_gmt":"2023-09-27T18:49:27","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=1210"},"modified":"2024-04-02T09:01:55","modified_gmt":"2024-04-02T03:31:55","slug":"parameters-post-and-cron-lab","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=1210","title":{"rendered":"Parameters, post and cron lab"},"content":{"rendered":"<p>Lab1: Create pipeline and build<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Youtube Link:<\/strong><\/span> <span style=\"color: #3366ff;\"><a style=\"color: #3366ff;\" href=\"https:\/\/www.youtube.com\/watch?v=4AFq3L2kbEs&amp;list=PLwO9EbabNLzsaHgP1dUCkdLg2CxZoPXGR&amp;index=12\">Navigation for lab<\/a><\/span><\/p>\n<pre>pipeline {\r\nagent any\r\nparameters {\r\nstring(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')\r\ntext(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')\r\n\r\nbooleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')\r\n\r\nchoice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')\r\n\r\npassword(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')\r\n}\r\nstages {\r\nstage('Example') {\r\nsteps {\r\necho \"Hello ${params.PERSON}\"\r\n\r\necho \"Biography: ${params.BIOGRAPHY}\"\r\n\r\necho \"Toggle: ${params.TOGGLE}\"\r\n\r\necho \"Choice: ${params.CHOICE}\"\r\n\r\necho \"Password: ${params.PASSWORD}\"\r\n}\r\n}\r\n}\r\n}<\/pre>\n<p>Lab2:<\/p>\n<p>&nbsp;<\/p>\n<pre>\/\/ Declarative Pipeline\r\npipeline {\r\n  agent any\r\n triggers {\r\n cron('* * * * *')\r\n}\r\n  stages {\r\n   stage('Example') {\r\n  steps {\r\n     echo 'Hello World'\r\n   }\r\n  }\r\n }\r\n}\r\n\r\n\r\n<\/pre>\n<p>Lab3:<\/p>\n<p>Post actions: The <code>post<\/code>\u00a0section defines one or more additional\u00a0<a href=\"https:\/\/www.jenkins.io\/doc\/book\/pipeline\/syntax\/#declarative-steps\">steps<\/a>\u00a0that are run upon the completion of a Pipeline\u2019s or stage\u2019s run (depending on the location of the\u00a0<code>post<\/code>\u00a0section within the Pipeline).\u00a0<code>post<\/code>\u00a0can support any of the following\u00a0postcondition\u00a0blocks:\u00a0<code>always<\/code>,\u00a0<code>changed<\/code>,\u00a0<code>fixed<\/code>,\u00a0<code>regression<\/code>,\u00a0<code>aborted<\/code>,\u00a0<code>failure<\/code>,\u00a0<code>success<\/code>,\u00a0<code>unstable<\/code>,\u00a0<code>unsuccessful<\/code>, and\u00a0<code>cleanup<\/code><\/p>\n<pre>pipeline {\r\nagent any\r\nstages {\r\nstage('Example') {\r\nsteps {\r\necho 'Hello World'\r\n}\r\n}\r\n}\r\npost {\r\nalways {\r\necho 'I will always say Hello again!'\r\n}\r\n}\r\n}<\/pre>\n<p>Try: replace post&#8211; &gt; always<\/p>\n<p>&nbsp;<\/p>\n<p>post failure example<\/p>\n<p>install gradle on ubnutu machine<\/p>\n<pre>apt-get install gradle<\/pre>\n<pre>pipeline {\r\nagent any\r\nstages {\r\nstage('Compile') {\r\nsteps {\r\ncatchError {\r\nsh 'gradle compileJava --stacktrace'\r\n}\r\n}\r\npost {\r\nsuccess {\r\necho 'Compile stage successful'\r\n}\r\nfailure {\r\necho 'Compile stage failed'\r\n}\r\n}\r\n}\r\n\/* ... other stages ... *\/\r\n}\r\npost {\r\nsuccess {\r\necho 'whole pipeline successful'\r\n}\r\nfailure {\r\necho 'pipeline failed, at least one step failed'\r\n}\r\n}\r\n\r\n}<\/pre>\n<p>post &#8211;&gt;failure<\/p>\n<p>post &#8211;&gt; success\u00a0 and provide echo statement<\/p>\n<p>Try this code and provide observation:<\/p>\n<pre>pipeline {\r\nagent any\r\n\r\n parameters {\r\n  choice(\r\n   name: 'ENVIRONMENT',\r\n   choices: ['dev', 'qa', 'prod'],\r\n   description: 'Select the deployment environment'\r\n )\r\n}\r\nstages {\r\n stage('Build') {\r\n  steps {\r\n\/\/ Add your build steps here\r\n   echo \"Building for ${params.ENVIRONMENT} environment\"\r\n  }\r\n}\r\n\r\n stage('Deploy') {\r\n  when {\r\n   expression {\r\n\/\/ You can use the 'params.ENVIRONMENT' variable to make decisions in your pipeline\r\n   return params.ENVIRONMENT == 'qa' || params.ENVIRONMENT == 'prod'\r\n  }\r\n}\r\nsteps {\r\n\/\/ Add your deployment steps here\r\n  echo \"Deploying to ${params.ENVIRONMENT} environment\"\r\n  }\r\n}\r\nstage('Test') {\r\n  steps {\r\n\/\/ Add your testing steps here\r\n  echo \"Testing in ${params.ENVIRONMENT} environment\"\r\n }\r\n}\r\n}\r\npost {\r\nalways {\r\n\/\/ Add any cleanup or post-build steps here\r\necho \"Pipeline completed for ${params.ENVIRONMENT} environment\"\r\n }\r\n }\r\n}<\/pre>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Lab1: Create pipeline and build Youtube Link: Navigation for lab pipeline { agent any parameters { string(name: &#8216;PERSON&#8217;, defaultValue: &#8216;Mr Jenkins&#8217;, description: &#8216;Who should I say hello to?&#8217;) text(name: &#8216;BIOGRAPHY&#8217;, defaultValue: &#8221;, description: &#8216;Enter some information about the person&#8217;) booleanParam(name: &#8216;TOGGLE&#8217;, defaultValue: true, description: &#8216;Toggle this value&#8217;) choice(name: &#8216;CHOICE&#8217;, choices: [&#8216;One&#8217;, &#8216;Two&#8217;, &#8216;Three&#8217;], description: &#8216;Pick [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-1210","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1210"}],"version-history":[{"count":9,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1210\/revisions"}],"predecessor-version":[{"id":1408,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1210\/revisions\/1408"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}