Labels for agent selection in Jenkins - Commands & Configuration
jenkins-agent --labels linux,java
--labels - Assigns labels to the agent for job selectionjenkins-jobs --label linux build my-pipeline
--label - Specifies the label to select the agent for running the jobjenkins-cli build my-pipeline -p LABEL=java
If you remember nothing else from this pattern, remember: Labels let Jenkins pick the right agent to run your job based on the agent's capabilities.
Practice
What is the main purpose of using labels in Jenkins agent selection?
Solution
Step 1: Understand Jenkins agent labels
Labels are tags assigned to agents to identify their capabilities or roles.Step 2: Purpose of labels in job assignment
Jenkins uses these labels to select the right agent that matches the job requirements.Final Answer:
To tell Jenkins which agent can run a specific job -> Option CQuick Check:
Labels guide agent selection = A [OK]
- Thinking labels change UI or store logs
- Confusing labels with system settings
Which of the following is the correct syntax to select an agent with label linux and docker in Jenkins pipeline?
Solution
Step 1: Understand label expressions in Jenkins
Jenkins uses logical operators like && for AND and || for OR in label expressions.Step 2: Correct syntax for AND condition
To require both labels, use 'linux && docker' inside the label field.Final Answer:
agent { label 'linux && docker' } -> Option DQuick Check:
AND labels use && = D [OK]
- Using space-separated labels without operators
- Using commas instead of logical operators
- Confusing AND (&&) with OR (||)
Given this Jenkins pipeline snippet, what agent will run the job if agents have labels linux, docker, and gpu?
agent { label 'linux && (docker || gpu)' }Solution
Step 1: Analyze label expression
The expression requires the agent to have the 'linux' label AND either 'docker' OR 'gpu' label.Step 2: Determine matching agents
Agents with 'linux' plus 'docker' or 'linux' plus 'gpu' satisfy the condition.Final Answer:
An agent with linux and either docker or gpu labels -> Option AQuick Check:
linux AND (docker OR gpu) = A [OK]
- Thinking all three labels are required
- Ignoring parentheses in label expressions
- Assuming OR means both labels must be present
What is wrong with this Jenkins pipeline agent label expression that causes the job to wait indefinitely?
agent { label 'linux && docker || gpu' }Solution
Step 1: Understand operator precedence in label expressions
Logical AND (&&) has higher precedence than OR (||), so expression is parsed as '(linux && docker) || gpu'.Step 2: Identify why job waits
If no agent has both linux and docker, but some have gpu alone, job may pick gpu agents. If gpu agents are missing or busy, job waits.Final Answer:
Missing parentheses causing wrong operator precedence -> Option BQuick Check:
Use parentheses to control label logic = C [OK]
- Ignoring operator precedence in label expressions
- Using wrong quote types
- Believing logical operators are not allowed
You want a Jenkins job to run only on agents that have linux and docker labels but never on agents with gpu label. Which label expression should you use?
Solution
Step 1: Understand label requirements
The job must run on agents with both 'linux' and 'docker' labels, but exclude any with 'gpu'.Step 2: Construct correct label expression
Use AND (&&) for required labels and NOT (!) to exclude 'gpu': 'linux && docker && !gpu'.Final Answer:
linux && docker && !gpu -> Option AQuick Check:
Include linux and docker, exclude gpu = B [OK]
- Using OR instead of AND for required labels
- Not excluding gpu label properly
- Misplacing parentheses causing wrong logic
Ready to prove it?
Pass the test and this pattern moves unknown
