2

I went through the following documentation: https://docs.sonarqube.org/latest/branches/overview/, but I am not able to make SonarQube detect other branches than master branch (I am using a developer edition). I've passed the following parameter as a variable to pom.xml properties: <sonar.branch.name>${git.branch}</sonar.branch.name>.

SonarQube list of branches

Community
  • 1
  • 1
Styx1
  • 139
  • 2
  • 7
  • 1
    but it seems to be listed below short lived branches? just your placeholder was not set? – wemu Dec 13 '19 at 20:02
  • Yes, but I need to refer to my branch – Styx1 Dec 15 '19 at 14:00
  • The branch that you build is known in your CI server? Via some environment variable? Then it should be enough to just pass that property into the build: "mvn install -Dgit.branch=yourbranchname" – wemu Dec 15 '19 at 15:08
  • the variable you used seems to come from a plugin? In that case this thread might be of some help as well: https://stackoverflow.com/questions/21553304/put-current-git-branch-to-project-version – wemu Dec 15 '19 at 15:20
  • This is the plugin's documentation: https://github.com/git-commit-id/maven-git-commit-id-plugin/blob/master/maven/docs/using-the-plugin.md. Yes, I saw that question before, but I need to make sonar automatically detect the name of my branch. – Styx1 Dec 16 '19 at 09:58
  • I'm using Jenkins as CI engine. – Styx1 Dec 16 '19 at 10:12
  • I have imported these plugins in my pom.xml file: https://pastebin.com/QCUvbkK8 Maybe the problem is in the phase of execution – Styx1 Dec 16 '19 at 11:02
  • I want SonarQube to detect the branch name like this from my console: https://ibb.co/gWBy23K, but Jenkins displays it like this when running a job: `[INFO] Branch name: ${git.branch}, type: short living` – Styx1 Dec 16 '19 at 11:28
  • so currently it works only locally but not on jenkings? Does the jenkings clone contain that .git directory on the build agent? – wemu Dec 16 '19 at 18:41
  • I figured out today it's not up to this git-commit-id plugin, but rather to Jenkins pipeline. Now, I have to figure out how to modify Jenkinsfile so this can work. – Styx1 Dec 16 '19 at 19:49

1 Answers1

0

So, at the end I figured out it's up to Jenkins pipeline, which was causing problems. At the end, I had to modify steps in my Jenkinsfile in order for SonarQube to detect my branches.

steps {
    script {
        slackSend message:"Starting Project Name Full Test Build Version ${VERSION} - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)", baseUrl: "https://projectname.slack.com/services/hooks/jenkins-ci/", token: "token"
    }   
    echo 'Starting Project Name Full Test Build and Sonar Analysis'
    withSonarQubeEnv('Sonar') {
        sh 'mvn clean verify -P full-test sonar:sonar -Dsonar.branch.name=$BRANCH_NAME -e --settings ${SETTINGS}'
    }
    script {
        sleep(20)
        def qualitygate = waitForQualityGate()
        if (qualitygate.status != "OK") {
                error "Project Name Full Test Build FAILED due to quality gate failure: ${qualitygate.status}"
        }
    }
    script {
        if ($BRANCH_NAME == "master") {
            echo 'Starting SNAPSHOT artifact deploy'
            sh 'mvn clean deploy -DskipTests -P dist --settings ${SETTINGS}'
        }
    }
apr_1985
  • 1,764
  • 2
  • 14
  • 27
Styx1
  • 139
  • 2
  • 7