How to Run on a Kubernetes Agent in Jenkins
A kubernetes agent block spins up a pod per build, then tears it down when done.
With the Kubernetes plugin, define an agent with a pod YAML. Each container in the pod becomes a place to run steps via the container() block, isolated per build.
A pod-templated build agent
Declare a pod with a build container and target it with container().
Jenkinsfile
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: node
image: node:20
command: ["sleep"]
args: ["99d"]
'''
}
}
stages {
stage('Test') {
steps {
container('node') {
sh 'npm ci && npm test'
}
}
}
}
}Notes
- The jnlp container is added automatically to connect the pod back to the controller.
- Pods are ephemeral, so each build starts clean; teams wanting managed ephemeral runners without operating Kubernetes can migrate to GitHub Actions on Latchkey.
Related guides
How to Set Build Retention in JenkinsKeep Jenkins disk usage in check by setting build retention with buildDiscarder in the pipeline options so ol…
How to Use environment Credentials in JenkinsInject stored Jenkins credentials into pipeline steps with the credentials() helper in an environment block s…