How to add Quality Gate to the pipeline flow?

Hello everyone. I need help adding Quality Gate to the following pipeline.

pipeline {
    agent {
    docker {
        image 'mcr.microsoft.com/dotnet/core/sdk:3.1'
        args '-u root:root -v /var/run/docker.sock:/var/run/docker.sock'
        }
    }    
    stages {
        stage('restor'){
        steps{
            echo '''dotnet restore -v d'''
            }
        }
        stage('Build Solution'){
        steps{
            sh '''dotnet build --configuration Release'''
            }
        }
        stage ('Unit Tests') {
        steps{
            sh '''
            export PATH="$PATH:/root/.dotnet/tools"            
            dotnet test ./WebApplication1.sln --configuration Release
            '''
            }
        }
        stage ('Sonar Analysis') {                  
        steps{
                withCredentials([string(credentialsId: 'adminsonarqube', variable: 'ADMIN_SONNARQUBE')]) {
                sh '''
                export PATH="$PATH:/root/.dotnet/tools"

                dotnet sonarscanner begin /k:"${JOB_NAME}" /d:sonar.host.url="http://10.10.100.18:9000" /d:sonar.login="$ADMIN_SONNARQUBE"
                dotnet build ./WebApplication1.sln --configuration Release                   
                dotnet sonarscanner end /d:sonar.login="$ADMIN_SONNARQUBE"
                '''
                }    
            }
        }
    }
}

The current flow follows.

Hi,

Welcome to the community!

From the screenshot, I guess we’re talking about Jenkins? The docs should help.

 
Ann

Specifically the waitForQualityGate docs.

I was not successful with the SonarQube documentation. So, I used the method below.

Follow.

    stage('Quality Gate') {
      steps{
        script {
        def Result = sh returnStdout: true, script:
        ''' 
        curl -s "http://IP:Port/api/qualitygates/project_status?projectKey=${JOB_NAME}"|cut -d'"' -f6|wc -c ;
        '''
        if ( env.Result <= "3" ){
        echo 'Passed'
        } else{
        currentBuild.result = 'ABORTED'
            } 
        }
    }
}

This is our Jenkins shared library method to run waitForQualityGate. Could also be inside a script tag in the pipeline. The logMessage and saveStatus methods are our own logging interface:

    sleep 10        // wait to ensure all sonar files are written to the local disk
    try {
        def source = gv.workspaceRootSrc
        dir (gv.workspaceRootSrc) {
            withSonarQubeEnv ('SonarQube') {
                // Wait for Quality Gate and put status into globalVariables
                gv.qualityGateStatus = waitForQualityGate().status
            }
            if (gv.qualityGateStatus == 'OK') {
                // Passed Quality Gate
                logMessage message: "[WFGG] Quality Gate Passed", messageLevel: 'INFO', globalVariables: gv
                saveStatus step:"Quality Gate", task: 'Gate', level: 'Pass', jobStatus: 'SUCCESS', message: "Quality Gate Passed", globalVariables: gv
            }
            else {
                // qualityGateStatus='ERROR' - failed Quality Gate
                logMessage message: "[WFGG] Quality Gate Failed", messageLevel: 'INFO', globalVariables: gv
                saveStatus step:"Quality Gate", task: 'Gate', level: 'Fail', jobStatus: 'UNSTABLE', message: "Quality Gate Failed", globalVariables: gv
            }
        }

    }

    catch (err) {

        gv.qualityGateStatus = 'ERROR'

        logMessage message: "[WFQG] Wait for Quality Gate Error ($err)", messageLevel: 'INFO', globalVariables: gv

        saveStatus step:"Quality Gate", task: 'Wait', level: 'Fail', jobStatus: 'UNSTABLE', message: "Wait for Quality Gate Error ($err)", globalVariables: gv

    }

I tried using your method, but Jenkins swears at gv prefix and what workspaceRootSrc means.
can you tell me how I can use this in a pipeline script block?

Sorry, gv is a global variable we def at the top of the pipeline and use to hold variables that need to persist between stages. workspaceRootSrc is the folder where the source code resides.

1 Like