In this tutorial, you’ll build a reusable Jenkins Shared Library to run Trivy scans in your CI/CD pipelines. By isolating scanning logic in a library, you’ll eliminate duplication and enable versioned updates via Git feature branches.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
| Requirement | Description |
|---|---|
| Jenkins Shared Library Repo | A Git repository to host your vars/ functions |
| Trivy CLI | Installed on your Jenkins agents or build environment |
1. Clone the Shared Library Repository
Start by cloning your existing shared-library project:2. Create a Feature Branch
Work on a dedicated branch to isolate your changes:3. Review the Hardcoded Trivy Stage
In many pipelines, you’ll find a stage like this in the application’sJenkinsfile:
Hardcoding scanner commands in every
Jenkinsfile is hard to maintain. Any change in flags or output formats would need updates in all pipelines.4. Create the TrivyScan.groovy in vars/
Inside your shared-library’s vars/ folder, add a new file:
5. Define the vulnerability Function
Open vars/TrivyScan.groovy and add a method that accepts the Docker image name:
We use triple-double-quotes (
"""…""") in Groovy to allow ${imageName} interpolation inside the shell script block.6. Add the reportsConverter Function
Extend the same file with report conversion logic:
7. Commit and Push Your Changes
Save, commit, and push the new shared-library logic:8. Consume the Shared Library in a Pipeline
In your application’sJenkinsfile, load the library and call the functions:
References
- Jenkins Shared Library Documentation
- Trivy – A Simple and Comprehensive Vulnerability Scanner
- Jenkins Pipeline Syntax