SonarQube CI/CD integration with Snapsec VM
Use SonarQube in your CI/CD pipeline to analyze code quality and security, then push vulnerability findings into Snapsec VM using a simple webhook. This guide is inspired by modern SonarQube best practices for 2025-style CI integration and alerting, and adapts them so that:- Your CI pipeline runs SonarQube analysis on every change.
- Vulnerabilities are fetched from SonarQube via its Web API as JSON.
- That JSON is sent directly to Snapsec VM using a webhook.
1. Prerequisites
- A running SonarQube server (self‑hosted or cloud) reachable from your CI environment.
- A SonarQube project configured for your repository.
- A SonarQube token with permission to run analysis and query issues.
- Snapsec:
- An Assessment in Snapsec VM where SonarQube vulnerabilities will be stored.
- Assessment ID (
<assessment-id>) - API key (
<your-api-key>)
- CI environment (GitHub Actions or GitLab CI) with:
sonar-scanner(or SonarQube build plugins, e.g. Maven/Gradle)curlavailable.
2. Create an assessment in Snapsec VM
Before you send any results, create a dedicated assessment in Snapsec VM that will hold the SonarQube findings:- Log in to the Snapsec UI.
- Go to the VM / Assessments section.
- Click New Assessment and give it a clear name, for example:
SonarQube - Backend Service
- Save the assessment and copy its Assessment ID value.
3. Run SonarQube analysis in CI
There are multiple ways to run SonarQube (Maven plugin, Gradle,sonar-scanner).Here is a generic example using the standalone scanner:
SONAR_TOKEN exported) before wiring it into CI.
4. Export SonarQube vulnerabilities as JSON
After analysis completes in CI, use the SonarQube Web API to fetch vulnerability issues for your project and save them as JSON. Example:- Calls the
/api/issues/searchendpoint. - Filters only vulnerability‑type issues (
types=VULNERABILITY). - Saves the response as
sonarqube-vulnerabilities.json.
Adjust parameters (e.g. severities=MAJOR,CRITICAL,BLOCKER, pagination, branch/pull request keys) to match your workflow.
5. Push SonarQube JSON directly to Snapsec VM via webhook
Snapsec knows how to parse SonarQube JSON output, so you can send the file directly to an import endpoint.Important: ReplaceBelow are ready-to-use examples for GitHub Actions and GitLab CI.<assessment-id>with your actual Assessment ID and<your-api-key>with your API key. Note on the-kflag: This flag tellscurlto perform an “insecure” SSL transfer, which bypasses certificate validation. You may need this for local or development environments. Remove it if your endpoint has a valid SSL certificate.
6. GitHub Actions example
- Create
.github/workflows/sonarqube-to-snapsec.ymlin your repository. - Copy the YAML above into that file.
- In your GitHub repository settings, create secrets:
SONAR_HOST_URL(e.g.https://sonarqube.my-company.com)SONAR_PROJECT_KEYSONAR_TOKENSNAPSEC_ASSESSMENT_IDSNAPSEC_API_KEY
- Adjust build/analysis steps for your language and project.
- Push your changes. GitHub Actions will run the workflow on each push or pull request.
7. GitLab CI example
- Create or edit
.gitlab-ci.ymlin the root of your repository. - Add the
sonarqube_to_snapsecjob shown above. - In your GitLab project, go to Settings → CI/CD → Variables and add:
SONAR_HOST_URLSONAR_PROJECT_KEYSONAR_TOKENSNAPSEC_ASSESSMENT_IDSNAPSEC_API_KEY
- Commit and push your changes. GitLab will run the job on merge requests and on the
mainbranch.
- Run SonarQube analysis automatically in your pipeline.
- Export vulnerability issues via the SonarQube Web API.
- Upload the resulting JSON directly to Snapsec VM using the provided webhook.