Using Powershell to Execute sonar-scanner in a Jenkins Declarative Pipeline

1,727 views
Skip to first unread message

joe.d.cava...@gmail.com

unread,
Oct 12, 2017, 11:56:14 AM10/12/17
to SonarQube
Hello,

I am attempting to use powershell rather than bat for withSonarQubeEnv() in a Declarative Pipeline for Jenkins.

NOTE: I use the command line for an Angular Project to be submitted with Typescript files (and the corresponding linting file, which is not included in the below scripts yet). I prefer not having a properties file since all the applications will be set up similar and I would like to make the below a global library eventually. 

               stage('SonarQube') {
        steps {
    withSonarQubeEnv('SonarQube Corp Server') {

                            /* powershell '''
                                sonar-scanner -Dsonar.login=$env:SONAR_AUTH_TOKEN -Dsonar.host.url=$env:SONAR_HOST_URL `
                                    -Dsonar.projectKey="NAWM_$env:PROJECT_NAME" -Dsonar.projectName="NAWM_$env:PROJECT_NAME" -Dsonar.projectVersion=$env:ComponentVersion `
                                    -Dsonar.projectBaseDir=$env:WORKSPACE -Dsonar.home=$env:WORKSPACE `
                                    -Dsonar.sourceEncoding=UTF-8 `
                                    -Dsonar.exclusions="**\\node_modules\\**,**\\dist\\**,**\\src\\**\\*.spec.ts,**\\app\\swagger\\**" `
                                    -Dsonar.sources=src\\ -Dsonar.verbose=true
                            ''' */

                            bat '''
                                sonar-scanner -Dsonar.login=%SONAR_AUTH_TOKEN% -Dsonar.host.url=%SONAR_HOST_URL% ^
                                    -Dsonar.projectKey="NAWM_%%PROJECT_NAME%" -Dsonar.projectName="NAWM_%PROJECT_NAME%" -Dsonar.projectVersion=%ComponentVersion% ^
                                    -Dsonar.projectBaseDir=%WORKSPACE% -Dsonar.home=%WORKSPACE% ^
                                    -Dsonar.sourceEncoding=UTF-8 ^
                                    -Dsonar.exclusions="**\\node_modules\\**,**\\dist\\**,**\\src\\**\\*.spec.ts,**\\app\\swagger\\**" ^
                                    -Dsonar.sources=src\\ -Dsonar.verbose=true
                            ''' 
    }
    }
    }


What is interesting is the powershell version of the script has errors with reading in the first parameter of the function vs the bat file (which executes). I get this regardless of the first option I use. The error for the powershell version is as follows : 

[SonarQube] Injecting SonarQube environment variables using the configuration: SonarQube Corp Server
[Pipeline] [SonarQube] {
[Pipeline] [SonarQube] powershell
[SonarQube] [TradeCalc_CDA_Jenkinsfile-4HEAPVKV77ZQKL5FQ27QFBSLPAXPYSZDVQTW2RG5FTR7NLKQBDCQ] Running PowerShell script
[SonarQube] sonar-scanner : ERROR: Unrecognized option: .login=******
[SonarQube] At C:\Jenkins\workspace\TradeCalc_CDA_Jenkinsfile-4HEAPVKV77ZQKL5FQ27QFBSLPAXPYSZDVQTW2RG5FTR7NLKQBDCQ@tmp\durable-c69b
[SonarQube] 0d43\powershellScript.ps1:2 char:33
[SonarQube] + ...             sonar-scanner -Dsonar.login=$env:SONAR_AUTH_TOKEN -Dsonar ...
[SonarQube] +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[SonarQube]     + CategoryInfo          : NotSpecified: (ERROR: Unrecogn...5f38c3c62dddd6f:String) [], RemoteException
[SonarQube]     + FullyQualifiedErrorId : NativeCommandError
[SonarQube]  
[SonarQube] INFO: 
[SonarQube] INFO: usage: sonar-scanner [options]
[SonarQube] INFO: 
[SonarQube] INFO: Options:
[SonarQube] INFO:  -D,--define <arg>     Define property
[SonarQube] INFO:  -h,--help             Display help information
[SonarQube] INFO:  -v,--version          Display version information
[SonarQube] INFO:  -X,--debug            Produce execution debug output


Thoughts on why this might be? I didn't know if this would be a SonarQube plugin issue or the powershell maintainers for Jenkins. I thought I would try here first in case I am missing something obvious. 

Thanks!

Joe Cavanaugh

unread,
Oct 19, 2017, 10:04:12 AM10/19/17
to SonarQube
Looks like Sonarqube does not make the URL available to powershell at all in Jenkins. The below code results in an empty url and token when executing from a Powershell command within the UI as well.
How would I open a support request ticket for an issue like this?

Write-Output "Sonar url - $env:SONAR_HOST_URL"
SonarQube.Scanner.MSBuild.exe begin /k:"$env:NAMEOFPRODUCT" /n:"NAWM_$env:NAMEOFPRODUCT" /v:"$env:MAJORVERSION.$env:BUILD_NUMBER" /d:sonar.host.url=$env:SONAR_HOST_URL /d:sonar.login=$env:SONAR_AUTH_TOKEN
dotnet msbuild /t:Rebuild
SonarQube.Scanner.MSBuild.exe end /d:sonar.host.url=$env:SONAR_HOST_URL /d:sonar.login=$env:SONAR_AUTH_TOKEN
Reply all
Reply to author
Forward
0 new messages