8

I am executing sonar through Jenkins build, there is use-case where 'src' location changes depending upon build so i want to use environment variable for specifying 'src' path.

For example in sonar-project.properties i want to specify as shown below:

src= c:/project/workspace instead i want to use src =${SONAR_RUN_WORKSPACE}

G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
Ganesh Rao B
  • 461
  • 2
  • 8
  • 23

3 Answers3

12

Environment variables are supported in sonar-project.properties starting from SonarQube Scanner version 2.9 (see SQSCANNER-9).

So, this should work now:

sonar.sources=${env.SONAR_RUN_WORKSPACE}
Oleksandr Shmyrko
  • 1,720
  • 17
  • 22
6

You cannot pass env variable into sonar-project.property file.

  • If you are creating sonar-project.property file from jenkins, you can use build with param.

    (or)

  • Create a sonar-project.properties without sonar.source.

  • Create build with param variable {path}, get the src path from user for each build,

  • Map the variable with sonar.source=${path} in sh.

  • Append sonar.source to sonar-project.properties for each build in sh using (>>) or cmd from jenkins.

    eg) sonar.source="path" >> sonar-project.properties

finally work space contains sonar-project.properties file with src path.

diamond
  • 171
  • 6
2

Nope, environment variables are not supported in sonar-project.properties. In any case, back to your use-case: you better use a relative path for sonar.sources so that, wherever analysis is ran (i.e. whatever workspace with Jenkins), sources are always found at the expected place (from the project's base directory).

Nicolas B.
  • 7,245
  • 17
  • 29