<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aleksander</title>
    <description>The latest articles on DEV Community by Aleksander (@monk).</description>
    <link>https://dev.to/monk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F607695%2F5fd6ea6f-0a80-4a47-bda7-e1ef39898493.jpeg</url>
      <title>DEV Community: Aleksander</title>
      <link>https://dev.to/monk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/monk"/>
    <language>en</language>
    <item>
      <title>Generating parallel stages in Jenkinsfile according to passed parameters</title>
      <dc:creator>Aleksander</dc:creator>
      <pubDate>Fri, 30 Apr 2021 11:56:14 +0000</pubDate>
      <link>https://dev.to/monk/generating-parallel-stages-in-jenkinsfile-according-to-passed-parameters-4nhd</link>
      <guid>https://dev.to/monk/generating-parallel-stages-in-jenkinsfile-according-to-passed-parameters-4nhd</guid>
      <description>&lt;p&gt;Let's say that you have an app that you want to test with different versions of Oracle DB. &lt;/p&gt;

&lt;p&gt;Sometimes you want to run tests for given pairs. Sometimes for one, sometimes for a few, sometimes for all.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;version A of your app with Oracle 11&lt;/li&gt;
&lt;li&gt;version A of your app with Oracle 12&lt;/li&gt;
&lt;li&gt;version B of your app with Oracle 11&lt;/li&gt;
&lt;li&gt;and so on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can create different pipelines for different cases, but why not make it easier? &lt;/p&gt;

&lt;p&gt;Here we will create a declarative pipeline that will run different jobs depending on the selected parameters. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. We need to decide how we will pass parameters
&lt;/h3&gt;

&lt;p&gt;For our use-case the easiest way would be to use &lt;code&gt;booleanParam&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    parameters {
        booleanParam(defaultValue: false, name: "oracle11a", description: "Oracle 11 + App Version A")
        booleanParam(defaultValue: false, name: "oracle12a", description: "Oracle 12 + App Version A")
        booleanParam(defaultValue: false, name: "oracle11b", description: "Oracle 11 + App Version B")
...
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why &lt;code&gt;booleanParam&lt;/code&gt;? Because all parameters in Jenkinsfile are stored as a map. So, our key in that map would be the name of the parameter and the value will be true or false (depending on did we select it or not in the UI)&lt;/p&gt;

&lt;h3&gt;
  
  
  2. We need to store additional information about our test cases.
&lt;/h3&gt;

&lt;p&gt;Above the &lt;code&gt;pipeline {...}&lt;/code&gt; we will create a map with parameters that we will need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useful_map = [:]

def populate_useful_map() {
    useful_map["oracle11a"] = ["oracle11", "appVersionA"]
    useful_map["oracle12a"] = ["oracle12", "appVersionA"]
    useful_map["oracle11b"] = ["oracle11", "appVersionB"]
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we create and populate our map of parameters that we want to have to pass to another job/test script/etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. We will create all stages that we will want to run
&lt;/h3&gt;

&lt;p&gt;So, for each parameter, if it was selected on the UI we will get values from the &lt;code&gt;useful_map&lt;/code&gt; and create a stage with a call to another job defined in Jenkins which will run tests with given versions of DB and our application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;params.each { key, value -&amp;gt;
   if (value) {
      def oracle_db = useful_map[key][0];
      def app_version = useful_map[key][1];
      stages_to_run["${oracle_db} + ${app_version}"] =
        {stage("${oracle_db} + ${app_version}") {
          build(job: "pipeline-to-run-our-tests", 
                propagate: true,
                parameters: [
    string(name: 'ORACLE_DB_VERSION', value: "${oracle_db}"),
    string(name: 'APP_VERSION', value: "${app_version}")
                ])
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. We want to run all the job created in parallel
&lt;/h3&gt;

&lt;p&gt;All work is done, now we just need to pass &lt;code&gt;stages_to_run&lt;/code&gt; that we created in step 3 to parallel function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stage('In parallel') {
            steps {
                script {
                    parallel stages_to_run
                }
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>jenkins</category>
      <category>groovy</category>
      <category>parallel</category>
      <category>jenkinsfile</category>
    </item>
  </channel>
</rss>
