Security Scanning in GitLab CI Pipelines
Add security scanning to your GitLab CI pipeline. Covers SAST, dependency scanning, container scanning, and secret detection with GitLab templates and open-source tools.
Security
Detailed Explanation
Security Scanning in GitLab CI
Security scanning in CI catches vulnerabilities before they reach production. GitLab offers built-in templates, but you can also use open-source tools directly.
Using GitLab Templates (Ultimate)
include:
- template: Security/SAST.gitlab-ci.yml
- template: Security/Dependency-Scanning.gitlab-ci.yml
- template: Security/Container-Scanning.gitlab-ci.yml
- template: Security/Secret-Detection.gitlab-ci.yml
Open-Source Alternative (Any Tier)
stages:
- test
- security
trivy_scan:
stage: security
image: aquasec/trivy:latest
script:
- trivy fs --format json --output trivy-report.json .
- trivy fs --severity HIGH,CRITICAL --exit-code 1 .
artifacts:
paths:
- trivy-report.json
expire_in: 30 days
allow_failure: true
gitleaks:
stage: security
image: zricethezav/gitleaks:latest
script:
- gitleaks detect --source . --report-format json --report-path gitleaks-report.json
artifacts:
paths:
- gitleaks-report.json
expire_in: 30 days
allow_failure: true
semgrep:
stage: security
image: returntocorp/semgrep
script:
- semgrep scan --config auto --json --output semgrep-report.json .
artifacts:
paths:
- semgrep-report.json
expire_in: 30 days
allow_failure: true
Container Image Scanning
container_scan:
stage: security
image: aquasec/trivy:latest
script:
- trivy image --severity HIGH,CRITICAL $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
needs:
- docker_build
Best Practices
- Start with
allow_failure: true: This prevents blocking deployments while you triage initial findings. - Scan on merge requests: Use
rulesto run scans on MR pipelines so developers see results before merging. - Fail on HIGH/CRITICAL: Use
--exit-code 1with severity filters to block only serious vulnerabilities. - Store reports as artifacts: Keep scan results for auditing and trend analysis.
Use Case
Every production pipeline should include some form of security scanning. Start with secret detection and dependency scanning (highest ROI), then add SAST and container scanning as your security posture matures.