Pull Request decoration for GitLab with Jenkins

Hi,
I am using:

  • Sonarqube 8.2 Developer Edition (Trial)
  • sonar-maven-plugin:3.6.0.1398
  • Jenkins Latest
  • Gitlab Latest

And I have managed to set a Test Job for Branch Analysis:

withSonarQubeEnv('SonarQube') {
            sh """
              mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar \
                -Dsonar.branch.name=${env.GIT_BRANCH} 
              """
        }

And a Test Jenkins Job for PR Decoration:

withSonarQubeEnv('SonarQube') {
            sh """
              mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar \
                -Dsonar.pullrequest.branch=${env.GIT_BRANCH} \
                -Dsonar.pullrequest.key=983
              """
        }

It all plays well. I was expecting a deeper integration (where I don’t need to add those parameters, but SonarQube detects new Merge Requests and triggers the right build, but I know it would require a 3-way integration that will also have some complex configuration).

But I want to achieve something automatic, so when a PR is created in gitlab, a Jenkins build is triggered with the right parameters (Values needed would be the sonar.pullrequest.key and sonar.pullrequest.base values).

I believe the 8.x gitlab PR decoration plays very well with Gitlab CI/CD, but we are a lot of people using Jenkins out there, and it would be really useful if somebody has already worked out how to integrate this in Jenkins.

Anybody have already gone through this?

Hi @A_Lopez,

Our scanner can auto-detect these values if one of the corresponding Branch Source plugins is installed. For GitLab, this would be GitLab Branch Source. You would then need to make sure your Jenkins job is a Multibranch Pipeline job, and use this GitLab Branch Source plugin as the branch source.

Coincidentally, we’re in fact in the process of updating our documentation with more info on this. It should get deployed by the end of this week :slight_smile: (or, if you don’t mind reading raw markdown, you can see the source page here before it gets deployed).

Let me know how it goes with the branch source plugin.

Hi again @A_Lopez,

Sorry, let me amend that a bit :sweat_smile:. This is coming in 8.3, which will be released this week . It’s not yet available in 8.2.

Thanks! I’ll install the Jenkins plugin tomorrow and report back if everything looks fine, but it looks exactly what I was looking for.

This helped to trigger jobs when we have a merge request, using Gitlab Plugin + Gitlab Branch Source plugin, but I cannot find variables that do the following

mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar \
-Dsonar.pullrequest.branch=${env.GIT_BRANCH} \                   
 -Dsonar.pullrequest.key=${env.gitlabMergeRequestId} \
                    -Dsonar.pullrequest.base=${env.gitlabTargetBranch}

So still stuck. It looks like those variables (or similar variables) are not around for multibranch pipeline jobs…I’ll continue looking into options.

There is another plugin called " Gitlab Merge Request Builder Plugin" that brings some variables, but it’s not for multibranch pipelines, hence it does not look that interesting as using “Gitlab Branch Source”.

I have my answer, that requires some sh-fu.

I’ve found there’s another variable called env.BRANCH_NAME that contains the branch name, or MR-key if it’s a Merge Request…so as long as we don’t call our git branches MR-xxxx, we can differentiate them by that variable…

Working Solution using Sonar Source Plugin

        stage('SonarQube Analysis') {
          steps {
            script {
                  MR_KEY = sh(returnStdout: true, script: "echo ${env.BRANCH_NAME} | cut -d'-' -f2").trim()
                  IS_MR = sh(returnStdout: true, script: "echo ${env.BRANCH_NAME} | cut -d'-' -f1").trim()
            }
            withSonarQubeEnv('SonarQube') {
                sh """
                  if [ "$IS_MR" = "MR" ]; then
                    mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar \
                      -Dsonar.pullrequest.branch=${env.GIT_BRANCH} \
                      -Dsonar.pullrequest.key=${MR_KEY}
                  else
                    mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar \
                      -Dsonar.branch.name=${env.GIT_BRANCH}
                  fi
                """
            }
          }
        }
1 Like

Check out the CHANGE_* environment variables at https://ci.eclipse.org/webtools/env-vars.html/, for something a bit more robust than the above.