- Version and maintain your scanning scripts centrally
- Simplify your Jenkinsfiles across multiple repositories
- Generate HTML and JUnit XML reports for scan results
Objectives
- Clone the
shared-librariesrepository - Create a new feature branch
- Review the existing Trivy stage in your Jenkinsfile
- Implement
vars/TrivyScan.groovywith two methods - Commit and push your changes
1. Clone the Shared Libraries Repository
First, clone the central library repository where your custom steps live.Make sure you have the necessary Git credentials set up or an SSH key configured to avoid authentication errors.
2. Create a Feature Branch
Branch offmain (or your default branch) to implement the Trivy scan logic.
3. Review the Existing Jenkinsfile Stage
Here’s the currentTrivy Vulnerability Scanner stage in the Solar System project’s Jenkinsfile:
4. Create vars/TrivyScan.groovy
Under shared-libraries/vars, add a new file named TrivyScan.groovy with two methods: one for scanning vulnerabilities, and one for converting the JSON output into HTML and JUnit XML.
Shared Library Methods at a Glance
| Method | Purpose | Output Files |
|---|---|---|
vulnerability() | Runs HIGH, MEDIUM, LOW & CRITICAL scans | trivy-image-MEDIUM-results.json,trivy-image-CRITICAL-results.json |
reportsConverter() | Converts JSON to HTML & JUnit XML report templates | .html and .xml reports for both severity levels |
Ensure your Jenkins agents have Trivy installed and the template files are accessible at
/usr/local/share/trivy/templates/. Otherwise, the conversion step will fail.5. Commit and Push Your Changes
Once your library file is in place:Next Steps
- Integrate
TrivyScan.vulnerability(imageName)andTrivyScan.reportsConverter()into your project’sJenkinsfile. - Tag or merge your branch when you’re ready to release the Shared Library.
- Monitor the generated reports in your CI/CD pipeline and enforce policies based on exit codes.
Links and References
- Jenkins Shared Library Documentation
- Trivy: A Simple and Comprehensive Vulnerability Scanner
- Git Basics