0
votes

I have been trying to integrate sonarqube with Jenkins. It was working fine until I used scripted pipeline to return the quality gate status. The code I am writing is:

pipeline {
agent any
stages {
    stage("Git checkout") {
        steps {
            git 'https://github.com/AmolMandloi/junit-java-example.git'
        }
    }
    stage("Maven") {
        steps {
            bat "mvn clean package test"
        }
    }
    stage("Sonar") {
        steps {
withSonarQubeEnv('sonar'){
        bat 'mvn sonar:sonar'
    }
        }
    }
    stage("Quality Gate"){
        timeout(time: 1, unit: 'HOURS') {
            def qg = waitForQualityGate()
            if (qg.status != 'OK') {
                error "Pipeline aborted due to quality gate failure: ${qg.status}"
          }
      }
  }
    stage("Postsonar") {
        steps{
            bat 'echo "All done"'
        }
    }

}}

The error is in the Quality Gate stage because without it all is working fine.enter image description here

This is the error:

Started by user Amol Mandloi Running in Durability level: MAX_SURVIVABILITY org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 21: Unknown stage section "timeout". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 21, column 9. stage("Quality Gate"){ ^

WorkflowScript: 21: Expected one of "steps", "stages", or "parallel" for stage "Quality Gate" @ line 21, column 9. stage("Quality Gate"){ ^

2 errors

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)

Finished: FAILURE

1

1 Answers

0
votes
stage("Quality Gate"){
    timeout(time: 1, unit: 'HOURS') {
        def qg = waitForQualityGate()
        if (qg.status != 'OK') {
            error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
    }
}

In declarative pipeline, timeout is not valid directly under stage. You can either use it as an option or as a step.

In any case you need a script step for a scripted section within a declarative pipeline.

Timeout option

stage("Quality Gate"){
    options {
        timeout(time: 1, unit: 'HOURS') 
    }
    steps {
        script {
            def qg = waitForQualityGate()
            if (qg.status != 'OK') {
                error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
    }
}

This syntax is more readable when the timeout applies to everything within the stage.

Timeout step

stage("Quality Gate"){
    steps {
        script {
            timeout(time: 1, unit: 'HOURS') {
                def qg = waitForQualityGate()
                if (qg.status != 'OK') {
                    error "Pipeline aborted due to quality gate failure: ${qg.status}"
            }
        }
    }
}

This syntax is necessary when the timeout applies only to parts of the stage. In your case I would go with the timeout option for a more declarative, readable approach.