Add Gitlab Project to SonarQube

Activate in Projekt on GitLab

Doc: https://docs.sonarqube.org/latest/analysis/gitlab-integration/

  1. Create Access Token in Gitlab in the Instance, Group or Project
    1. Token name: SonarQube
    2. Scopes: api
    3. Expiration date: empty = no
    4. Role: Reporter

Save the group access token

Import GitLab Project into SonarQube

  1. Add Projekt
  2. Enter group access token
  3. Select Project you want to add
  4. Choose “With GitLab CI” for analyze your repository
  5. Project key: Other

Configure Project in Gitlab

  1. add file: .sonar-project.properties
    • In root dir of the project.
    • Filename: .sonar-project.properties
    • Content:
      sonar.projectKey=<project-name>_<key from sonarqube>
      sonar.qualitygate.wait=false
      sonar.python.version=2
  2. Add two vars to Project: Settings→ CI/CD → Variables
    1. key: SONAR_TOKEN
      1. Value: generate token in SonarQube Webinterface
      2. uncheck “Protect Variable”
      3. CHECK “Mask Variable”
    2. key: SONAR_HOST_URL
      1. Uncheck both: Protect Variable / Mask Variable

  3. enable shared runners in Settings → CI/CD
  4. add .sonar-wrapper script to project in root dir
    We use the .sonar-wrapper script in the project main branch to configure files without extensions

  5. add sonarqube job in .gitlab-ci.yml
    stages:
     - sonarqube
      
    include:
      - remote: 'https://gitsrv.schnuerle.com:8443/sonar/sonar-wrapper/-/raw/main/sonar-gitlab-ci.yml'
    
      
      


Authors:
  • Jochen Schnuerle