SonarCloud Prepare Analysis Task uusing environment variables

Hi All

I am trying to use the Sonar SonarCloudPrepare@1 task in Azure DevOps within an extension i have created.
The purpose is to use the extension to ensure that all projects will use SonarQube for analysis.
When I hard code the values for the inputs to the task all works fine - but then the analysis will only be done on the project with key(myAppKey).

  • task: SonarCloudPrepare@1
    displayName: ‘SonarQube - Initialize’

    Hard coded values work - but would be project specific when the extension runs

    We want to be able to specify different projects at this point

    SonarCloud: SonarQube
    organization: myOrg
    projectKey: myAppKey
    projectName: myAppKey

So i have a csv file for each application repo with a different project key.
The valuses are read from the file and set as follows:
Write-Host “##vso[task.setvariable variable=sonarProjectKey]$sqPK” where $sqPK is read from a csv file

The value is picked up correctly as i output it (in another task) using
Write-Host “Using key: $env:sonarProjectKey”

However when i try to use these environment variables the task does not work…

  • task: SonarCloudPrepare@1
    displayName: ‘SonarQube - Initialize’
    SonarCloud: ‘$(env:sonarConnection)’
    organization: ‘$(env:sonarOrganization)’
    projectKey: ‘$(env:sonarProjectKey)’
    projectName: ‘$(env:sonarProjectName)’

Maybe my syntax is wrong.
Any help appreciated.
Thanks Will

Hi @WillMacaulay welcome to the community !

Have you tested simply $(sonarProjectKey) ?

Hi Mickael. Thanks for the welcome. Got it to work with the organization, project key and project name. Had to hard code the SonarCloud value - could not get it to work with environment variable.

So this works:

  • task: SonarCloudPrepare@1
    displayName: ‘SonarQube - Initialize’

    Cannot set the SonarCloud value using a variable for some reason!!!

    SonarCloud: SonarQube

    SonarCloud: $(SONARCONNECTION) … Cant use !!! ???

    Others can be set by variable

    organization: $(SONARORGANIZATION)
    projectKey: $(SONARPROJECTKEY)
    projectName: $(SONARPROJECTNAME)

Ah yeah indeed, this is not really a variable, but more a link to an Object, that’s why. And i don’t see why you’ll need to have it variable since i think all your projects will use the same service connection ? Or maybe you have another use case.

Glad it works :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.