Skip to main content

Integrasi GitLab CI dengan SonarQube

1. Tujuan

Mengintegrasikan GitLab CI/CD dengan SonarQube sehingga setiap perubahan kode pada repository dapat otomatis dilakukan Static Code Analysis.

Dengan integrasi ini pipeline akan:

Developer push code
        │
        ▼
GitLab CI Pipeline
        │
        ▼
Sonar Scanner
        │
        ▼
SonarQube Analysis
        │
        ▼
Quality Gate Result

Output yang dihasilkan:

  • Bugs

  • Vulnerabilities

  • Code Smells

  • Security Hotspots

  • Maintainability Rating


2. Prerequisite

Komponen yang harus tersedia:

Komponen Keterangan
GitLab Self-hosted atau GitLab.com
GitLab Runner Untuk menjalankan pipeline
SonarQube Server Server analisis kode
Sonar Scanner Tool yang mengirim source code ke SonarQube

Contoh arsitektur implementasi:

Developer
    │
    ▼
GitLab Server
    │
    ▼
GitLab Runner
    │
    ▼
SonarQube Server

3. Membuat Project di SonarQube

Masuk ke:

Administration
→ Projects
→ Create Project

Isi parameter:

Project Name : wid-test
Project Key  : wid-test

Klik:

Create Project

Setelah project dibuat, SonarQube akan menampilkan:

This project has not been analyzed yet

Artinya SonarQube menunggu scan pertama dari CI/CD.


4. Generate SonarQube Token

Masuk ke:

My Account
→ Security

Buat token baru:

Token Name : wid-test
Type       : Project

Copy token yang dihasilkan.

Contoh:

sqp_xxxxxxxxxxxxxxxxx

Token ini akan digunakan oleh GitLab CI untuk autentikasi ke SonarQube.


5. Konfigurasi Variable di GitLab

Masuk ke repository GitLab:

Settings
→ CI/CD
→ Variables

Tambahkan variable berikut:

Variable Value
SONAR_HOST_URL http://sonarqube-server:9000
SONAR_TOKEN token SonarQube
SONAR_PROJECT_KEY wid-test

6. Instalasi GitLab Runner

Install GitLab Runner pada server CI.

Ubuntu / Debian

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner

Cek runner:

gitlab-runner --version

7. Register GitLab Runner

Jalankan:

sudo gitlab-runner register

Isi parameter:

GitLab URL : https://gitlab.domain.com
Token      : registration token dari GitLab
Description: sonar-runner
Tags       : shell
Executor   : shell

Setelah selesai, runner akan muncul di:

Settings → CI/CD → Runners

8. Instalasi Sonar Scanner di Runner

Karena runner menggunakan shell executor, maka Sonar Scanner harus diinstall di server runner.

Download scanner:

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip

Extract:

unzip sonar-scanner-cli-5.0.1.3006-linux.zip

Move ke directory system:

sudo mv sonar-scanner-5.0.1.3006-linux /opt/sonar-scanner

Tambahkan ke PATH:

sudo nano /etc/profile.d/sonar.sh

Isi:

export PATH=$PATH:/opt/sonar-scanner/bin

Reload environment:

source /etc/profile.d/sonar.sh

Verifikasi:

sonar-scanner --version

9. Konfigurasi Pipeline GitLab

Buat file:

.gitlab-ci.yml

Contoh konfigurasi:

stages:
  - sonar

sonarqube-check:
  stage: sonar
  tags:
    - shell

  variables:
    SONAR_USER_HOME: "$CI_PROJECT_DIR/.sonar"

  cache:
    paths:
      - .sonar/cache

  script:
    - |
      sonar-scanner \
        -Dsonar.projectKey=$SONAR_PROJECT_KEY \
        -Dsonar.sources=. \
        -Dsonar.host.url=$SONAR_HOST_URL \
        -Dsonar.login=$SONAR_TOKEN \
        -Dsonar.qualitygate.wait=true

  only:
    - sonarqube

10. Trigger Pipeline

Pipeline dijalankan dengan push ke branch:

sonarqube

Contoh:

git checkout -b sonarqube
git push origin sonarqube

Pipeline akan menjalankan:

Git clone
   ↓
Sonar Scanner
   ↓
Send analysis to SonarQube

11. Verifikasi di SonarQube

Masuk ke project SonarQube:

Projects → wid-test

Jika sukses akan muncul:

Last Analysis : few seconds ago

Metrics yang muncul:

  • Bugs

  • Vulnerabilities

  • Code Smells

  • Security Hotspots

  • Coverage

  • Maintainability


12. Troubleshooting

Pipeline Pending

Penyebab:

GitLab Runner belum aktif

Solusi:

sudo systemctl start gitlab-runner

HTTP Basic Access Denied

Penyebab umum:

GitLab reverse proxy tidak sinkron dengan external_url

Solusi:

edit /etc/gitlab/gitlab.rb
external_url "https://gitlab.domain.com"

Lalu jalankan:

gitlab-ctl reconfigure

sonar-scanner: command not found

Penyebab:

Sonar Scanner belum diinstall di runner

Solusi:

Install sonar-scanner di server runner.


Unrecognized option -Dsonar

Penyebab:

syntax YAML salah

Solusi:

Gunakan YAML multiline block:

script:
  - |
    sonar-scanner \
      -Dsonar.projectKey=...

13. Best Practice

Untuk environment production disarankan:

GitLab Runner executor = docker

Keuntungan:

  • environment terisolasi

  • dependency otomatis

  • pipeline reproducible

Arsitektur ideal:

Internet
    │
Reverse Proxy
    │
 ┌─────────────┬─────────────┐
 GitLab        SonarQube
      │
 GitLab Runner (Docker)

14. Improvement yang Direkomendasikan

Integrasi dapat ditingkatkan dengan:

  • SonarQube scan pada Merge Request

  • Quality Gate untuk block merge

  • Integrasi test coverage

  • Integrasi Security scanning

Flow CI/CD ideal:

Build
Test
SonarQube Scan
Quality Gate
Docker Build
Deploy

Jika Anda mau, saya juga bisa buatkan versi KB yang lebih “enterprise style” (biasanya dipakai di Confluence / internal DevOps documentation) lengkap dengan:

  • Diagram arsitektur

  • Flow pipeline

  • Troubleshooting matrix

  • Best practice security.