Skip to content

Instantly share code, notes, and snippets.

@wilbit
Last active May 30, 2023 20:58
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wilbit/4eba5b9bbf9dced260fdde7e8242964c to your computer and use it in GitHub Desktop.
Save wilbit/4eba5b9bbf9dced260fdde7e8242964c to your computer and use it in GitHub Desktop.
It removes sonar.branch.name property in Azure Pipeline. Put it somewhere between your SonarQubePrepare and SonarQubeAnalyze tasks.
- powershell: |
$params = "$env:SONARQUBE_SCANNER_PARAMS" -replace '"sonar.branch.name":"[\w/,-.]*"\,?'
Write-Host "##vso[task.setvariable variable=SONARQUBE_SCANNER_PARAMS]$params"
@carlosen14
Copy link

carlosen14 commented Aug 10, 2020

how to use this or similar in vmImage: 'ubuntu-latest'?

EDIT:

Tried it myself, looks like it works same, it solved the error but it crashed with a different one on Publish task:

Starting: SonarQubePublish
==============================================================================
Task         : Publish Quality Gate Result
Description  : Publish SonarQube's Quality Gate result on the Azure DevOps build result, to be used after the actual analysis.
Version      : 4.8.1
Author       : sonarsource
Help         : Version: 4.8.1. [More Information](http://redirect.sonarsource.com/doc/install-configure-scanner-tfs-ts.html)
==============================================================================
##[error][SQ] Task failed with status FAILED, Error message: Unrecoverable indexation failures: 1 errors among 1 requests
Finishing: SonarQubePublish

@wilbit
Copy link
Author

wilbit commented Aug 10, 2020

Yeah, that is a different one.
According to my previous experience, it could be related to memory on server (disk or ram). Have a look on https://community.sonarsource.com/t/analysis-failed-with-unrecoverable-indexation-failures/12329/2

@carlosen14
Copy link

Thanks for answering, but it wasn't for disk space, as pipeline agent is running on Azure free agents and the limit the free-space on disk to 10 GB.

anyway I made it work removing some additional properties that were crashing and by adding this task

      - task: Bash@3
        displayName: Set Sonar Qube params
        inputs:
          targetType: 'inline'
          script: 'export SONAR_SCANNER_OPTS="-Xmx512m"'

By the way, this is the only way I could pass variables into sonar properties, do it via inline wasn't parsing the properties and assigned literally the string '$(someVar)' instead the var content.

      - bash: | # Grab the package version
          version=`node -p "const p = require('./package.json'); p.version;"`
          echo "##vso[task.setvariable variable=packageVersion]$version"
          user="$(git log -1 --pretty=format:%an | perl -pe '$')"
          echo "##vso[task.setvariable variable=gitUser]$user"
          date_Proy=`date -I`
          echo "##vso[task.setvariable variable=dateProy]$date_Proy"
          build_ID="$(Build.BuildId)"
          echo "##vso[task.setvariable variable=customBuildID]$build_ID"
          echo $version $user $date_Proy $build_ID
      - bash: | # Insert variables into sonar-project.properties
          # echo "sonar.projectDate=$(dateProy)" >> sonar-project.properties

          echo "sonar.projectName=mod-seguridad-backend" >> sonar-project.properties
          echo "sonar.projectKey=$(SONAR_BACKEND_KEY)" >> sonar-project.properties
          echo "sonar.projectVersion=$(packageVersion)-$(Build.SourceBranchName)-$(gitUser)-$(customBuildID)" >> sonar-project.properties

          echo "sonar.analysis.revision=$(Rev:.r)" >> sonar-project.properties
          echo "sonar.analysis.branch=$(Build.SourceBranchName)" >> sonar-project.properties
          echo "sonar.analysis.buildid=$(Build.BuildId)" >> sonar-project.properties
      - task: SonarQubePrepare@4
        displayName: Sonar Qube Prepare
        inputs:
          SonarQube: 'sonar-ragasa-dev'
          scannerMode: 'CLI'
          configMode: 'file'
          configFile: 'sonar-project.properties'

@wilbit
Copy link
Author

wilbit commented Aug 12, 2020

Huh, interesting.

@poychang
Copy link

By default, SonarQube CE only support analysis "master" branch. But you can set default branch on Azure DevOps to "develop" or any other branch. That make SonarQube treat the branch like "master".

@wilbit
Copy link
Author

wilbit commented Sep 23, 2021

@poychang, it does not provide the ability to analyse different branches of the same Git repository using, for instance, different SonarQube projects (I don't believe it is a good idea to change a default branch each time, it event won't work for running several builds on different branches simultaneously).

The gist does (at least it did when I used it).

@poychang
Copy link

You are right. This gist is more generic use and it’s work!
For my scenario, I only scan code on DEV branch. We only commit new code to DEV and after that we use PR. No new code, no need to scan. So, I choose change default branch to reach that goal.

@wilbit
Copy link
Author

wilbit commented Sep 28, 2021

👍

@dasMulli
Copy link

If anyone is looking for a non-windows variant, this works fine on ubuntu images:

- script: |
    FILTERED_PARAMS=$(echo $SONARQUBE_SCANNER_PARAMS | sed 's/"sonar.branch.name":"[^"]*"\,//g')
    echo "##vso[task.setvariable variable=SONARQUBE_SCANNER_PARAMS]$FILTERED_PARAMS"
  displayName: Filter out non-CE sonar parameters

@wilbit
Copy link
Author

wilbit commented Nov 12, 2021

If anyone is looking for a non-windows variant, this works fine on ubuntu images:

I use powershell on Linux machines (Azure Pipelines Microsoft-managed agents) and it works well.

- script: |
    FILTERED_PARAMS=$(echo $SONARQUBE_SCANNER_PARAMS | sed 's/"sonar.branch.name":"[^"]*"\,//g')
    echo "##vso[task.setvariable variable=SONARQUBE_SCANNER_PARAMS]$FILTERED_PARAMS"
  displayName: Filter out non-CE sonar parameters

@dasMulli , for your script I would use bash instead of script because script on Windows machines uses cmd.exe and the commands from your script won't work via cmd.exe, so, there is not reason to use a generic script task.

@ElaineBSchwaner
Copy link

Hello, @wilbit,
How can I use this with Mac agent?
I tried and the error was:
/Users/runner/work/_temp/ebba7608-9021-4e4f-9953-4dd7bc559b34.sh: line 1: =: command not found
/Users/runner/work/_temp/ebba7608-9021-4e4f-9953-4dd7bc559b34.sh: line 2: Write-Host: command not found

@wilbit
Copy link
Author

wilbit commented Jan 22, 2023

@ElaineBSchwaner , for Mac agent you better use @dasMulli 's version for Bash shell
The final version looks like this

- bash: |
    FILTERED_PARAMS=$(echo $SONARQUBE_SCANNER_PARAMS | sed 's/"sonar.branch.name":"[\w/,-.]*"\,//g')
    echo "##vso[task.setvariable variable=SONARQUBE_SCANNER_PARAMS]$FILTERED_PARAMS"
  displayName: Filter out non-CE sonar parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment