Developing a parameterized pipeline in jenkins

Scenario: you're on a development team using Jenkins to test and deploy a new application. Your team has already developed the outline for a pipeline. Now they need to update a pipeline. So that it accepts parameters for multiple environments. You've been given several requirements for the updates.

you need to add 3 parameters with defaults :

Environment DEVELOPMENT, STAGING, PRODUCTION

APIKEY 123ABC

CHANGELOG This is the change log.

The first parameter is an environment which allows the person triggering the pipeline to select the target environment.

The choices for the environment should be limited to development, staging and production. The default environment should be developed.

The second parameter should be named API Key and allow a secret value to be passed into the pipeline. Make sure that the API key is not exposed when the value is entered in the Jenkins interface. Use the value 123ABC as the default.

And the last parameter should be named changelog and accept freeform text that can be added to a report.

There are 3 stages in the pipeline.

currently three stages: Test, Deploy, and Report.

Deploy only deploy the PRODUCTION environment

The report uses the CHANGELOG parameter for the report content. Use the ENVIRONMENT parameter for the name of the report file

to update the deploy stage to only deploy to the production environment. In the production stage, update the report so that it uses the changelog for the content of the report. Also, it changed the name of the report. so that it matches the name of the environment.

Tips to solve :

  • A pipeline template is available in the exercise files.

  • use pipeline syntax to generate syntax for your pipeline.

  • Especially take a look at Declarative Directive Generator

First, let's go to the Jenkins server's web page.

On the Jenkins Interface, click on the new item button which is marked in yellow. Then you will get the screen below. Here I have given my pipeline named "Parameterized Pipeline" then click on pipeline and then finally select ok.

pipeline {
    agent any
    parameters {
      choice choices: ['DEVELOPMENT', 'STAGING', 'PRODUCTION'], 
         description: 'Choose the environment for this deployment.', 
         name: 'ENVIRONMENT'

      password defaultValue: '123ABC', 
         description: 'Enter the API key to use for this deployment.', 
         name: 'API_KEY'

      text defaultValue: 'This is the change log.', 
         description: 'Enter the components that were changed in this deployment.', 
         name: 'CHANGELOG'
    }    
    stages {
        stage('Test') {
            steps {
                echo "This step tests the compiled project"
            }
        }
        stage('Deploy') {
            when {
              expression { params.ENVIRONMENT == "PRODUCTION" }
            }            
            steps {
                echo "This step deploys the project"
            }
        }        
        stage('Report') {
            steps {
                echo "This stage generates a report"
                sh "printf \"${params.CHANGELOG}\" > ${params.ENVIRONMENT}.txt"
                archiveArtifacts allowEmptyArchive: true, 
                    artifacts: '*.txt', 
                    fingerprint: true, 
                    followSymlinks: false, 
                    onlyIfSuccessful: true
            }
        }
    }
}

once you click on the save button then you will be redirected to the below page here in this page click on the build now option which is marked in yellow color.

After some seconds your screen would be like below

Now reload the page and then your screen will be like below. Changes you can observe here.

copy that generate declarative directive output to the keyboard.

Now again go back to the pipeline and Make changes as below images in the pipeline syntax and then click on save button at the bottom.

Let's wrap up the things we did so far..

  • we created a pipeline job and added parameters. Those parameters exposed three options, Development, staging and production.

  • we added an API key, which uses the password type for a parameter that protects the value that's being passed into the job. And then we added a change log parameter that uses the multi-line string type so that we could enter multiple lines here in this large text field.

  • Then we added some conditions that only allow deployments to happen for the PRODUCTION environment.

  • so, we did two tests there we deployed once with the DEVELOPMENT environment and this deployment was skipped.

  • And then we deployed again with the PRODUCTION environment and we did get a deployment. so that worked perfectly.

  • The last change we did is we modified the report name from report.txt to use the name of the environment that was selected as a parameter.

congratulations folks, we have finished our challenge.

See you soon with yet another interesting blog, till then please follow me and read and learn..

Thanks for reading.

Before you leave this page, please provide your valuable feedback by doing like, share and commenting .

Did you find this article valuable?

Support Nagacharan by becoming a sponsor. Any amount is appreciated!