{"id":1838,"date":"2020-04-05T12:36:10","date_gmt":"2020-04-05T09:36:10","guid":{"rendered":"https:\/\/artem.services\/?p=1818"},"modified":"2020-04-05T21:24:29","modified_gmt":"2020-04-05T18:24:29","slug":"1838","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1838&lang=en","title":{"rendered":"Jenkins &#8212; Active Choice: Pipeline examples"},"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>A few examples of how <a href=\"https:\/\/plugins.jenkins.io\/uno-choice\/\" target=\"_blank\" rel=\"noopener noreferrer\">Active Choices<\/a> parameters in <strong>Pipeline <\/strong>as a code<strong><br \/>\n<\/strong><\/p>\n<h3>Example 1<\/h3>\n<p>Single selection from the list provided. A drop-down list of environments, by default, the first item in the list is selected.<\/p>\n<h4>Pipeline:<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nproperties([\r\n  parameters([\r\n    [$class: 'CascadeChoiceParameter', \r\n      choiceType: 'PT_SINGLE_SELECT', \r\n      description: 'Select environment',\r\n      filterLength: 1,\r\n      filterable: false,\r\n      name: 'Environment', \r\n      script: [\r\n        $class: 'GroovyScript', \r\n        script: [\r\n          classpath: [], \r\n          sandbox: false, \r\n          script: \r\n            'return[\\'Development\\',\\'Production\\']'\r\n        ]\r\n      ]\r\n    ]\r\n  ])\r\n])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1819\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.14.00.png\" alt=\"\" width=\"654\" height=\"258\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.14.00.png 654w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.14.00-300x118.png 300w\" sizes=\"(max-width: 654px) 100vw, 654px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1820\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.14.14.png\" alt=\"\" width=\"658\" height=\"264\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.14.14.png 658w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.14.14-300x120.png 300w\" sizes=\"(max-width: 658px) 100vw, 658px\" \/><\/p>\n<h3><\/h3>\n<p><!--more--><\/p>\n<h3>Example 2<\/h3>\n<p>Single selection based on script result. And also depending on the selected environment, in the &quot;<strong>Environment<\/strong>&quot; parameter. The script executes the remote <strong>SSH<\/strong> command on the selected environment, and returns a list of directories located in the &quot;<strong>\/var\/www\/html<\/strong>&quot; directory.<\/p>\n<h4>Pipeline:<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nproperties([\r\n  parameters([\r\n    [$class: 'CascadeChoiceParameter', \r\n      choiceType: 'PT_SINGLE_SELECT', \r\n      description: 'Select Site',\r\n      filterLength: 1,\r\n      filterable: false,\r\n      referencedParameters: 'Environment',\r\n      name: 'Site', \r\n      script: [\r\n        $class: 'GroovyScript', \r\n        script: [\r\n          classpath: [], \r\n          sandbox: false, \r\n          script: \r\n            '''if (Environment.equals(&quot;Development&quot;)){\r\n    def command = $\/ssh centos@192.168.1.101 sudo ls \/var\/www\/html \/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}\r\nelse if(Environment.equals(&quot;Production&quot;)){\r\n    def command = $\/ssh centos@192.168.1.102 sudo ls \/var\/www\/html \/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}'''\r\n        ]\r\n      ]\r\n    ]\r\n  ])\r\n])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1822\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.33.32.png\" alt=\"\" width=\"712\" height=\"424\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.33.32.png 712w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.33.32-300x179.png 300w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1823\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.33.41.png\" alt=\"\" width=\"682\" height=\"434\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.33.41.png 682w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.33.41-300x191.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Example 3<\/h3>\n<p>We pass to the <strong>Active Choice<\/strong> script the result of the previous selection, with the &quot;<strong>Site<\/strong>&quot; parameter. We indicate that this parameter depends on the two previous ones &#8212; &quot;<strong>referencedParameters: &#39;Environment, Site&#39;<\/strong>&quot;. The parameter displays a list of directories of the selected site, and excludes &quot;<strong>..<\/strong>&quot; from the output so that the user does not accidentally delete the directory above that specified.<\/p>\n<h4>Pipeline:<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nproperties([\r\n  parameters([\r\n        [$class: 'CascadeChoiceParameter', \r\n        choiceType: 'PT_SINGLE_SELECT', \r\n        description: 'Select Folder. \\'.\\' - Remove all site folder',\r\n        filterLength: 1,\r\n        filterable: false,\r\n        referencedParameters: 'Environment, Site',\r\n        name: 'Folder', \r\n        script: [\r\n          $class: 'GroovyScript', \r\n          script: [\r\n            classpath: [], \r\n            sandbox: false, \r\n            script: \r\n              '''if (Environment.equals(&quot;Development&quot;)){\r\n    def command = $\/ssh centos@192.168.1.101 sudo ls -a \/var\/www\/html'\/'${Site} | grep -v '\\\\.\\\\.' \/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}\r\nelse if(Environment.equals(&quot;Production&quot;)){\r\n    def command = $\/ssh centos@192.168.1.102 sudo ls -a \/var\/www\/html'\/'${Site} | grep -v '\\\\.\\\\.'\/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}'''\r\n        ]\r\n      ]\r\n    ]\r\n  ])\r\n])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1827\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.51.00.png\" alt=\"\" width=\"834\" height=\"534\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.51.00.png 834w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.51.00-300x192.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.51.00-768x492.png 768w\" sizes=\"(max-width: 834px) 100vw, 834px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1828\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.51.12.png\" alt=\"\" width=\"714\" height=\"534\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.51.12.png 714w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.51.12-300x224.png 300w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/p>\n<p>By default, &quot;<strong>.<\/strong>&quot; Is selected, that is, the entire directory of the site is selected.<\/p>\n<p>&nbsp;<\/p>\n<h3>Example 4<\/h3>\n<p>Add a boolean parameter to confirm the deletion of the selected directory.<\/p>\n<h4>Pipeline:<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nproperties([\r\n  parameters([\r\n    [$class: 'BooleanParameterDefinition', \r\n      description: 'Set this to confirm deletion',\r\n      name: 'Confirm',\r\n      defaultValue: false\r\n    ]\r\n  ])\r\n])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1829\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.55.56.png\" alt=\"\" width=\"680\" height=\"608\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.55.56.png 680w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-05-at-12.55.56-300x268.png 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The whole <strong>pipeline<\/strong> will look like this:<\/p>\n<h4>Pipeline:<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nproperties([\r\n  parameters([\r\n    [$class: 'CascadeChoiceParameter', \r\n      choiceType: 'PT_SINGLE_SELECT', \r\n      description: 'Select environment',\r\n      filterLength: 1,\r\n      filterable: false,\r\n      name: 'Environment', \r\n      script: [\r\n        $class: 'GroovyScript', \r\n        script: [\r\n          classpath: [], \r\n          sandbox: false, \r\n          script: \r\n            'return[\\'Development\\',\\'Production\\']'\r\n        ]\r\n      ]\r\n    ],\r\n    [$class: 'CascadeChoiceParameter', \r\n      choiceType: 'PT_SINGLE_SELECT', \r\n      description: 'Select Site',\r\n      filterLength: 1,\r\n      filterable: false,\r\n      referencedParameters: 'Environment',\r\n      name: 'Site', \r\n      script: [\r\n        $class: 'GroovyScript', \r\n        script: [\r\n          classpath: [], \r\n          sandbox: false, \r\n          script: \r\n            '''if (Environment.equals(&quot;Development&quot;)){\r\n    def command = $\/ssh centos@192.168.1.101 sudo ls \/var\/www\/html \/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}\r\nelse if(Environment.equals(&quot;Production&quot;)){\r\n    def command = $\/ssh centos@192.168.1.102 sudo ls \/var\/www\/html \/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}'''\r\n        ]\r\n      ]\r\n    ],\r\n    [$class: 'CascadeChoiceParameter', \r\n        choiceType: 'PT_SINGLE_SELECT', \r\n        description: 'Select Folder. \\'.\\' - Remove all site folder',\r\n        filterLength: 1,\r\n        filterable: false,\r\n        referencedParameters: 'Environment, Site',\r\n        name: 'Folder', \r\n        script: [\r\n          $class: 'GroovyScript', \r\n          script: [\r\n            classpath: [], \r\n            sandbox: false, \r\n            script: \r\n              '''if (Environment.equals(&quot;Development&quot;)){\r\n    def command = $\/ssh centos@192.168.1.101 sudo ls -a \/var\/www\/html'\/'${Site} | grep -v '\\\\.\\\\.' \/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}\r\nelse if(Environment.equals(&quot;Production&quot;)){\r\n    def command = $\/ssh centos@192.168.1.102 sudo ls -a \/var\/www\/html'\/'${Site} | grep -v '\\\\.\\\\.'\/$\r\n        def proc = command.execute()\r\n        return proc.text.readLines()\r\n}'''\r\n        ]\r\n      ]\r\n    ],\r\n    [$class: 'BooleanParameterDefinition', \r\n      description: 'Set this to confirm deletion',\r\n      name: 'Confirm',\r\n      defaultValue: false\r\n    ]\r\n  ])\r\n])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Pipeline<\/strong> itself, checking the existence of the specified directory, and notifying <strong>Slack<\/strong> about which user (requires the installed <a href=\"https:\/\/artem.services\/?p=1810&amp;lang=en\" target=\"_blank\" rel=\"noopener noreferrer\">user build vars<\/a> plugin) which directory has been deleted, will look like this.<\/p>\n<h4>Pipeline:<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npipeline {\r\n  agent any\r\n  options {\r\n    buildDiscarder(logRotator(numToKeepStr: '10'))\r\n    timeout(time: 60, unit:'MINUTES')\r\n    timestamps()\r\n  }\r\n  environment {\r\n    def SSH_USER = 'centos'\r\n    def DEVELOPMENT_INSTANCE = '192.168.1.101'\r\n    def PRODUCTION_INSTANCE = '192.168.1.102'\r\n    def WEB_DIR = '\/var\/www\/html'\r\n    def SLACK_CHANNEL = 'general'\r\n  }\r\n  stages {\r\n    stage('Definition of variables') {\r\n      steps {\r\n        script {\r\n          if ( env.Site.isEmpty() ) {\r\n            echo &quot;Site not specified. If you continue, the www root directory may be deleted.&quot;\r\n            autoCancelled = true\r\n            error('Aborting the build.')\r\n          }\r\n          if ( env.Folder == '.' ) {\r\n            env.FULL_PATH = &quot;${WEB_DIR}\/${Site}&quot;\r\n            echo &quot;Folder not specified&quot;\r\n            echo &quot;Path for delete is: ${FULL_PATH}&quot;\r\n          }          \r\n          else {\r\n            env.FULL_PATH = &quot;${WEB_DIR}\/${Site}\/${Folder}&quot;\r\n            echo &quot;Path for delete is: ${FULL_PATH}&quot;\r\n          }\r\n        }\r\n      }\r\n    }\r\n    stage('Check dir existence') {\r\n      steps {\r\n        script {\r\n          echo &quot;Environment: ${env.Environment}&quot;\r\n          echo &quot;Full path: ${FULL_PATH}&quot;\r\n          if ( env.Environment == 'Development' ) {\r\n            env.EXIST_DIR = sh(script: 'ssh \\${SSH_USER}@\\${DEVELOPMENT_INSTANCE} \\'[ -d \\${FULL_PATH} ] &amp;&amp; echo \\&quot;yes\\&quot; || echo \\&quot;no\\&quot;\\'', returnStdout: true).trim()\r\n            echo &quot;EXIST_DIR: ${EXIST_DIR}&quot;\r\n          }\r\n          else if ( env.Environment == 'Production' ) {\r\n            env.EXIST_DIR = sh(script: 'ssh \\${SSH_USER}@\\${PRODUCTION_INSTANCE} \\'[ -d \\${FULL_PATH} ] &amp;&amp; echo \\&quot;yes\\&quot; || echo \\&quot;no\\&quot;\\'', returnStdout: true).trim()\r\n            echo &quot;EXIST_DIR: ${EXIST_DIR}&quot;\r\n          }\r\n          else {\r\n            echo &quot;Environment not specified&quot;\r\n            autoCancelled = true\r\n            error('Aborting the build.')\r\n          }\r\n          if ( env.EXIST_DIR == 'no' ) {\r\n            echo &quot;Directory: ${FULL_PATH} on ${env.Environment} does not exist&quot;\r\n            autoCancelled = true\r\n            error('Aborting the build.')\r\n          }\r\n        }\r\n      }\r\n    }\r\n    stage('Remove dir') {\r\n      when { expression { return params.Confirm } }\r\n      steps {\r\n        script {\r\n          echo &quot;Remove directory ${FULL_PATH} on ${env.Environment} server&quot;\r\n          if ( env.Environment == 'Development' ) {\r\n            sh &quot;ssh \\${SSH_USER}@\\${DEVELOPMENT_INSTANCE} \\&quot;sudo rm -rf ${FULL_PATH}\\&quot;&quot;\r\n          }\r\n          else if ( env.Environment == 'Production' ) {\r\n            sh &quot;ssh \\${SSH_USER}@\\${PRODUCTION_INSTANCE} \\&quot;sudo rm -rf ${FULL_PATH}\\&quot;&quot;\r\n          }\r\n          else {\r\n            echo &quot;Environment not specified&quot;\r\n            autoCancelled = true\r\n            error('Aborting the build.')\r\n          }\r\n        }\r\n      }\r\n    }\r\n    stage('Output') {\r\n      steps {\r\n        script {\r\n          wrap([$class: 'BuildUser']) {\r\n            if ( env.Confirm.toBoolean() ) {\r\n              echo &quot;Directory ${FULL_PATH} on ${env.Environment} server was deleted by user: ${BUILD_USER}&quot;\r\n              slackSend channel: &quot;${SLACK_CHANNEL}&quot;, color: 'good', message: &quot;Job: ${JOB_NAME} was successful.\\nEnvironment: `${env.Environment}`\\nDirectory: `${FULL_PATH}`\\nState: `Deleted`\\nUser: `${BUILD_USER}`&quot;\r\n            }\r\n            else {\r\n              echo &quot;Directory ${FULL_PATH} on ${env.Environment} server was deleted (Dry run) by user: ${BUILD_USER}&quot;\r\n              slackSend channel: &quot;${SLACK_CHANNEL}&quot;, color: 'good', message: &quot;Job: ${JOB_NAME} was successful.\\nEnvironment: `${env.Environment}`\\nDirectory: `${FULL_PATH}`\\nState: `Dry run`\\nUser: `${BUILD_USER}`&quot;\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n  post {\r\n    failure {\r\n      slackSend channel: &quot;${SLACK_CHANNEL}&quot;, color: 'danger', message: &quot;Job: ${JOB_NAME} was finished with some error.\\nPlease watch the Jenkins Console Output: ${JOB_URL}${BUILD_ID}\/consoleFull&quot;\r\n    }\r\n    aborted {\r\n      slackSend channel: &quot;${SLACK_CHANNEL}&quot;, color: 'warning', message: &quot;Job: ${JOB_NAME} was canceled.\\nPlease watch the Jenkins Console Output: ${JOB_URL}${BUILD_ID}\/console.&quot;\r\n    }\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; A few examples of how Active Choices parameters in Pipeline as a code Example 1 Single selection from the list provided. A drop-down list of environments, by default, the first item in the list is selected. Pipeline: &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[613],"tags":[1465,617,641],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1838"}],"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=1838"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1838\/revisions"}],"predecessor-version":[{"id":1840,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1838\/revisions\/1840"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}