Setting up Code Versioning with Gitlab CI

Hi,
I am using Sonar Developer Edition version 8.4.1.
I am confused on how best to use the -Dsonar.projectVersion scanner option in GitLab CI. I would like to use the last commit short sha to our Master branch as the version. So, the new code period should be set to any code since that last Master commit. I really am lost trying to find the best way to do this since Developer edition has branch analysis and merge request support. The code bases are primarily Go, Python, and NodeJS.

I tried a bit of code where we configure the scanner options in CI:

if [[ "$CI_COMMIT_BRANCH" == master ]] || [[ "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" == master ]]; then
  COMMAND="$COMMAND
      -Dsonar.projectVersion=$CI_COMMIT_SHORT_SHA"
fi

But I am not getting the results as I would expect, I only see “Master” in the activity. And most of the project’s new code period is not getting any version at all.

Has anyone setup versioning in a similar matter in GitlabCI? Or any best practices for setting up versions with branches, etc?

Thanks!

Hi,

Welcome to the community!

I have the sense that a couple things are tangled together here.

First, I would not advise using the commit SHA as the project version number. The assumption behind sonar.projectVersion is that it will change for each deployment to production. E.G. when we released SonarQube 8.8, we bumped the project’s on sonar.projectVersion to 8.9.

And speaking of 8.8, your version has some age on it at this point. I would urge you to upgrade to the latest version to get the best experience.

Regarding your branches’ new code period(…?) I think what you’re looking for is the Reference Branch setting for New Code Period.

 
HTH,
Ann