1

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
Krishnan
  • 15
  • 1
  • 3
  • can you please provide your jenkins configuration with screenshots. and elaborate if you are using a sonar-project.properties file, or if you inject other settings? – Simon Schrottner May 07 '18 at 11:08
  • You can use a shell script to change the content of a text file. You can create a variable and inject it into the sonar properties file and get it done. – Vighnesh Pai May 07 '18 at 11:21

3 Answers3

3

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}"/>
Fidel
  • 977
  • 1
  • 6
  • 13
0

@Simon Schrottner

We have used following in Jenkins (Note: we have used Gradle)

sonar.projectKey=$JOB_NAME
sonar.projectName=$JOB_NAME
sonar.projectVersion=$BUILD_NUMBER
sonar.exclusions=vendor/**, storage/**, resources/**
sonar.language=java
sonar.sources=$WORKSPACE
sonar.sourceEncoding=UTF-8
sonar.java.binaries=$WORKSPACE/build/classes/java/main
0

In my case (android project). I'm using either this 2 approach instead

Using android configuration property

android {}

dependencies {}

sonarqube {
    properties {
        // ...

        // get the versionCode, buildVariantName, versionName 
        // or anything you want to configure.
        property("sonar.projectVersion", "${project.android.defaultConfig.versionName}")

        // ...
}

Using git tag

This approach using git describe --always, it will get the latest tag that connected to the branch:

$: ./gradlew sonarqbue -DprojectVersion=`git describe --always`

The open & close ` (tick) in the above ./gradlew arguments mean to execute a command. In this case git describe --always

The above approach also safer if we want to change our repository or CI/CD platform (e.g: migrating to use Gitlab CI) or you might want to check for predefined environment variables on the specific CI/CD platform to satisfy projectVersion dynamic value.

Inspired by this answer here

mochadwi
  • 1,190
  • 9
  • 32
  • 87