Skip to main content
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.

Prerequisites

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’s Jenkinsfile:
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’s Jenkinsfile, load the library and call the functions:
Now your security scan is centralized, versioned, and easy to update!

References

Watch Video