Gitlab integration with SonarQube

2019-03-11 12:45发布

问题:

I am pretty new to Development community and specifically to DevOps practices , as a part of project we are trying to integrate SonarQube with Gitlab , did some R& D on SonarQube and Git CI ( Continuous Integration ) and look like plugin is released for Github and SonarQube whereas not for Gitlab.

How realistic is it to configure GitLab with SonarQube for inspecting code quality for every pull request and what will be the best practice to integrate these two piece.

Thanks

回答1:

you don't really need a plugin. make something like this in your .gitlab-ci.yml

stages: 
- build 
build_master:
  image: maven
  stage: build
  artifacts:
    paths:
    - target/*.jar
  script:
  - mvn package sonar:sonar -Dsonar.host.url=https://sonar.yourdomain.tld/ 
  only:
  - master

and every master push will be tested! (this is for a Java project...)



回答2:

Currently there are (as far I am aware) two community driven plugins which aim to provide MR-analysis/integrate with GitLab.

Both of them are currently going through the Feedback phase for their next release and both aim to land into the Update Center with that release.

  • https://git.johnnei.org/Johnnei/sonar-gitlab-plugin | RFF for 0.2.0
  • https://gitlab.talanlabs.com/gabriel-allaigre/sonar-gitlab-plugin | RFF for 2.0.0

With both you're able to run a build which will provide comments in GitLab with the newly found violations. Both are highly inspired by SonarSource's GitHub plugin.

However I'm not in the position to advise you on which of the two to use as I'm the developer the first and thus biased.



回答3:

I was into same requirement and here is how I implemented,

Create a runner without specifying any tags and of shared type. Create a file .gitlab-ci.yml file with the following commands,

variables:
  SONAR_URL: "http://your_sonar_url"
  SONAR_LOGIN: "sonar_user_id"
  SONAR_PASSWORD: "sonar_password"

sonarqube_master_job:
  stage: test
  only:
    - master
  image: maven:3.3.9-jdk-8-alpine
  script:
    - mvn --batch-mode verify sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASSWORD

If you create runner with specific tags, then you need to mention the tags in the .gitlab-ci.yml file

you can get more information on adding tags in this link, https://forum.gitlab.com/t/activated-specific-runner-is-not-working/7002