We currently have a jenkins pipeline using Multibranch Workflow plugin
. Each git branch execute a sonarqube analysis creating a sonarqube project using the sonar.branch
property. This is very useful because each branch is being analyzed before merge it, the problem appear when a branch is merged with master and disappear on GIT, the project continues on sonarqube and need to be deleted manually.
Is There a way to do it automatically? or any other recomendation?
Asked
Active
Viewed 5,457 times
10

G. Ann - SonarSource Team
- 22,346
- 4
- 40
- 76

F.Rosado
- 487
- 4
- 20
-
have you got the solution if so, please share comprehensively – Gentle Jun 27 '19 at 05:56
-
No yet, the only solution found is to use commercial solutions. I'm still looking for a different solution for community version. – F.Rosado Jun 27 '19 at 08:42
-
I have found one solution and posted as an answer below, check if it helps. – Gentle Jul 04 '19 at 08:19
3 Answers
4
The best way to remove SonarQube branch projects is to not push them to the server in the first place. Here are your options:
- use SonarLint to spot issues directly in your IDE
- if you're using GitHub, use the GitHub Plugin
- if you're using BitBucket, use this plugin or this one
At SonarSource, we use SonarLint and the GitHub Plugin on a daily basis. One last solution is to delete manually the project using the api/projects/delete web service.

Teryk - SonarSource
- 994
- 5
- 12
-
1If you use a continuous integration system like Jenkins, when you create a branch on git from master the jenkinsfile it is also copied, avoid sonarque analysis imply modify this file. In some cases the developer forget this step. – F.Rosado Feb 07 '17 at 11:42
-
2We have the same "problem" with Travis. That's why our script do some conditional checks (are we on master branch or not) before running the SonarQube analysis. – Julien H. - SonarSource Team Feb 08 '17 at 12:40
-
1I want my branch to be on sonar but I would like them to be deleted when branch is merged / deleted. So you use a git hook to call sonar api / delete on branch merge / delete ? – TheBakker Oct 17 '18 at 09:57
3
If you happen to be using Bitbucket Server and the Sonar for Bitbucket Server add-on, there's an automatic way to perform this cleanup. To enable this setting from Bitbucket Server, follow the 3 steps shown in the screenshots below.

ASnyder
- 83
- 5
-
Perhaps is the best solution if you can buy the plugin, it is not always possible. – F.Rosado Mar 22 '17 at 08:09
-
if you cannot invest 10 bucks / 10 users you probably also should'nt be investing time into continuous integration / code QA. Extreme programming and a very simple SCM is probably all you need for such a project. Just sayian. SonarQube and the likes make sense in business-oriented teams only - for everything else a simple cronjob might do the trick. – specializt Jun 20 '17 at 09:50
-
1My case is different, the number of users for bitbucket is large (+2000) but the number of sonarqube users is small (less than 100). Perhaps a custom scheduled task that check projects and repositories is a solution. – F.Rosado Aug 08 '17 at 16:25
2
You can define a method as below, to do the job and then call the method whenever you need according to your need or on some event like git branch merged/removed, etc.
def deleteSonarProject() {
def sonarToken = "-your admin credential token generate it from sonarServer-"
def url = "http://yourBaseURL:portNumber/api/projects/delete"
sh "curl -u ${sonarToken}: ${url} -d 'project=projectKey'"
}

Gentle
- 519
- 6
- 12
-
interesting. I should launch this script when the branch has been deleted o merged. I will review that solution. Thanks a lot. – F.Rosado Jul 23 '19 at 13:30
-
1
-