Search code examples
jenkinssonarqubeenvironment-variables

what is the right way to get source branch name in the jenkins pull request builder job


i want to use branch analysis feature on sonarqube on Jenkins pull request builder.

My repository has a sonar.gradle file. If I set properties sonar.branch.name = "featurebranch" and sonar.branch.target = "master" and create a PR, I see sonarqube analysis for featurebranch is updated

I'd like to use environment variable ghprbSourceBranch (available as environment variable according to link) and set property sonar.branch.name = $ghprbSourceBranch in sonar.gradle file My PR build is failing with this line of code in sonar.gradle file

property "sonar.branch.name", $ghprbSourceBranch

I tried the following and they fail too

property "sonar.branch.name", ${ghprbSourceBranch}

property "sonar.branch.name", ${env.ghprbSourceBranch} property "sonar.branch.name", System.getenv("CHANGE_BRANCH") property "sonar.branch.name", System.getenv("TARGET_BRANCH") property "sonar.branch.name", System.getenv("BRANCH_NAME")


Solution

  • I'm using the Pull-Request analysis feature for Sonar Enterprise 7.9 with the following parameters in Jenkins, but with the Sonar CLI to run the scan:

    sonar.pullrequest.key=${env.CHANGE_ID}
    sonar.pullrequest.base=${env.CHANGE_TARGET}
    sonar.pullrequest.branch=${env.CHANGE_BRANCH}
    

    Necessary for PR decoration along with SonarCloud scanner Github App

    sonar.pullrequest.provider=GitHub
    sonar.pullrequest.github.repository=yourOrg/yourRepo
    

    See also Sonar Docs.