SonarQube PR decoration is not working for github with Jenkins

Must-share information (formatted with Markdown):

  • SonarQube 9.2
  • We are trying to enable PR decoration for Git repository. We are doing the sonar-scan with Jenkins pipeline. Following this document - GitHub Integration | SonarQube Docs
  • what have you tried so far to achieve this
    We tried to run the scan manually and it’s working fine but when we are trying to do it with Jenkins it’s not working. Since we have created the project manually, we have also tried adding parameters mentioned in this document - SonarQube

This is our jenkins pipeline stage:

stage('SonarQube Analysis') {
    container('jdk') {
    // Define sonarqube analysis parameters
    def sonarParams = ""
    def scannerHome = tool 'SonarScanner';
    if (env.CHANGE_ID) {
        sonarParams = "-Dsonar.pullrequest.key=${env.CHANGE_ID}"
        sonarParams += " -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH}"
        sonarParams += " -Dsonar.pullrequest.base=${env.CHANGE_TARGET}"
    } else {
        sonarParams = "-Dsonar.branch.name=${env.BRANCH_NAME}"
    }
      withSonarQubeEnv() {
        sh "${scannerHome}/bin/sonar-scanner ${sonarParams}"
       }
   }
  }

The values of Env variables are not rendering during the scan but when we specify the hard code values it works fine. Could you please suggest what if these are the correct environment variables ?
Since we are using a multi branch pipeline so can’t hard code the parameter values.

Hey there.

You really shouldn’t have to specify anything in SonarQube v9.2 if you’re using the GitHub Branch Source plugin.

By default, the scanner checks that JENKINS_HOME or JENKINS_URL exists. If so, it checks that CHANGE_ID and CHANGE_TARGET exist and with the help of CHANGE_BRANCH sets sonar.pullrequest.key, sonar.pullrequest.base and sonar.pullrequest.branch respectively.

So you might want to check that your build has all these required variables, so that you can skip manual configuration altogether.

If environment variables aren’t being passed into your container… that’s really a Jenkins/Docker issue rather than a SonarQube one. Do other enviornment variables render, even if you just do an echo?

Hi Colin,

Other environment variables like BRANCH_NAME is rendering but the value is not the original branch name. It’s giving the value like PR-123 instead of actual branch name like sonar-test.