Jenkins Pipeline - Will Jenkins use the same agent throughout the pipeline stages for the same job?

I’m working with Jenkins Pipeline, and I have a question regarding agent allocation for different stages in a pipeline. I have a scenario where I have multiple nodes with the same label, and I want to know whether Jenkins will try to use the same agent throughout the pipeline for the same job.

From my understanding, Jenkins will try to use the same agent for all stages of a pipeline when the agent is defined with a label. However, I’d like to find an official document or any supporting reference for this statement. Here’s my scenario:

I have three nodes with the same label:

  • stateful-0
  • stateful-1
  • stateful-2

My Jenkinsfile has the following structure:

pipeline {
agent {
label ‘stateful’
}
stages {
stage(‘Stage 1’) {
steps {
// …
}
}
stage(‘Stage 2’) {
steps {
// …
}
}
stage(‘Stage 3’) {
steps {
// …
}
}
}
}

My question is: Will Jenkins try to use the same agent throughout the pipeline for the same job if the agent is defined with a label? For example, if Stage 1 is running on stateful-1, will Stage 2 and Stage 3 also run on stateful-1?

If possible, is there any official documentation or supporting references to help clarify this behavior.

Thank you!

We use agents exclusively, and we have not experienced a switch of agents during execution of a job.
The main goal of an agent is to move the “build-workload” off the Jenkins-instance itself. By using ephemeral agents, you ensure a clean setup at every start of your jobs.

Is this applicable for scripted pipelines as well ? Is it by design that “all the stages” use the same agent through out the pipeline? Or there is a chance that an agent switch can happen between the stages? We have some stages that run for hours ( UI Tests).

We run a mix of scripted and declarative pipelines. One job gets one agent.
If you use Kubernetes, and have different templates, each container runs separately, albeit in the same pod.