This lesson shows how to run scripted Jenkins pipeline stages inside Kubernetes pods using the Jenkins Kubernetes plugin. Unlike declarative pipelines where anDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
agent block often handles checkout and workspace for you, scripted pipelines require more explicit control: define pod templates, manage checkouts on each node, and transfer artifacts between agents with stash/unstash.
You will learn:
- How to define a Kubernetes pod via
podTemplateandcontainerTemplate. - How to run some stages on a static Jenkins agent and other stages inside Kubernetes containers.
- How to move installed dependencies (for example
node_modules) between agents usingstash/unstash.
Inline podTemplate example
Below is a pod template you can generate from Jenkins “Pipeline Syntax” and then simplify for use inside a Jenkinsfile. This example defines a pod with two Node.js container templates:cloudselects the Kubernetes cloud configured in Jenkins.labelis the pod label that binds a Jenkinsnode(...)to this pod template.- Each
containerTemplatebecomes a container inside the created pod. Usingsleepkeeps the container alive while the Jenkins agent communicates with it.

Use the “Pipeline Syntax” generator
To produce the initial Groovy block, use Jenkins’ “Pipeline Syntax” generator. The form looks like this when creating a pod template for a cloud:


Practical scripted Jenkinsfile pattern
This common pattern uses:- A static Jenkins agent (e.g., long-lived Ubuntu/Docker executor) to perform checkout and install dependencies (fast, cache-friendly).
stashon that agent to capture installed dependencies.- A Kubernetes pod/container to run unit tests after
unstashing the artifacts andcheckout scmon that node.
- In scripted pipelines, switching to a different agent/node (for example from a static agent to a Kubernetes pod via
node('label')) does not carry over the workspace or checked-out files. Always runcheckout scmon the node where you will execute build/test commands, or usestash/unstashto transfer files between agents. stashthe produced artifacts on the producer agent (where you installed dependencies) andunstashthem on the consumer agent (the Kubernetes container) to make tools and dependencies available where they are needed.
Common failure examples (and fixes)
- Missing repository files on the Kubernetes container (ENOENT: package.json).
Cause: forgetting tocheckout scminside the k8snode(...)block. Failing log example:
checkout scm on the same node/container where you invoke npm test.
- Missing dev/test tools (e.g., mocha: not found).
Cause: dependencies were installed on a different agent and not transferred. Symptom:sh: mocha: not found.
Fix:stashnode_modules on the installing agent andunstashon the test agent, or install dependencies inside the test container.
Be mindful of stash size and limits: stashing large directories (e.g., entire build artifacts) may increase build time and storage use. Prefer caching plugins or artifact repositories for large dependencies. Also ensure workspace paths and ownership are compatible across agents (uid/gid differences can affect file access).
checkout scm and unstash to the unit testing stage, the pipeline should successfully provision pods and run tests. Monitor pod provisioning, logs, and step output in the Jenkins UI (Blue Ocean or classic UI):

Quick reference table
| Field | Purpose | Example |
|---|---|---|
| cloud | Kubernetes cloud configured in Jenkins | dasher-prod-k8s-us-east |
| label | Node label used by node('label') | nodejs-pod |
| containerTemplate.name | Container identifier inside the pod | node-18 |
| containerTemplate.image | Docker image for the container | node:18-alpine |
| command / args | Command to keep container alive for the agent | sleep / 9999999 |
| ttyEnabled | Allocate pseudo-TTY for interactive runs | true |
| stash / unstash | Transfer files between agents | stash(includes: 'node_modules/**', name: 'deps') |
Summary
- Use
podTemplateandcontainerTemplateto define Kubernetes pods and containers for scripted pipelines. - Reference the pod via
node('label')and execute inside a container withcontainer('name'). - Always
checkout scmor usestash/unstashwhen switching between different agents or pods. - Define pod templates either inline in the Jenkinsfile (good for one-off pipelines) or in the Jenkins UI/cloud level (recommended for reuse).