Jenkins builds using the variable $ {GIT_BRANCH} as the sonarqube parameter without "origin /"

Large team with separate git branches per command and sonar as a code quality check.

The scm tool is configured to collect each branch matching that name: 'feature-branch-*' , which works great for maven builds.
I wanted to create sonarqube so that it uses a different branch for each real git branch, but sonarqube does not allow wildcards. However, it allows variables, so I tried ${GIT_BRANCH} , but this variable takes place: 'origin/feature-branch-214' , which sonarqube does not recognize as a valid branch name. Sonarqube expects 'feature-branch-214'

So I need to add something (in the sonarqube branch) that makes the substring ${GIT_BRANCH} to exclude the part of 'origin/' .

+6
source share
4 answers

You can create a new variable and assign a value:

 `echo ${GIT_BRANCH} | cut -d'/' -f 2-` 

You can then use the new variable in the Jankins Git Branch SonarQube parameter by following these steps.

Steps

In the build section, add the Execute Shell step with command :

 echo NEW_VAR=`echo ${GIT_BRANCH} | cut -d'/' -f 2-` > newfile 

Then add the Inject an environment variable step with Properties File Path :

 newfile 

In the SonarQube configuration, leave the branch empty field and add the following to Additional properties field :

 -Dsonar.branch=${NEW_VAR} 

The above solution is not very clean, but I checked that it works

+6
source

Or you could:

 echo NEW_VAR=${GIT_BRANCH#*/} > newfile 
+6
source
 git show-ref | grep $(git rev-parse HEAD) | grep remotes | grep -v HEAD | sed -e 's/.*remotes.origin.//' 
0
source

The easiest way is to use the Jenkins environment variable GIT_LOCAL_BRANCH instead of GIT_BRANCH. You can see the full list of Jenkins environment variables at http: // [jenkins-url] /env-vars.html.

0
source

All Articles