-->

Execute sonar-runner in TFS build

2020-07-27 04:26发布

问题:

How do I make the command to run sonar-runner at the end of the build TFS (Team Foundation Server) in continuous integration? Info: TFS 2013, Windows Server 2008 R2, sonar-runner 2.3

回答1:

The only way to execute arbitrary commands as part of the build process is to add an InvokeProcess activity to the BuildProcessTemplate. This isn't as difficult as it may seem at first.

  1. Make a copy of the DefaultTemplate.xaml which is located in the /BuildProcessTemplates folder of your team project. Name it whatever you want - SonarRunnerTemplate.xaml or something.

  2. Open up the new template in Visual Studio

  3. Hit "Collapse All" at the top right to ease navigation to the proper activity.

  4. Browse to Process > Sequence > Run On Agent > Try Compule, Test, and Associate Changesets and Work Items > in the Finally clause > Revert Workspace and Copy Files > If DropBuild And DropLocation is Set

  5. In the "Then" box you will see a Copy Files to Drop Location activity. Move this into the "Else" box temporarily.

  6. Add a Sequence activity from the Toolbox into the (now empty) Then box

  7. Move the Copy Files to Drop Location (which we moved earlier) back into the Sequence activity you just added to the Then box.

  8. Add a InvokeProcess activity from the Toolbox to run AFTER the "Copy Files to Drop Location" activity

  9. Open the Properties for the InvokeProcess activity. The FileName field is the command which you need to execute. In this case, the command to execute your Sonar runner. It might be easiest to write a batch or powershell script which runs Sonar... and then invoke that.

  10. Save the build process template and add it to TFS. You can then use this template for your build definition.

One issue with this approach is that the build will not "finish" until after the InvokeProcess command has returned. So if your sonar run takes a really long time (I know I have seen it take over 24 hours for a large codebase) the build will take forever to complete. This can be mitigated by having your script spawn off the Sonar process asynchronously.



回答2:

There is now an official way to get MsBuild and TeamBuild to work with SonarQube, as part of a collaboration between Microsoft and SonarQube. You can find all the details on the SonarQube website:

http://www.sonarqube.org/announcing-sonarqube-integration-with-msbuild-and-team-build/

And on the Microsoft ALM website:

http://blogs.msdn.com/b/visualstudioalm/archive/2015/04/28/technical-debt-management-announcing-sonarqube-integration-with-msbuild-and-team-build.aspx

There is also documentation prepared by the ALM rangers that describe how to install SonarQube and integrate it with an existing TFS server:

https://vsarguidance.codeplex.com/downloads/get/1452516



标签: tfs sonarqube