Skip to main content
This guide shows how to load a TrivyScan shared library into a Jenkins Declarative Pipeline and invoke its methods from pipeline stages. The shared library runs Trivy to scan Docker images and converts JSON results into HTML and JUnit formats for publishing. What you’ll learn:
  • How the trivyScan shared library is implemented
  • How to reference a specific branch of a trusted library with @Library
  • How to call shared-library methods inside a Declarative Pipeline
  • How to publish Trivy reports in Jenkins

Shared library implementation

Create a shared library file at vars/trivyScan.groovy that exposes two global methods:
  • vulnerability(String imageName): runs Trivy to generate JSON vulnerability results
  • reportsConverter(): converts JSON results to HTML and JUnit (XML) formats
Example contents of vars/trivyScan.groovy:
Table: shared library methods at a glance Links:

Repository setup and branch usage

  • In the shared-libraries repository we added a branch named featureTrivyScan that contains trivyScan.groovy (and other shared library files, e.g., Slack notification helpers).
  • When configuring Jenkins Global Trusted Pipeline Libraries, the library’s default version is usually main. Administrators can allow pipeline authors to override the default version so a pipeline can use a different branch or tag.
To use a specific branch of a trusted shared library from your Jenkinsfile add an @Library annotation at the top of the Jenkinsfile with the library name and branch:

Declarative vs Scripted pipeline: where to call shared-library methods

  • Scripted Pipeline: you can call global vars methods directly, e.g.:
    • trivyScan.vulnerability "image-name"
  • Declarative Pipeline: method calls on global vars must be executed inside a script { ... } block. Calling them directly inside steps (outside script) will raise an error:
    • “method calls on objects are not allowed outside the script directive block.”
Wrap shared-library method invocations in a script block in Declarative Pipelines. Example:
Use fenced code blocks for any examples containing braces to avoid MDX parsing issues.

Pipeline execution and logs (high level)

  • The @Library('dasher-trusted-shared-library@featureTrivyScan') annotation instructs Jenkins to resolve and load the specified branch of the shared library at build time.
  • trivyScan.vulnerability prints the image name and runs Trivy to create JSON results for medium/low/high severities (non-failing) and for critical severity (exit-code 1).
  • trivyScan.reportsConverter converts JSON results to HTML and JUnit XML so they can be published as build artifacts and test reports.
Example console output lines (trimmed):

Reports and artifacts

  • After running reportsConverter, the generated HTML and XML files are available as build artifacts.
  • Use the HTML Publisher plugin to display HTML reports in the Jenkins job UI, or archive the artifacts for download.
Useful plugin:

Using multiple library versions

  • Jenkins supports loading multiple shared libraries. You can reference specific branches or tags with @Library('name@branch') and call methods from each loaded library as needed.
That’s the complete flow to load and invoke the TrivyScan shared library from a Declarative Jenkins Pipeline, including the required script {} wrapper for method calls on library objects.
A dark-themed Jenkins "Manage Jenkins → System" page showing the Global Trusted Pipeline Libraries configuration for a library named "dasher-trusted-shared-library" with default version set to "main." The form shows various options (checkboxes), retrieval method set to "Modern SCM," and Save/Apply buttons.

Watch Video