Question

Is there a way to use sonar-runner to dynamically update the project version for every build without having to manually update the sonar-project.properties file each time? (note: this is for a non-mavenized .NET MVC app)

Was trying to parameterize the properties file having it reference an environment var set automatically

Added in sonar-runner.bat

for /f "delims=" %%i in ('hg identify -n') do set HG_LOCAL_REV=%%i

Modified sonar-project.properites

sonar.projectVersion=%HG_LOCAL_REV% #don't want to have to manually update this

Would be great if we could just pass the projectVersion as a parameter for sonar-runner.bat

Was it helpful?

Solution 2

I decided to create a simple template file as a quick work around. Just search/replace for a unique string in the template file and save to a new sonar-project.properties file for the project.

Here's some sample code in case anyone's interested:

$hg_version = hg identify -n
(get-content {C:\dev\myProj\sonar-project.template | foreach-object {$_ -replace "my_proj_version", $hg_version}) | set-content c:\dev\myProj\sonar-project.properties
sonar-runner

Hope this is helpful for anyone else finding this.

OTHER TIPS

You can remove "sonar.projectVersion" altogether from sonar-project.properties, and pass the value as a command line argument instead, when launching the sonar-runner:

sonar-runner -Dsonar.projectVersion="something"

Note: Sonar-runner (mentioned in the original post) is deprecated and replaced with SonarQube Scanner.

This is a working recipe for an iOS (Swift) project with TeamCity (version 2017.1.4) and SonarQube scanner (version 2.9.0.670). I'm adding it here as a future reference to myself and in case someone else needs it:

sonar-project.properties

  • Add line: sonar.projectVersion=${env.APP_VERSION}-SNAPSHOT

TeamCity Build Config, "Parameters"

  • Define an environment variable called: APP_VERSION + add other parameters such as sonar.host.url to avoid coupling source code with the instance version, passwords, etc.

TeamCity Build Config, Build step 1 (Command line)

  • Add script (be sure to replace $MY_APP_FOLDER$ with the real name of the folder: echo "##teamcity[setParameter name='env.APP_VERSION' value='$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$MY_APP_FOLDER$/Info.plist")']"

TeamCity Build Config, Build step 2 (Command line)

  • Add script (assuming develop is the branch that is reported to SonarQube): if [ %teamcity.build.branch% = "develop" ] then SONAR_RUNNER_OPTS=-Xmx1024m sonar-scanner -e \ -Dsonar.host.url=%system.sonar.host.url%\ -Dsonar.login=%system.sonar.login%\ -Dsonar.password=%system.sonar.password%\ -Dsonar.cfamily.build-wrapper-output.bypass=true fi
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top