Skip to main content
In this guide you’ll refactor the existing Solar System Jenkinsfile to run selected Node.js stages inside a Kubernetes pod by referencing a k8s-agent.yaml pod specification file stored in the repository. This keeps the pod spec versioned alongside your code and lets you run Node.js-related steps inside a container while leaving non-containerized stages (for example Docker or privileged Trivy scans) to run on the controller. What you’ll do:
  • Add a k8s-agent.yaml pod spec at the repository root.
  • Update the Jenkinsfile to use the Kubernetes declarative agent with yamlFile.
  • Run Node.js stages inside the pod (default container node-18) and run other stages on the controller using agent any at the stage level.
Tip: keep your repository open at the project root (solar-system) while editing.

Existing Jenkinsfile (before refactor)

This is the top of the original Jenkinsfile used as the starting point:

1) Create the Kubernetes pod spec file (k8s-agent.yaml)

At the repository root create k8s-agent.yaml. This minimal pod spec provides two Node.js containers that remain running (using cat and tty: true) so Jenkins can exec into them. Save the following content into k8s-agent.yaml:
This pod spec intentionally keeps the containers alive to allow Jenkins to run shell commands inside the selected container. We will use node-18 as the pipeline default container.
Keeping a small k8s-agent.yaml in the repo improves reproducibility. You can extend this pod spec with init containers, environment variables, or volume mounts later (for example to inject credentials or cache layers).

2) Update the Jenkinsfile to use the Kubernetes agent

Replace the top-level agent any with a Kubernetes agent block that references the YAML file and sets a defaultContainer:
Notes:
  • cloud must match a configured Kubernetes cloud in your Jenkins Kubernetes plugin.
  • yamlFile points to the pod spec file in the repository (k8s-agent.yaml).
  • defaultContainer designates the container where shell steps run by default (node-18).
Make sure the Jenkins Kubernetes cloud name (cloud 'dasher-prod-k8s-us-east') matches a configured Kubernetes cloud in your Jenkins settings. If it does not match, pod provisioning will fail.

3) Run only Node.js stages inside the Kubernetes pod

Change the stages that depend on Node.js to run inside the pod (they will run in the node-18 container by default). For stages that need to run on the controller (for Docker builds, privileged Trivy scans, etc.), specify agent any at the stage level. Example Node.js stage implementations after the refactor:
  • Installing Dependencies (runs inside Kubernetes pod, default node-18):
  • Dependency scanning (parallel branch with NPM audit):
  • Unit Testing (with retry):
  • Code Coverage (allow failures so coverage reporting won’t break the pipeline):
  • Docker build or Trivy scans (run on controller or appropriate agent): declare agent any at the stage level.

Stage placement quick reference

4) Commit and push

Commit both k8s-agent.yaml and the modified Jenkinsfile to your feature branch and push. Depending on your SCM hooks, the pipeline should be triggered automatically. When the pipeline runs, Jenkins will:
  • Fetch the k8s-agent.yaml file from the repository.
  • Provision a pod on the configured Kubernetes cloud using the pod spec.
  • Mount a shared workspace volume so all containers in the pod share the same workspace across stages.
  • Execute Node.js stages inside the node-18 container (default), allowing artifacts and dependencies installed during one stage to be available to subsequent stages in the same pod.
Below is the pipeline UI showing the run in Blue Ocean:
A dark-themed Jenkins web UI showing the pipeline status for a job named "feature/advanced-demo," with multiple build runs and stage icons indicating successes and a few warnings/errors. The left sidebar shows navigation items like Status, Build Now, and Open Blue Ocean, plus a Build History panel.

Example console output highlights

During a run you’ll see Jenkins load the shared library, retrieve the YAML, and provision the pod. Example excerpts (shortened):
  • Loading library and obtaining the YAML:
  • Pod spec created by the plugin (excerpt):
  • Sample Installing Dependencies output:
  • Sample npm audit output:
  • Sample test and coverage output:
Because Node.js stages share the same workspace volume in the pod, dependencies installed in the “Installing Dependencies” stage persist for subsequent stages without re-installing.

Summary and best practices

  • Use agent { kubernetes { yamlFile 'k8s-agent.yaml' } } to keep a reusable pod spec in your repository instead of embedding YAML inside the Jenkinsfile.
  • Select a defaultContainer (for example node-18) for Node-based stages.
  • Override stage-level agent with agent any for steps that must run outside the pod (Docker builds, privileged scans).
  • A single pod with multiple containers and a shared workspace volume lets you reuse artifacts and installed dependencies across stages, reducing duplication and improving pipeline speed.
  • You can add more containers to the pod and switch containers within stages using container('container-name') { ... } if you need different runtimes or tools in different stages.

Watch Video