How do `agent` directives work (any, none, label, docker, kubernetes)?
IntermediateConceptJenkins
Answer
agent says where a pipeline or stage runs. agent any uses any available node. agent none at the top means no global agent, so each stage must declare its own (useful to avoid holding an executor during an input wait). agent { label 'x' } selects by label. agent { docker { image 'maven:3.9-eclipse-temurin-21' } } runs steps in a container on a Docker-capable node. agent { kubernetes { yaml '...' } } creates a pod from a spec.
pipeline {
agent none
stages {
stage('Build') {
agent { docker { image 'node:22' args '-v $HOME/.npm:/root/.npm' } }
steps { sh 'npm ci && npm run build' }
}
}
}
More on Jenkins
What is Jenkins and how is its architecture organised?BeginnerWhat are executors, nodes and labels in Jenkins?IntermediateWhy should you not run builds on the Jenkins controller?BeginnerHow do inbound (JNLP/WebSocket) and SSH agents connect to the controller?IntermediateWhat is a Jenkinsfile and where does it live?BeginnerDeclarative versus Scripted Pipeline: what are the differences?Intermediate