{"id":1200,"date":"2023-09-27T22:22:30","date_gmt":"2023-09-27T16:52:30","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=1200"},"modified":"2024-04-02T09:00:46","modified_gmt":"2024-04-02T03:30:46","slug":"scripted-and-declarative-pipeline-for-text-artifacts","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=1200","title":{"rendered":"Scripted and declarative pipeline  for text artifacts"},"content":{"rendered":"<p>Lab1:<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Youtube Link<\/strong><\/span> : <span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/www.youtube.com\/watch?v=JaOMunYORbs&amp;list=PLwO9EbabNLzsaHgP1dUCkdLg2CxZoPXGR&amp;index=11\">Navigation for lab<\/a><\/span><\/p>\n<ol>\n<li><strong>Node Block<\/strong>: The <code>node<\/code> block specifies that the pipeline should be executed on a Jenkins agent (node). It defines the environment where the pipeline stages will run.<\/li>\n<li><strong>Stage: Create build output<\/strong>:\n<ul>\n<li><code>sh \"mkdir -p output\"<\/code>: This shell command creates a directory named &#8220;output&#8221; if it doesn&#8217;t already exist. The <code>-p<\/code> option ensures that the command doesn&#8217;t throw an error if the directory already exists.<\/li>\n<li><code>writeFile file: \"output\/usefulfile.txt\", text: \"This file is useful, need to archive it.\"<\/code>: This step writes some content into a file named &#8220;usefulfile.txt&#8221; in the &#8220;output&#8221; directory.<\/li>\n<li><code>writeFile file: \"output\/uselessfile.md\", text: \"This file is useless, no need to archive it.\"<\/code>: Similarly, this step writes content into a file named &#8220;uselessfile.md&#8221; in the &#8220;output&#8221; directory.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Stage: Archive build output<\/strong>:\n<ul>\n<li><code>archiveArtifacts artifacts: 'output\/*.txt', excludes: 'output\/*.md'<\/code>: In this stage, you archive the build output artifacts. Specifically:\n<ul>\n<li><code>artifacts: 'output\/*.txt'<\/code>: This specifies that you want to archive all files with the &#8220;.txt&#8221; extension in the &#8220;output&#8221; directory.<\/li>\n<li><code>excludes: 'output\/*.md'<\/code>: This specifies that you want to exclude all files with the &#8220;.md&#8221; extension from archiving.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre>node {\r\nstage('Create build output') {\r\n\/\/ Make the output directory.\r\n   sh \"mkdir -p output\"\r\n\r\n\/\/ Write a useful file, which is needed to be archived.\r\n   writeFile file: \"output\/usefulfile.txt\", text: \"This file is useful, need to archive it.\"\r\n\r\n\/\/ Write a useless file, which is not needed to be archived.\r\n   writeFile file: \"output\/uselessfile.md\", text: \"This file is useless, no need to archive it.\"\r\n}\r\n\r\nstage('Archive build output') {\r\n\/\/ Archive the build output artifacts.\r\n    archiveArtifacts artifacts: 'output\/*.txt', excludes: 'output\/*.md'\r\n  }\r\n}<\/pre>\n<p>Convert same code to declarative block<\/p>\n<p>Lab2:<\/p>\n<ol>\n<li><strong>Agent Block<\/strong>: The <code>agent<\/code> block specifies that this pipeline can be executed on any available agent in your Jenkins environment. The keyword <code>any<\/code> means Jenkins will allocate any available agent to run this pipeline.<\/li>\n<li><strong>Stages<\/strong>:a. <strong>Stage: Create build output<\/strong>:\n<ul>\n<li><code>steps<\/code>: This block defines the steps that should be executed within this stage.<\/li>\n<li><code>script<\/code>: The <code>script<\/code> block allows you to run arbitrary shell scripts within the pipeline. Inside this block, the following steps are performed:\n<ul>\n<li><code>sh \"mkdir -p output\"<\/code>: This shell command creates a directory named &#8220;output&#8221; if it doesn&#8217;t already exist. The <code>-p<\/code> option ensures that the command doesn&#8217;t throw an error if the directory already exists.<\/li>\n<li><code>writeFile file: \"output\/usefulfile.txt\", text: \"This file is useful, need to archive it.\"<\/code>: This step writes some content into a file named &#8220;usefulfile.txt&#8221; in the &#8220;output&#8221; directory.<\/li>\n<li><code>writeFile file: \"output\/uselessfile.md\", text: \"This file is useless, no need to archive it.\"<\/code>: Similarly, this step writes content into a file named &#8220;uselessfile.md&#8221; in the &#8220;output&#8221; directory.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>b. <strong>Stage: Archive build output<\/strong>:<\/p>\n<ul>\n<li><code>steps<\/code>: This block defines the steps that should be executed within this stage.<\/li>\n<li><code>archiveArtifacts<\/code>: This is a built-in Jenkins step for archiving files. In this step:\n<ul>\n<li><code>artifacts: 'output\/usefulfile.txt'<\/code>: It specifies that you want to archive only the file &#8220;usefulfile.txt&#8221; located in the &#8220;output&#8221; directory.<\/li>\n<li><code>excludes: 'output\/*.md'<\/code>: It specifies that you want to exclude any files with the &#8220;.md&#8221; extension from archiving. This means that &#8220;uselessfile.md&#8221; will not be included in the archived artifacts.<\/li>\n<li><code>followSymlinks: false<\/code>: This option is set to <code>false<\/code>, meaning that symbolic links (symlinks) will not be followed when archiving artifacts.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre>pipeline {\r\nagent any\r\n stages {\r\n  stage('Create build output') {\r\n   steps {\r\n    script {\r\n     \/\/ Make the output directory.\r\n     sh \"mkdir -p output\"\r\n     \/\/ Write a useful file, which is needed to be archived.\r\n     writeFile file: \"output\/usefulfile.txt\", text: \"This file is useful, need to archive it.\"\r\n    \/\/ Write a useless file, which is not needed to be archived.\r\n     writeFile file: \"output\/uselessfile.md\", text: \"This file is useless, no need to archive it.\"\r\n     }\r\n    }\r\n   }\r\n   stage('Archive build output') {\r\n    steps {\r\n    \/\/ Archive the build output artifacts.\r\n     archiveArtifacts artifacts: 'output\/*.txt', excludes: 'output\/*.md'\r\n     }\r\n   }\r\n  }\r\n}<\/pre>\n<p>Try : Go to pipeline add one more stage after archive build output,:<\/p>\n<p>name: Interactive input<\/p>\n<p>steps: Add input(Check syntax in pipeline)<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lab1: Youtube Link : Navigation for lab Node Block: The node block specifies that the pipeline should be executed on a Jenkins agent (node). It defines the environment where the pipeline stages will run. Stage: Create build output: sh &#8220;mkdir -p output&#8221;: This shell command creates a directory named &#8220;output&#8221; if it doesn&#8217;t already exist. [&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-1200","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1200","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=1200"}],"version-history":[{"count":4,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1200\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/1200\/revisions\/1407"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}