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

  • 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:


Inside the Jenkins file I want the following line:$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.

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):


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