0
0
Jenkinsdevops~10 mins

Labels for agent selection in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Labels for agent selection
Start Jenkins Job
Check Job's Agent Label
Find Agent with Matching Label
Assign Job to Agent
Run Job on Agent
Job Complete
Jenkins uses labels to find an agent that matches the job's requested label, then runs the job on that agent.
Execution Sample
Jenkins
pipeline {
  agent { label 'linux' }
  stages {
    stage('Build') {
      steps { echo 'Building on Linux agent' }
    }
  }
}
This Jenkins pipeline runs the build stage on an agent labeled 'linux'.
Process Table
StepActionLabel CheckedAgent FoundJob AssignedOutput
1Start joblinuxNoNoWaiting for agent
2Search agentslinuxAgent1 (linux)NoFound agent with label 'linux'
3Assign joblinuxAgent1 (linux)YesJob assigned to Agent1
4Run joblinuxAgent1 (linux)YesOutput: Building on Linux agent
5Job completelinuxAgent1 (linux)YesJob finished successfully
💡 Job completes after running on agent with matching label 'linux'
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Agent SelectedNoneAgent1 (linux)Agent1 (linux)Agent1 (linux)Agent1 (linux)
Job StatusWaitingWaitingAssignedRunningCompleted
Key Moments - 3 Insights
Why does the job wait at the start before running?
The job waits because Jenkins is searching for an available agent with the matching label, as shown in step 1 and 2 of the execution table.
What happens if no agent has the requested label?
The job will stay in the waiting state and not run, because Jenkins cannot assign it to any agent without the matching label, as seen in step 1 where no agent is found.
Can a job run on an agent without the requested label?
No, Jenkins strictly assigns the job only to agents that have the requested label, ensuring the job runs in the correct environment, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the job assigned to an agent?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Check the 'Job Assigned' column in the execution table.
According to the variable tracker, what is the job status after step 4?
AWaiting
BRunning
CAssigned
DCompleted
💡 Hint
Look at the 'Job Status' row after step 4 in the variable tracker.
If no agent has the label 'linux', what would happen to the job status?
AIt would stay waiting indefinitely
BIt would run on any available agent
CIt would fail immediately
DIt would run on the master node
💡 Hint
Refer to the key moment about what happens if no agent has the requested label.
Concept Snapshot
Jenkins uses labels to select agents for jobs.
Specify label in pipeline with agent { label 'name' }.
Jenkins finds an agent with that label.
Job runs only on matching labeled agent.
If no agent matches, job waits.
Labels ensure correct environment for jobs.
Full Transcript
This visual execution shows how Jenkins uses labels to select agents for running jobs. The job starts and Jenkins checks the requested label 'linux'. It searches for an agent with that label and finds Agent1. The job is assigned to Agent1 and runs there, producing the output 'Building on Linux agent'. The job completes successfully. Variables like 'Agent Selected' and 'Job Status' change step-by-step, showing the job moving from waiting to running to completed. Key moments explain why the job waits if no agent matches and why it cannot run on agents without the label. The quiz tests understanding of when the job is assigned, job status changes, and what happens if no matching agent exists.