5

I'm trying to create a Jenkins multibranch pipeline where on every push to bitbucket, a SonarQube analysis is performed on that branch of the project. Jenkins correctly creates the new job for each branch and a new project is created in SonarQube with the branch name appended to the project name.

The issue I'm having is that when SonarQube creates the new project, the webhook to report the Quality Gate status is not set by default, so I have to manually go into each SonarQube project and set the Webhook url. This is an issue when my team makes many branches a day.

Is there a way to specify in my Jenksfile that I want the SonarQube project to have a webhook?

stage('SonarQube Analysis') {
        steps {
            withSonarQubeEnv('Sonarqube Server') {
                script {
                    def sonarScanner = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
                    sh "${sonarScanner}/bin/sonar-scanner " +
                    "-Dsonar.projectKey=ProjectName-${GIT_BRANCH} " +
                    "-Dsonar.projectName=ProjectName-${GIT_BRANCH} " +
                    "-Dsonar.projectVersion=0.0.0 " +
                    "-Dsonar.sources=**/src " +
                    "-Dsonar.java.binaries=**/build " +
                    "-Dsonar.exclusions=excluded_dirs/** " +
                    "-Dsonar.sourceEncoding=UTF-8"
                }
            }
            timeout(time: 5, unit: 'MINUTES') {
                script {
                    def qg = waitForQualityGate()
                    if (qg.status != 'OK') {
                        error "Pipeline aborted due to a quality gate failure: ${qg.status}"
                    }
                }
            }
        }
    }

Currently, my Jenkins build times out after 5 minutes. When the webhook is set, it takes a few seconds to hear back. My webhook url is correct, I just want the Jenkinsfile to set it, not me manually.

EDIT: Unfortunately, I am not an admin in SonarQube, only my projects

DarkHark
  • 614
  • 1
  • 6
  • 20

2 Answers2

9

As admin in sonarqube, go to https://my-sonarqube.tld/admin/webhooks configure the url to be https://my-jenkins-domain.tld/sonarqube-webhook/

This should then apply to all projects. If you are still not receiving deliveries, check recent deliveries (option in same page) and view error.

Your jenkins will need to have a valid certificate for a secure connection to be established

See also: https://docs.sonarqube.org/latest/project-administration/webhooks/

Alternatively, you can set a webhook per invocation/scan of a project. Either on the cli -Dsonar.webhooks.project=https://my-jenkins-domain.tld/sonarqube-webhook/ or in sonar-project.properties onar.webhooks.project=https://my-jenkins-domain.tld/sonarqube-webhook/

metalisticpain
  • 2,698
  • 16
  • 26
  • Unfortunately, I am not an admin in SonarQube, only the projects I create. I should have specified that earlier. Thank you – DarkHark Jan 29 '19 at 21:10
  • 2
    Maybe try `-Dsonar.webhooks.project=https://my-jenkins-domain.tld/sonarqube-webhook/` on run? (stab in the dark \) – metalisticpain Jan 29 '19 at 21:29
  • Failing that, the documentation is pretty clear. You may need to have the team that administers your sonarqube add the webhook for you. You can configure up to 20 according to the documentation. – metalisticpain Jan 29 '19 at 21:34
  • Your first comment worked! If you put that in your answer, I'll mark is as the correct one. Thanks! – DarkHark Jan 29 '19 at 22:04
  • I may have jumped the gun on this one. SonarQube reported back the first time I ran the build, but now it's stuck in 'IN_PROGRESS'. Receiving the IN_PROGRESS seems like it is at least hearing back, so I'm not sure why it's getting stuck there. – DarkHark Jan 30 '19 at 15:48
  • 1
    Using https://community.sonarsource.com/t/waitforqualitygate-timeout-in-jenkins/2116 , I was able to figure out that I needed to add a sleep(10) between my script and call to waitForQualityGate() – DarkHark Jan 30 '19 at 16:45
2

I saw a workaround here https://community.sonarsource.com/t/waitforqualitygate-timeout-in-jenkins/2116/9

Adding a sleep in between is solving the issue for me

        }
        sleep(10)
        timeout(time: 5, unit: 'MINUTES') {
jandry
  • 170
  • 2
  • 7
  • 1
    Better use webhook - if your sonar will be busy and return "IN-PROCESS" in 10s it will fail again – lojza Mar 25 '21 at 11:51