Scripted Jenkins Input Step Pipeline with Sonar Quality Gate

Overview:-

Jenkins is used for CI/CD to avoid manual intervention to deploy the application code in lower environment (like Dev, SIT and QA) then trigger automation code and then deploy to production.

There is also need to add bit of manual intervention like ‘Approve’ or ‘Select type of testing’ etc in the pipe line stages. Here you go, this article will give small example how to use input steps in pipeline.

In addition, one more step to integrate quality check of your automation script.

What you need?

Jenkins server up and running (either service or bat), SonarQube up and running (either service or bat) and maven based JAVA automation code in GitHub.

Will not cover configuration of Jenkins and SonarQube , those are easy and plenty of tutorials and web blogs are available to help you. Let’s focus how to create a pipeline and integrate Jenkins and SonarQube with Input Steps.

How to do it?

Step one – Open SonarQube server (typically http://localhost:9000). Go to My Account -> Security and then generate a token.

Step two – In Jenkins server, install Sonar Scanner plugin. (Manage Jenkins -> Manage Plugins -> SonarQube Scanner for Jenkins).

Step three – Navigate to Manage Jenkins -> Configure System -> SonarQube Servers -> Configure/add.

Name – Put any name ‘sonar’ in my example.

Server URL – Keep it blank if it’s default http://localhost:9000

Server Authentication token – Click on Add then select Jenkins. Select Kind= Secret Text. Put the token from step one in secret text box. Keep any values in ID and description.

No alt text provided for this image

Step four – Save and Apply

Step five – Mange Jenkins -> Global Tool Configuration -> SonarQube Scanner Installation.

Name= Any name, e.g. SONAR_HOME

SONAR_RUNNER_HOME = physical drive location of SonarQube

Apply and Save

No alt text provided for this image

Step six – Create a pipeline with following configuration

GitHub project – <githuburl>

GitHub hook trigger for GITScm polling

Pipeline – Pipeline script

Step seven – Add following code in Pipeline script

node {
   def mvnHome
   def sonarHome
   
   stage('Preparation') { // for display purposes
      // Get some code from a GitHub repository
      git 'https://github.com/<git_repo>/azureselenium.git'
      // Get the Maven tool.
      // ** NOTE: This 'M3' Maven tool must be configured
      // **       in the global configuration.           
      mvnHome = tool 'M2_HOME'
      sonarHome= tool 'SONAR_HOME'
   }
   stage('Smoke Test') {
      // Run the maven build
      withEnv(["MVN_HOME=$mvnHome"]) {
         if (isUnix()) {
            sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
         } else {
            bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore test -Psmoke/)
         }
      }
   }
    stage('Regression Test') {
      // Run the maven build
      withEnv(["MVN_HOME=$mvnHome"]) {
         if (isUnix()) {
            sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
         } else {
            bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore test -Pregression/)
         }
      }
   }
   
    stage('User Input') {
        def typeOfTesting = input(
            message: 'What is the reg value?', 
            parameters: [
                [$class: 'ChoiceParameterDefinition', 
                    choices: 'smoke\nregression\nsanity', 
                    name: 'input', 
                    description: 'A select box option']
            ])

        echo "Reg is ${typeOfTesting}"
        
        withEnv(["MVN_HOME=$mvnHome"]) {
         if (isUnix()) {
            sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
         } else {
            bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore test -P${typeOfTesting}/)
         }
      }
    }
    stage('Code Quality Gate') {
        withSonarQubeEnv('sonar') { 
            bat 'mvn sonar:sonar'
        }
   }
}

Now the pipeline is ready with user input and Quality Check integrated with Sonar Qube.

No alt text provided for this image