CodeQL CI/CD integration with Snapsec VM
Use CodeQL in your CI pipeline to statically analyze your code for security issues and then push the findings into Snapsec VM using a simple webhook. This guide shows you, step by step, how to:- Run CodeQL in your CI pipeline (GitHub Actions or GitLab CI example).
- Generate a SARIF/JSON report with CodeQL results.
- Send that report directly to Snapsec VM using a webhook.
If you need pipeline examples for other CI systems (Jenkins, Azure Pipelines, CircleCI, TravisCI, AWS CodeBuild, DroneCI, etc.), you can refer to the sample configurations in the CodeQL team’s repository advanced-security/sample-codeql-pipeline-config.
1. Prerequisites
- A repository with source code you want to analyze.
- CodeQL CLI / CodeQL bundle available in your CI environment (see GitHub’s CodeQL docs or the sample pipeline repo above).
- Snapsec:
- An Assessment in Snapsec VM where CodeQL findings will be stored.
- Assessment ID (
<assessment-id>) - API key (
<your-api-key>)
- CI environment with
curlavailable.
2. Create an assessment in Snapsec VM
Before you send any results, create a dedicated assessment in Snapsec VM that will hold the CodeQL findings:- Log in to the Snapsec UI.
- Go to the VM / Assessments section.
- Click New Assessment and give it a clear name, for example:
CodeQL - Monolith
- Save the assessment and copy its Assessment ID value.
3. Generate a CodeQL SARIF/JSON report (locally or in CI)
The exact commands depend on your language and setup, but a typical flow with the CodeQL CLI looks like this:codeql.sarif file (which is JSON-formatted SARIF) containing the CodeQL findings.
You can try these commands locally first to confirm everything works before wiring them into CI.
4. Push CodeQL SARIF/JSON directly to Snapsec VM via webhook
Snapsec can parse SARIF/JSON output from CodeQL, 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.
5. GitHub Actions example (CodeQL CLI)
The example below uses the CodeQL CLI bundle in a GitHub Actions workflow to generatecodeql.sarif and then push it to Snapsec.
- Create
.github/workflows/codeql-to-snapsec.ymlin your repository. - Copy the YAML above into that file.
- In your GitHub repository settings, create secrets:
SNAPSEC_ASSESSMENT_IDSNAPSEC_API_KEY
- Adjust the CodeQL language and query suite for your stack (for example, use
--language=pythonand the Python query suite). - Push your changes. GitHub Actions will run the workflow on each push or pull request.
For more advanced CodeQL configurations or other CI systems, see the templates in
advanced-security/sample-codeql-pipeline-config.
6. GitLab CI example (CodeQL CLI)
If you use GitLab, you can run the CodeQL CLI in a GitLab CI job and then post the SARIF file to Snapsec:- Create or edit
.gitlab-ci.ymlin the root of your repository. - Add the
codeql_to_snapsecjob shown above. - In your GitLab project, go to Settings → CI/CD → Variables and add:
SNAPSEC_ASSESSMENT_IDSNAPSEC_API_KEY
- Adjust the CodeQL language and query suite to match your project.
- Commit and push your changes. GitLab will run the job on merge requests and on the
mainbranch.
- Run CodeQL automatically in your pipeline.
- Upload the
codeql.sarifreport directly to Snapsec VM using the provided webhook.