Search code examples
jenkinssonarqubesonarqube-scan

how to change sonarqube projectVersion number dynamically in jenkins


i tried updating sonarqube version dynamically in jenkins by adding sonar.projectVersion=${project.version} suggested at https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle or sonar.projectVersion=${$APP_BUILD_NUMBER} here APP_BUILD_NUMBER environmental variable but no use it considered as is version name.

Full Analysis property

# required metadata
sonar.projectKey=myproject
sonar.projectName=myproject
sonar.projectVersion=2.0.2
sonar.sourceEncoding=UTF-8


# path to source directories (required)
sonar.sources=src/main/java

# List of the module identifiers
sonar.modules=app,ui
ui.sonar.projectBaseDir=ui
# Properties can obviously be overriden for
# each module - just prefix them with the module ID
app.sonar.projectName=App

# Uncomment this line to analyse a project which is not a java project.
# The value of the property must be the key of the language.
sonar.language=java

# java version used by source files:
sonar.java.source=1.8

Solution

  • More detailed question would have helped to answer better. Here I made assumptions to answer better.

    I assumed, dynamic version name that you are trying to update is jenkins build number. which is accessible via jenkins environment variable ${BUILD_NUMBER}. You can directly use this in "Execute Shell" (i.e) echo ${BUILD_NUMBER} will work.

    You can use other environment variables too. ex: ${BUILD_TAG}. you can find the list of jenkins environment variables are available http://JENKINS-URL/env-vars.html/

    These environment variables are available to your build script (i.e) ANT, Maven, Gradle, etc as well. Just make sure, you have used proper way to access environment variables inside the build script. For Ant:

    <property environment="env"/>
    <property name="sonar.projectVersion" value=${env.BUILD_NUMBER}"/>