Jenkinsfile from using agent any to leveraging a Kubernetes agent. By externalizing Pod definitions into a YAML manifest and targeting specific Node.js containers for build stages, we achieve more consistent, scalable CI/CD workflows.
Original Jenkinsfile Overview
The current pipeline uses a generic agent and defines global tools, environment variables, and stages:Defining the Kubernetes Pod Manifest
Create ak8s-agent.yaml at your repo root to specify two Node.js containers:
node-18 and node-19 containers that Jenkins will schedule within a single Pod.
Ensure the Kubernetes plugin is installed in Jenkins and your Kubernetes cloud configuration (
dasher-prod-k8s-us-east) is active before running the refactored pipeline.Refactoring Jenkinsfile to Use Kubernetes Agent
Replace the top-levelagent any with the kubernetes agent block, referencing the YAML manifest and defaulting to node-18:

Stage-Level Container Configuration
We’ll run Node.js–specific stages innode-18, while Docker build and security scans fall back to agent any. Here’s the updated stage block:

Running and Monitoring the Refactored Pipeline
Commit your changes and push to trigger the pipeline. You can monitor status and logs in Blue Ocean:

All Node.js stages share an
emptyDir volume by default, so dependencies installed in one stage persist for subsequent stages within the same Pod.Links and References
- Jenkins Kubernetes Plugin
- Blue Ocean Documentation
- Kubernetes Pod Spec
- Trivy Security Scanner
- Jenkins Pipeline Syntax