2
votes

We use VSTS build with standard SonarQube build steps:

  • SonarQube for MsBuild - Begin Analysis
  • ... build
  • SonarQube for MsBuild - End Analysis

Some time after build I can see Analysis results in SonarQube - whether it Passed or Failed quality gate. But the VSTS build is successful even if quality gate is Failed.

Is there a way to fail a VSTS build if quaility gate is failed?

Following this: http://docs.sonarqube.org/display/SONAR/Breaking+the+CI+Build I've tried looking for report-task.txt file, but I can't see it anywhere.

I can probably just run MSBuild.SonarQube.Runner.exe as command-line build step, as described here: http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Scanner+for+MSBuild#AnalyzingwithSonarQubeScannerforMSBuild-AnalyzingfromtheCommandLine

But I thought I should first try standard Build Steps for SonarQube

2

2 Answers

4
votes

Here is a link to failing the build on quality gate violations with 5.3 or later, it uses the SonarQube for MSBuild - Begin Analysis task

https://blogs.msdn.microsoft.com/visualstudioalm/2016/02/11/use-sonarqube-quality-gates-to-control-your-visual-studio-team-services-builds/

This updated task is not available with TFS 2015 Update 1 but is available in Update 2 RC1 and VSTS (VSO).

Regards, Wes

1
votes

I too had this requirement to fail the Build if sonar quality gate fails. I did created a power shell task after sonarqube display task. Here is the script to find the status:

function Get-SonarQubeStatus() {

  # Step 1. Create a username:password pair
  $credPair = "username:password"

  # Step 2. Encode the pair to Base64 string
  $encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))

  # Step 3. Form the header and add the Authorization attribute to it
  $headers = @{ Authorization = "Basic $encodedCredentials" }

  # Step 4. Make the GET request
  $responseData = Invoke-WebRequest -Uri https://localhost/api/qualitygates/project_status?projectKey=<projectkey> -Method Get -Headers $headers -UseBasicParsing

  #write-host $responseData.content

  $x = $responseData.content | ConvertFrom-Json
  $sonarQualityGateResult = $x.projectStatus.status

  if($sonarQualityGateResult -eq "ERROR")
    {
        write-host "CI failed due to Sonarqube quality Gate"
        exit 1
    }

}