Thadir
Thadir

Reputation: 201

Jenkins SonarQube step go to Warn

I am slowly diving in to the pipeline groovy dsl of Jenkins. And I am trying to figure out how I can create a step that does sonar and reflects the quality gate message in the Jenkins buildserver.

At this moment I have the folowing step definded for sonar:

stage ('sonar') {
  mvn "-Dsonar.lang.patterns.jsp=notverified -Dsonar.host.url=http://sonar.server.example:9494 org.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar -Dsonar.login=key
}

Now I want that when Maven gets the :

[INFO] Quality gate status: WARN

that it does not set it on OK or Error but makes that build step go WARN as well (so instead of the nice green or red a nice color of Yellow). I have been digging trough the documentation but as far as I can tell there is no real way to make a Step go to WARN state in any way its in essense binary. And there is noting in between any one a idea?

Upvotes: 2

Views: 874

Answers (1)

Hugues M.
Hugues M.

Reputation: 20467

According to CloudBees support, you can set build result by setting currentBuild.result yourself, so, to get the yellow orb that indicates unstable status (failed unit tests), you can do this:

currentBuild.result = 'UNSTABLE'

Now, you want to do this when Sonar quality gate status is "WARN".

They document how to do it: first you wrap your sonar execution with withSonarQubeEnv('Your Sonar server'), then you wait for server to call you back with the results with waitForQualityGate():

stage("Build & SonarQube analysis") {
    node {
        withSonarQubeEnv('My SonarQube Server') {
            sh 'mvn clean package sonar:sonar'
        }
    }
}

stage("Quality Gate Check"){
    timeout(time: 20, unit: 'MINUTES') {
        def qg = waitForQualityGate()
        if (qg.status == 'WARN') {
            currentBuild.result = 'UNSTABLE'
        }
    }
}

I have not tested this, I have an outdated version that I plan to upgrade when I have time, using a manually scripted approach that's 10 times more complicated than the above, I want to get rid of it and replace it with this solution... Not done yet but I'm sharing notes I collected.

Upvotes: 2

Related Questions