Skip to main content
This lesson refactors an existing Jenkinsfile so the same pipeline can be reused across upcoming demos. It builds on concepts from the Jenkins Pipelines course. If you haven’t completed that course, review it first:
This demo shows a practical Jenkinsfile refactor to:
  • Reduce and focus stages for CI-focused demos.
  • Keep Slack notifications and critical scans.
  • Move report generation into the stage that produces the artifacts (Trivy).
Prerequisites
  • Familiarity with Declarative Jenkins Pipelines.
  • Basic Git usage (branching, committing, pushing).
  • Basic knowledge of Trivy for container vulnerability scanning.
  • The repository used in this demo (details below).
Repository and branch being used This repo lives under the dasher organization as solar-system. In the previous course we used the feature/enabling-slack branch. For the advanced demos we create a new branch from that branch so we can experiment without affecting the original.
A dark-themed Gitea organization page for "dasher-org" showing a list of repositories (like "solar-system", "solar-system-gitops-argocd", etc.) and a right-hand sidebar with Members and Teams. The top has navigation and buttons for "New Repository" and "New Migration."
Open the repository in your editor or terminal. Example shell prompt:
Goals of the refactor
  • Retain Slack notification logic to keep CI visibility.
  • Keep Docker build and Trivy vulnerability scan stages.
  • Keep npm install, unit tests, and code coverage stages.
  • Comment out long-running or environment-specific stages we won’t use (SonarQube, OWASP dependency-check, pushing to registries, EC2/K8s deployments).
  • Move Trivy convert and report publishing (publishHTML, junit) into the Trivy stage so artifacts are grouped with their generating stage.
Slack notification helper The Slack helper function is a small Groovy method used inside the post blocks. It remains unchanged and will be reused by the simplified top-level post block.
Top of the Jenkinsfile — agent, tools, environment Example snippet from the top of the Jenkinsfile showing agent, tools, environment, and the simplified post block. This remains the structural basis for the pipeline:
Which stages we keep vs. comment out To make the pipeline concise and focused for demos, we kept the core CI stages and commented out most environment-dependent or long-running stages. For readability the disabled stages are preserved in the file as commented placeholders to show they exist but are not executed in this demo:
Trivy stage: run, convert, and publish reports locally within the stage Rather than publishing Trivy’s HTML/XML reports from a global post block, move the conversion and publishing into the Trivy stage’s own post so that report generation stays close to its source. The workflow in the Trivy stage typically:
  1. Run Trivy scans for different severity sets.
  2. Output JSON results.
  3. Convert JSON to HTML and JUnit XML using trivy convert.
  4. Publish JUnit and HTML reports using junit and publishHTML.
Example Trivy scan commands used in the Trivy stage (shell commands executed by the stage):
Convert the JSON results to HTML and JUnit XML:
Publish JUnit/XML and HTML reports inside the Trivy stage post block:
Cleaning up the global post section To keep the pipeline concise and visually clear in the Jenkins UI, we removed or commented out global junit and publishHTML calls and instead publish results in the specific stage that produces them. The top-level post block now contains only notifications and optional workspace cleanup:
Creating and pushing a new feature branch Rather than editing the original feature/enabling-slack branch directly, create a new branch feature/advanced-demo. Example git flow:
As soon as the branch is pushed, the multibranch pipeline (Git Organization job) detects the new branch and triggers a build when it finds the Jenkinsfile. Build execution and handling expected failures The refactor reduces stage count (from ~20 to ~4–5), which is ideal for demos. During early runs you may encounter Trivy failures that originate outside your environment (e.g., rate-limiting when Trivy downloads its vulnerability DB). These failures are external and should be handled separately (e.g., use a cached local DB, adjust Trivy exit codes, or re-run). Sample console excerpt showing a Trivy DB download rate-limit error:
Trivy failures like the example above are usually due to external rate-limiting of Trivy DB downloads. Consider these mitigations:
  • Use a local Trivy DB cache (mirror) for CI.
  • Adjust --exit-code thresholds to avoid failing builds on lower-severity issues.
  • Add retry logic or fallback behavior for DB downloads.
Summary of the refactor
  • Focused and reduced the Jenkinsfile to key CI stages for demos: dependencies, unit tests/coverage, Docker build, and Trivy scans.
  • Preserved and reused Slack notification logic; simplified the global post block to notifications and cleanup.
  • Moved Trivy report conversion and publishing into the Trivy stage post block for logical grouping of artifacts and easier debugging.
  • Disabled long-running/deployment-specific stages in-place (commented) so they can be re-enabled later if needed.
  • Created feature/advanced-demo branch to test the refactor without modifying the original branch.
Next steps
  • Implement Trivy DB caching or retry strategies to avoid rate-limit issues.
  • Consider moving shared helper functions (like slackNotificationMethod) to a Jenkins shared library if they will be reused across multiple pipelines.
  • Re-enable additional stages gradually when demonstration requirements expand (for example, SonarQube or K8s deployment stages).
Links and references

Watch Video