{"id":1842,"date":"2020-04-05T14:15:45","date_gmt":"2020-04-05T11:15:45","guid":{"rendered":"https:\/\/artem.services\/?p=1833"},"modified":"2020-04-05T21:29:21","modified_gmt":"2020-04-05T18:29:21","slug":"1842","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1842&lang=en","title":{"rendered":"Jenkins &#8212; Active Choice: CheckBox &#8212; Working with an Array"},"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>When working with the<strong> Active Choice CheckBox<\/strong> parameter, the parameter values are written to the variable, separated by commas. To work with them as separate elements, you need to save them in an array. For example, there is the following <strong>Active Choice<\/strong>, which displays a list of environments in the form of a <strong>CheckBox<\/strong>.<\/p>\n<h3>Pipeline:<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nproperties([\r\n  parameters([\r\n    [$class: 'CascadeChoiceParameter', \r\n      choiceType: 'PT_CHECKBOX', \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\\',\\'QA\\',\\'Staging\\',\\'Production\\']'\r\n        ]\r\n      ]\r\n    ]\r\n  ])\r\n])\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>It looks like this:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1834\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/001.png\" alt=\"\" width=\"329\" height=\"261\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/001.png 329w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/001-300x238.png 300w\" sizes=\"(max-width: 329px) 100vw, 329px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Choose two environments, &quot;<strong>Development<\/strong>&quot; and &quot;<strong>QA<\/strong>&quot;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1835\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/002.png\" alt=\"\" width=\"327\" height=\"263\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/002.png 327w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/002-300x241.png 300w\" sizes=\"(max-width: 327px) 100vw, 327px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The value of the variable &quot;<strong>Environment<\/strong>&quot; will be the following: &quot;<strong>Development, QA<\/strong>&quot;. We will save these values to an array for further work with it.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nString[] Env_Array = &quot;${params.Environment}&quot;.split(',');\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Completely <strong>Pipeline<\/strong> will have the following form<\/p>\n<h3>Pipeline:<\/h3>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nproperties([\r\n  parameters([\r\n    [$class: 'CascadeChoiceParameter', \r\n      choiceType: 'PT_CHECKBOX', \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\\',\\'QA\\',\\'Staging\\',\\'Production\\']'\r\n        ]\r\n      ]\r\n    ]\r\n  ])\r\n])\r\n\r\npipeline {\r\n  agent any\r\n  stages {\r\n    stage('Check env') {\r\n      steps {\r\n        script {\r\n          if ( env.Environment.isEmpty() ) {\r\n            echo &quot;Environment not specified.&quot;\r\n            autoCancelled = true\r\n            error('Aborting the build.')\r\n          }\r\n          else {\r\n            echo &quot;Environment total: ${env.Environment}&quot;\r\n            String[] Env_Array = &quot;${params.Environment}&quot;.split(',');\r\n            for (x in Env_Array) {\r\n              echo &quot;ENV: ${x}&quot;\r\n            }\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The result of the <strong>Jenkins<\/strong> job:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1836\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/003.png\" alt=\"\" width=\"511\" height=\"577\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/003.png 511w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/003-266x300.png 266w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with the Active Choice CheckBox parameter, the parameter values are written to the variable, separated by commas. To work with them as separate elements, you need to save them in an array. For example, there is the following Active Choice, which displays a list of environments in the form of a CheckBox. Pipeline: &hellip; <a href=\"https:\/\/artem.services\/?p=1842&#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; Active Choice: CheckBox &#8212; Working with an Array&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":[1465,1481,1483,617,641,1485],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1842"}],"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=1842"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1842\/revisions"}],"predecessor-version":[{"id":1844,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1842\/revisions\/1844"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}