SonarScanner automatic trigger when a pull request is created

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension) => SonarQube Version 8.2.0.32929

  • what are you trying to achieve => As soon as we create a new pull request in bitbucket datacenter (Bitbucket v6.6.1), how do we automatically trigger a scan and display the results on sonarqube.

  • what have you tried so far to achieve this
    I am able to do it manually when I have the following lines in the Jenkins file:

    -Dsonar.pullrequest.branch=$env.BRANCH_NAME
    -Dsonar.pullrequest.base=develop
    -Dsonar.pullrequest.key=1

Inside the Jenkins file I want the following line:
-Dsonar.branch.name=$env.BRANCH_NAME

If I understand this correctly, we can have only one of the above two configuration. I do not want to change the configuration inside the Jenkinsfile every time a developer creates a pull request in bitbucket.

Hi,
Just FYI, assuming that you are using the Bitbucket Branch Source plugin in Jenkins, SonarQube will be able to automatically detect branches and pull requests starting in v8.3.

Meanwhile, you can do the following.

if $env.CHANGE_ID is defined (meaning it’s a P/R):

-Dsonar.pullrequest.branch=$env.CHANGE_BRANCH
-Dsonar.pullrequest.base=$env.CHANGE_TARGET
-Dsonar.pullrequest.key=$env.CHANGE_ID

Otherwise if $env.BRANCH_NAME is defined (for branches):

-Dsonar.branch.name=$env.BRANCH_NAME
1 Like