- Builds a Docker image,
- Pushes it to Docker Hub,
- Runs multiple Kubernetes security checks (OPA, Kubesec) in parallel with Trivy,
- Deploys the image to a Kubernetes cluster only if scan thresholds pass.
Quick overview
- Build and push Docker image.
- Run parallel security scans:
- OPA (conftest)
- Kubesec
- Trivy (image vulnerability scanner)
- Fail the pipeline if configured thresholds (e.g., CRITICAL) are met; otherwise continue to deployment.
Pipeline snippet — where Trivy fits
Below is the relevant part of the Jenkinsfile. The Trivy scan stage runs in parallel with other Kubernetes checks and is placed after Docker build/push and before Kubernetes deployment.Recommended pipeline stage mapping
Trivy wrapper script (trivy-k8s-scan.sh)
Create a single script in your repository so Jenkins can call it as part of the pipeline. The example below:- Uses Trivy inside Docker (no local install required).
- Runs two scans:
- A non-failing scan for LOW/MEDIUM/HIGH (exit code 0).
- A failing scan for CRITICAL (exit code 1) so the pipeline fails when criticals exist.
- Mounts
$WORKSPACEas the Trivy cache to speed up subsequent scans.
Make sure
imageName is exported in the pipeline environment and that the Jenkins agent has permissions to run Docker commands. The second Trivy run intentionally returns exit code 1 when CRITICAL vulnerabilities are found to stop the pipeline.Example environment block in Jenkinsfile
DefineimageName (and other deployment variables) so the scan script can access the built image reference:
Example Trivy output and what it shows
When Trivy finds vulnerabilities, it reports:- Affected file or component (e.g.,
app.jar), - Vulnerability ID (CVE),
- Severity level,
- Installed version and suggested fixed versions (when available).

Remediation example — update Tomcat in Spring Boot POM
If Trivy points to Tomcat vulnerabilities originating from a Spring Boot-managed dependency, update the Tomcat version in yourpom.xml properties (or upgrade the Spring Boot parent). Example:
When updating dependency versions, always run your unit/integration tests and validate application behavior. Some upgrades may require code changes—verify in a non-production environment first.

Best practices and final notes
- Different scanners (Trivy, Dependency-Check, etc.) may return different results due to varying vulnerability databases and heuristics. Treat each finding as an investigation item.
- Tune severity thresholds for Trivy to match your risk policy. Use exit codes to enforce fail/allow rules.
- Use the workspace cache (
-v "$WORKSPACE":/root/.cache/trivy) to speed repeated scans. - Consider scanning both the base image and application artifacts (JAR/WAR) included in the image to get a full picture.
- Store credentials (Docker Hub, kubeconfig) securely in Jenkins Credentials and reference them in the pipeline with the proper wrappers.
Links and references
- Trivy: https://github.com/aquasecurity/trivy
- Jenkins Blue Ocean: https://www.jenkins.io/projects/blueocean/
- Open Policy Agent (conftest): https://www.conftest.dev/
- Kubesec: https://kubesec.io/