banner



How To Keep Text Ina Dynamic Block Readable Rotating Block

In Jenkins'due south declarative pipeline, you can add together parameters every bit part of Jenkinsfile. In that location are many supported parameters types that you lot can use with a declarative pipeline.

In this blog, you take answers to the following.

  1. How to apply parameters in the declarative pipeline?
  2. How to apply dynamic parameters or active pick parameters in the declarative pipeline?

Generating Pipeline Code for Parameters

Y'all can generate the parameter pipeline code block hands using the Jenkins pipeline generator. You will find the Pipeline syntax generator link under all the pipeline jobs, as shown in the image below.

Jenkins pipeline syntax generator

Navigate to the pipeline generator in Jenkins and under steps, search for backdrop, equally shown beneath.

Generating Jenkins parameter code for declarative pipeline

Using Parameters in Jenkinsfile

This script given below has the following parameter types.

  1. Option parameters
  2. Boolean parameter
  3. Multi-line cord parameter
  4. String Parameter

Hither is the Github link for this code.

          pipeline {     agent any     stages {         stage('Setup parameters') {             steps {                 script {                      properties([                         parameters([                             choice(                                 choices: ['ONE', '2'],                                  proper name: 'PARAMETER_01'                             ),                             booleanParam(                                 defaultValue: true,                                  description: '',                                  proper name: 'BOOLEAN'                             ),                             text(                                 defaultValue: '''                                 this is a multi-line                                  string parameter example                                 ''',                                   name: 'MULTI-LINE-STRING'                             ),                             string(                                 defaultValue: 'scriptcrunch',                                  name: 'STRING-PARAMETER',                                  trim: truthful                             )                         ])                     ])                 }             }         }     }    }        

Notation: The parameters specified in the Jenkinsfile will appear in the job just after the offset run. Your showtime job run will neglect as yous volition non be able to provide the parameter value through the job.

Access Parameters Inside Pipeline Stages

You lot can access a parameter at whatever stage of a pipeline. Accessing parameters in stages is pretty straightforward. You just accept to employ params.[NAME] in places where yous need to substitute the parameter.

Hither is an example of a stage that will exist executed based on the condition that we become from the option parameter.

The parameter name is ENVIRONMENT, and we access it in the stage as params.Environment. So when the choice parameter matches PROD, it volition execute the steps mentioned in the stage.

          stage('Deploy to Product') {             when {                 expression {                     return params.Environment == 'PROD'                 }             }             steps {                     sh """                     repeat "deploy to production"                     """                 }             }    }        

Using Active Selection Parameter in Declarative Pipeline for Dynamic Parameters

Unlike default parameter types, the Agile choice parameter blazon gives y'all more command over the parameters using a groovy script. You can accept dynamic parameters based on user parameter choice.

To use the active pick parameter, you need to have an Agile Choices plugin installed in Jenkins.

Here is a small utilise case for an agile choice parameter.

  1. A job should have three parameters
    • Environment (dev, phase & prod)
    • AMI List (Should listing the AMIs based on environment)
    • AMI information (Show information near the AMIs related to a specific surroundings)
  2. If the user selects dev, the AMI listing should dynamically change the values related to dev and show data related to the AMIs.

Here is the image which shows the above employ case. Information technology shows how the AMI list and AMI information changes when you lot select different environments.

declarative active choice parameter demo

There are iii types of active choice parameters.

Agile Choices Parameter

Thi parameter type returns a ready of parameters returned by the swell script. For example, an environment parameter that lists dev, stage, and prod values.

          return['dev','phase','prod']        

You lot can likewise return values from third-party APIs as parameters.

One such example is dynamically showing folders from a Github repo in the Jenkins parameters. To make this work you simply need to write a groovy script that calls Github APIs and query the folders of the specific repository.

Agile Choices Reactive Parameter

Returns parameters based on conditions based on another referenced parameter. You can refer to an agile option parameter and return a parameter based on a condition. For case, if the environment parameter is selected as a dev, the reactive parameter will return AMI ids for dev based on groovy conditions.

In the following example, Env is the reference parameter.

          if (Env.equals("dev")){     return["ami-sd2345sd", "ami-asdf245sdf", "ami-asdf3245sd"] } else if(Env.equals("stage")){     render["ami-sd34sdf", "ami-sdf345sdc", "ami-sdf34sdf"] } else if(Env.equals("prod")){     return["ami-sdf34", "ami-sdf34ds", "ami-sdf3sf3"] }        

Agile Choices Reactive Reference Parameter

The reactive reference parameter is similar to a reactive parameter except for the fact that it mostly will non be used in the build surroundings. Significant, it is often used to display data to the user dynamically to select the correct values from the other parameter input fields, every bit shown in the higher up use case image.

Using Active Choice Parameters With Declarative Pipeline

If you are wondering how to apply agile choice parameters in a declarative pipeline, here is the Jenkinsfile with all Active Option parameter types. If you execute this, you will get parameters like the demo I have shown with the employ case.

Note: Sometimes, after the execution of the pipeline, the parameters won't testify up correctly. If it happens, open up job configuration and save it one time without changing anything. The values will show up.

If you take problem copying the code, use this Github link

          pipeline {     agent any         stages {             stage('Parameters'){                 steps {                     script {                     properties([                             parameters([                                 [$class: 'ChoiceParameter',                                      choiceType: 'PT_SINGLE_SELECT',                                      description: 'Select the Environemnt from the Dropdown List',                                      filterLength: one,                                      filterable: false,                                      name: 'Env',                                      script: [                                         $class: 'GroovyScript',                                          fallbackScript: [                                             classpath: [],                                              sandbox: simulated,                                              script:                                                  "return['Could not get The environemnts']"                                         ],                                          script: [                                             classpath: [],                                              sandbox: false,                                              script:                                                  "return['dev','stage','prod']"                                         ]                                     ]                                 ],                                 [$form: 'CascadeChoiceParameter',                                      choiceType: 'PT_SINGLE_SELECT',                                      description: 'Select the AMI from the Dropdown List',                                     proper noun: 'AMI List',                                      referencedParameters: 'Env',                                      script:                                          [$class: 'GroovyScript',                                          fallbackScript: [                                                 classpath: [],                                                  sandbox: false,                                                  script: "return['Could not get Environment from Env Param']"                                                 ],                                          script: [                                                 classpath: [],                                                  sandbox: imitation,                                                  script: '''                                                 if (Env.equals("dev")){                                                     return["ami-sd2345sd", "ami-asdf245sdf", "ami-asdf3245sd"]                                                 }                                                 else if(Env.equals("stage")){                                                     return["ami-sd34sdf", "ami-sdf345sdc", "ami-sdf34sdf"]                                                 }                                                 else if(Env.equals("prod")){                                                     return["ami-sdf34sdf", "ami-sdf34ds", "ami-sdf3sf3"]                                                 }                                                 '''                                             ]                                      ]                                 ],                                 [$class: 'DynamicReferenceParameter',                                      choiceType: 'ET_ORDERED_LIST',                                      description: 'Select the  AMI based on the following infomration',                                      proper noun: 'Image Information',                                      referencedParameters: 'Env',                                      script:                                          [$form: 'GroovyScript',                                          script: 'return["Could not get AMi Information"]',                                          script: [                                             script: '''                                                     if (Env.equals("dev")){                                                         return["ami-sd2345sd:  AMI with Java", "ami-asdf245sdf: AMI with Python", "ami-asdf3245sd: AMI with Dandy"]                                                     }                                                     else if(Env.equals("stage")){                                                         render["ami-sd34sdf:  AMI with Java", "ami-sdf345sdc: AMI with Python", "ami-sdf34sdf: AMI with Great"]                                                     }                                                     else if(Env.equals("prod")){                                                         return["ami-sdf34sdf:  AMI with Java", "ami-sdf34ds: AMI with Python", "ami-sdf3sf3: AMI with Groovy"]                                                     }                                                     '''                                                 ]                                         ]                                 ]                             ])                         ])                     }                 }             }         }    }        

Jenkinsfile Parameter Best Practices

The post-obit are some of the all-time practices y'all can follow while using parameters in a Jenkinsfile.

  1. Never pass passwords in the String or Multi-line parameter block. Instead, apply the countersign parameter of access Jenkins credentials with credential id as the parameter.
  2. Effort to use parameters merely if required. Alternatively, yous can use a config management tool to read configs or parameters in the runtime.
  3. Handle the wrong parameter execution in the stages with a proper exception handling. It avoids unwanted step execution when a wrong parameter is provided. It happens typically in multi-line and string parameters.

Jenkinsfile Parameter FAQs

How to dynamically populate the choice parameter in the declarative pipeline?

Dynamic parameters can be achieved by using an agile choice parameter. It uses a groovy script to dynamically populate choice parameter values.

How are the parameters used in the declarative pipeline?

In the declarative pipeline, parameters tin be incorporated using the properties cake. It supports all types of Jenkins parameters.

How to generate pipeline code for parameters?

You tin can utilise the native Jenkins pipeline syntax generator to generate the code block for any type of pipeline parameter.

Source: https://devopscube.com/declarative-pipeline-parameters/

Posted by: morganarmest00.blogspot.com

0 Response to "How To Keep Text Ina Dynamic Block Readable Rotating Block"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel