📖 Scenario: You are setting up a Jenkins pipeline to run your build steps on a specific agent. This helps Jenkins know where to run your tasks, like choosing a particular machine or environment.
🎯 Goal: Build a Jenkins pipeline script that uses the agent directive to specify the node where the pipeline should run.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Use the agent directive to specify the node label linux
Add a simple stage with a steps block that echoes a message
Print the message to confirm the pipeline runs on the specified agent
💡 Why This Matters
🌍 Real World
In real projects, specifying the agent ensures your build runs on the right machine with needed tools.
💼 Career
Knowing how to use the Jenkins agent directive is essential for DevOps engineers to manage build environments efficiently.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with pipeline {} and inside it add an empty agent directive.
Jenkins
Hint
Start with the pipeline block and add an empty agent directive inside it.
2
Specify the agent node label
Modify the agent directive to specify the node label linux using agent { label 'linux' }.
Jenkins
Hint
Use agent { label 'linux' } to tell Jenkins to run on the node labeled 'linux'.
3
Add a stage with a simple echo step
Inside the pipeline block, add a stage named 'Build' with a steps block that runs echo 'Building on linux agent'.
Jenkins
Hint
Add a stage with name 'Build' and inside steps use echo to print the message.
4
Print the pipeline output
Run the pipeline script and ensure it prints Building on linux agent in the console output.
Jenkins
Hint
Run the pipeline in Jenkins and look for the message Building on linux agent in the console output.
Practice
(1/5)
1. What does the agent directive specify in a Jenkins pipeline?
easy
A. The build triggers for the pipeline
B. The version of Jenkins to use
C. The environment variables for the pipeline
D. Where the pipeline or stage should run
Solution
Step 1: Understand the role of agent
The agent directive tells Jenkins where to run the pipeline or a specific stage.
Step 2: Compare other options
Options A, B, and C relate to Jenkins configuration but not the agent directive.
Final Answer:
Where the pipeline or stage should run -> Option D
Quick Check:
Agent directive = run location [OK]
Hint: Agent means where to run your pipeline or stage [OK]
Common Mistakes:
Confusing agent with environment variables
Thinking agent sets build triggers
Assuming agent defines Jenkins version
2. Which of the following is the correct syntax to specify that a Jenkins pipeline should run on any available agent?
easy
A. agent any
B. agent: any
C. agent = any
D. agent: 'any'
Solution
Step 1: Recall correct Jenkins pipeline syntax
The correct syntax to specify any available agent is agent any without colons or equals.
Step 2: Analyze other options
Options A, B, and D use incorrect punctuation or quotes not valid in declarative pipeline syntax.
Final Answer:
agent any -> Option A
Quick Check:
Correct syntax for any agent = agent any [OK]
Hint: Use 'agent any' without punctuation for any agent [OK]
Common Mistakes:
Adding colons or equals signs incorrectly
Using quotes around any
Confusing declarative syntax with scripted syntax
3. Given this Jenkins pipeline snippet, what agent will the Build stage run on?
A. Incorrect agent block syntax, should be agent { label 'docker' }
B. Missing quotes around label name
C. Agent directive cannot use label
D. Pipeline missing stages block
Solution
Step 1: Review agent block syntax
The label value must be enclosed in quotes, like label 'docker'.
Step 2: Identify syntax error
The snippet has label docker without quotes, causing a syntax error as docker is treated as an undefined variable.
Final Answer:
Missing quotes around label name -> Option B
Quick Check:
Agent block syntax must be exact [OK]
Hint: Agent block must be exactly agent { label 'name' } [OK]
Common Mistakes:
Misplacing braces or indentation
Using agent label without braces
Confusing agent label with docker agent syntax
5. You want a Jenkins pipeline that does not allocate any agent by default but runs the Deploy stage on a Docker container with image nginx:latest. Which pipeline snippet correctly achieves this?
hard
A. pipeline {
agent any
stages {
stage('Deploy') {
agent none
steps {
echo 'Deploying with nginx container'
}
}
}
}