Parameterize jenkins pipeline jobs (part 2)

Devops Tutorial​​ 30 -​​ Parameterize jenkins​​ pipeline jobs​​ (part 2)

What you will Learn:

  • Parameterize​​ pipeline​​ jobs​​ (using pipeline syntax)​​ (continued)

  • Parameterize pipeline jobs (using declarative script)

Parameterize pipeline jobs​​ (using pipeline syntax)​​ (Contd…)

This is continuation of previous article. We got an error​​ “Couldn't find any revision to build. Verify the repository and branch configuration for this job.”

To resolve this error, we​​ have to change the branch name ‘master’ to ‘main’ since the github has branch name as ‘main’​​ 

Let us configure the project

In the ‘General’ section, rename ‘master’ to ‘main’ as shown below

Next, do the same thing in pipeline section

The code is now as shown below:

properties([parameters([choice(choices: ['main', 'feature_1'], description: 'Select desired branch to build', name: 'branches')])])

node{

 ​​ ​​ ​​​​ stage('checkout from github') {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "checking-out from branch ${params.branches}"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ git url: 'https://github.com/W2AGit/JenkinsPipelineAsACode', branch: "${params.branches}"

 ​​ ​​ ​​​​ }

}

Apply and Save

Notice below that we now see ‘Build with Parameters’ option on the left hand side

Click this option.​​ 

See below. On the right hand side we see a dropdown. The text that you see below the dropdown is the description that we had mentioned while creating the choice parameter (this description is also part of code snippet that we generated)​​ 

Next click the dropdown, you would see 2 branches: master and feature_1

Let us select ‘feature_1’​​ 

Click Build. Notice below, the build is success

See console output

Similarly, build this time with ‘main’ branch

Parameterize pipeline jobs (using declarative script)

Look at the below declarative pipeline script that has 2 sections: parameters and stages. In the parameters section, we have defined 3 parameters types: choice, boolean, string

Each parameter type has: name, default value, description.

In the stages section, we are fetching these parameters using params object that we saw earlier (self-explanatory).

pipeline {

 ​​ ​​ ​​​​ agent any  ​​ ​​​​ 

 ​​ ​​ ​​​​ parameters{

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ choice(name: 'choice_param',

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ choices: 'pink\nred\norange',

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ description: 'Please choose any color')
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ booleanParam(name: 'boolean_param',

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ defaultValue: false,

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ description: 'Select checkbox')
 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ string(name: 'string_param',

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ defaultValue: 'W2A',

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ description: 'Enter some string value')

 ​​ ​​ ​​​​   ​​ ​​ ​​ ​​​​ }

 ​​ ​​ ​​​​ stages {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ stage('DifferentPrameterTypes') {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ steps {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "Welcome to choice parameter: ${params.choice_param}"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "Welcome to boolean parameter: ${params.boolean_param}"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ echo "Welcome to string parameter: ${params.string_param}"

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​   ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​   ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

  ​​ ​​ ​​​​ }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

Just copy the above code, create a new pipeline job


Paste the code in pipeline section as shown below

Apply and save

Build Now

Refresh the page to see ‘Build with Parameters’ option

​​ 

Click this option.​​ 

Notice below, we see the choice, boolean and string parameters with default values

Click Build

Click Logs, you would see the print messages as expected

So this is how​​ we can parameterize​​ build pipelines.

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 1st Nov 2024