Sonarqube to Gitlab Webhook
Environment variables
export PORT=9090
export APP_NAME=sonarqube-to-gitlab-webhook
export LOG_LEVEL=INFO
Configuration
You need to configure these for local tests or real deployment.
Configure these environment variables:
- GITLAB_URL : Gitlab URL. Example: https://gitlab.domain
- SONARQUBE_SECRET : Secret created in Webhook in Sonarqube configuration. Example: LONGHASH
- GITLAB_TOKEN : Gitlab Personal Token with api access.
Dependency Management
The project is using Go Modules for dependency management
Module: github.com/betorvs/sonarqube-to-gitlab-webhook
Test and coverage
Run the tests
TESTRUN=true go test ./... -coverprofile=cover.out
go tool cover -html=cover.out
Install golangci-lint and run lint:
golangci-lint run
Docker Build
docker build .
Deploy Kubernetes
kubectl create ns sonarqube-webhook
kubectl create secret generic sonarqube-webhook --from-literal=sonarqubeSecret=LONGHASH --from-literal=gitlabToken=xxx-9X-zxczxczxczxc -n sonarqube-webhook --dry-run=client -o yaml > sonarqube-secret.yaml
kubectl apply -f sonarqube-secret.yaml
kubectl apply -f deployment.yaml
Example Job and sonar config
Creates a step inside your .gitlab-ci.yml
:
services:
- docker:18.09.7-dind
stages:
- test
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
variables:
SONAR_HOST_URL: "https://sonar.example.local"
GIT_DEPTH: 0
sonarqube-check:
stage: test
script:
- sonar-scanner -Dsonar.qualitygate.wait=true
allow_failure: true
sonar.projectKey=projectGroup/projectName
sonar.analysis.disabledGitlabPost=false
sonar.analysis.disabledQualityReport=true
Use sonar.analysis.disabledGitlabPost
equal true
to disable post in GitLab.
Use sonar.analysis.disabledQualityReport
equal true
to remove full quality report (it will print only Quality Gateway Name and Quality Gateway Status)
Add this configuration in sonar-propject.properties:
sonar.analysis.projectID="10"
Example of commit
Commit example in gitlab.com
SONARQUBE REPORT
URL: Report Link
Quality Gateway
Quality Gateway Conditions
Metric Name: reliability_rating
- Operator: GREATER_THAN
- Value: 1
- Error Threshold: 1
- Status: OK ๐
Metric Name: security_rating
- Operator: GREATER_THAN
- Value: 1
- Error Threshold: 1
- Status: OK ๐
Metric Name: maintainability_rating
- Operator: GREATER_THAN
- Value: 1
- Error Threshold: 1
- Status: OK ๐
Metric Name: coverage
- Operator: LESS_THAN
- Value: 0.0
- Error Threshold: 70
- Status: OK ๐
Metric Name: duplicated_lines_density
- Operator: GREATER_THAN
- Value: 0.0
- Error Threshold: 3
- Status: OK ๐
References
Golang Spell
The project was initialized using Golang Spell.
Architectural Model
The Architectural Model adopted to structure the application is based on The Clean Architecture.
Further details can be found here: The Clean Architecture and in the Clean Architecture Book.