📖 Scenario: You are managing a Jenkins server that uses multiple agents (nodes) to run jobs. Sometimes agents go offline due to maintenance or network issues. You want to track which agents are online and which are offline to manage your builds better.
🎯 Goal: Build a simple Jenkins pipeline script that lists all agents, checks their availability status, and prints whether each agent is online or offline.
📋 What You'll Learn
Use Jenkins pipeline Groovy script syntax
Access Jenkins instance to get all agents
Check each agent's availability status
Print agent name and status (online/offline)
💡 Why This Matters
🌍 Real World
In real Jenkins environments, agents can go offline due to maintenance or network issues. Monitoring their availability helps keep builds running smoothly.
💼 Career
DevOps engineers and Jenkins administrators need to manage agent availability to ensure continuous integration and delivery pipelines are reliable.
Progress0 / 4 steps
1
Get all Jenkins agents
Write a Jenkins pipeline Groovy script line to get all agents from the Jenkins instance and store them in a variable called agents.
Jenkins
Hint
Use Jenkins.instance.nodes to get the list of all agents.
2
Prepare a status map
Create an empty map called agentStatus to store each agent's name and its online/offline status.
Jenkins
Hint
Use [:] to create an empty map in Groovy.
3
Check each agent's availability
Use a for loop with variable agent to iterate over agents. Inside the loop, check if agent.getComputer().isOnline() is true. If yes, set agentStatus[agent.getNodeName()] = 'online', else set it to 'offline'.
Jenkins
Hint
Use agent.getComputer().isOnline() to check if the agent is online.
4
Print agent availability status
Use a for loop with variables name and status to iterate over agentStatus. Print each agent's name and status in the format: Agent: <name> is <status>.
Jenkins
Hint
Use println("Agent: ${name} is ${status}") to print the status.
Practice
(1/5)
1. What does it mean when a Jenkins agent is marked as offline?
easy
A. The agent is connected and ready to accept jobs.
B. The agent is not available to run any jobs.
C. The agent is running a job currently.
D. The agent is permanently deleted from Jenkins.
Solution
Step 1: Understand agent status meaning
In Jenkins, an agent marked offline means it is not available to run jobs.
Step 2: Differentiate from other statuses
Online agents are connected and ready; offline means unavailable temporarily or for maintenance.
Final Answer:
The agent is not available to run any jobs. -> Option B
Quick Check:
Offline agent = unavailable [OK]
Hint: Offline means agent can't run jobs now [OK]
Common Mistakes:
Confusing offline with running a job
Thinking offline means deleted
Assuming offline means ready
2. Which of the following is the correct way to mark a Jenkins agent as offline using the Jenkins CLI?
easy
A. jenkins-cli offline-agent --name agent1
B. java -jar jenkins-cli.jar offline-agent agent1
C. java -jar jenkins-cli.jar offline-agent --agent agent1
D. java -jar jenkins-cli.jar disconnect-node agent1
Solution
Step 1: Recall Jenkins CLI command for offline
The correct command to mark an agent offline is disconnect-node with the agent name as argument.
Step 2: Check syntax correctness
Options A, B, and C use incorrect commands or flags; only java -jar jenkins-cli.jar disconnect-node agent1 uses the correct command and syntax.
Final Answer:
java -jar jenkins-cli.jar disconnect-node agent1 -> Option D
Quick Check:
disconnect-node command marks offline [OK]
Hint: Use 'disconnect-node' command with agent name [OK]
If agent1 is offline, what will happen when this pipeline runs?
medium
A. The pipeline will fail immediately with an error.
B. The pipeline will run on the master node instead.
C. The pipeline will wait until agent1 comes online.
D. The pipeline will skip the node block and continue.
Solution
Step 1: Understand node block behavior
The node('agent1') block waits for the specified agent to be available before running.
Step 2: Check what happens if agent is offline
If agent1 is offline, Jenkins will wait until it comes online or timeout occurs.
Final Answer:
The pipeline will wait until agent1 comes online. -> Option C
Quick Check:
node waits for agent online [OK]
Hint: node waits for specified agent to be online [OK]
Common Mistakes:
Assuming pipeline fails immediately
Thinking it runs on master automatically
Believing it skips the node block
4. You have a Jenkins agent that should be offline for maintenance, but jobs are still assigned to it. What is the most likely cause?
medium
A. The agent was not properly marked offline in Jenkins configuration.
B. The agent is offline but Jenkins is ignoring the status due to a plugin bug.
C. The agent was marked offline but the 'Accept tasks' option is still enabled.
D. The jobs are configured to run only on the master node.
Solution
Step 1: Check agent offline marking
If jobs are still assigned, likely the agent was not properly marked offline in Jenkins UI or configuration.
Step 2: Eliminate other causes
The agent was marked offline but the 'Accept tasks' option is still enabled. is incorrect because offline agents do not accept tasks; The agent is offline but Jenkins is ignoring the status due to a plugin bug. is rare; The jobs are configured to run only on the master node. is unrelated.
Final Answer:
The agent was not properly marked offline in Jenkins configuration. -> Option A
Quick Check:
Proper offline marking stops job assignment [OK]
Hint: Ensure agent is correctly marked offline in Jenkins UI [OK]
Common Mistakes:
Assuming offline agent still accepts tasks
Blaming plugins without checking config
Confusing job node assignment
5. You want to create a Jenkins pipeline that automatically skips running on an agent if it is offline and instead runs on the master node. Which scripted pipeline snippet achieves this?
hard
A. if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) {
node('agent1') { echo 'Running on agent1' }
} else {
node('master') { echo 'Running on master' }
}
B. node('agent1') {
if (agent1.isOnline()) {
echo 'Running on agent1'
} else {
node('master') { echo 'Running on master' }
}
}
C. try {
node('agent1') { echo 'Running on agent1' }
} catch (Exception e) {
node('master') { echo 'Running on master' }
}
D. node('agent1') {
echo 'Running on agent1'
}
node('master') {
echo 'Running on master'
}
Solution
Step 1: Check agent online status in pipeline
if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) {
node('agent1') { echo 'Running on agent1' }
} else {
node('master') { echo 'Running on master' }
} correctly checks if 'agent1' is online using Jenkins API before choosing node.
Step 2: Verify fallback to master node
If agent1 is offline, if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) {
node('agent1') { echo 'Running on agent1' }
} else {
node('master') { echo 'Running on master' }
} runs the job on 'master' node, matching the requirement.
Step 3: Analyze other options
node('agent1') {
if (agent1.isOnline()) {
echo 'Running on agent1'
} else {
node('master') { echo 'Running on master' }
}
} uses invalid syntax; try {
node('agent1') { echo 'Running on agent1' }
} catch (Exception e) {
node('master') { echo 'Running on master' }
} relies on exception which is not best practice; node('agent1') {
echo 'Running on agent1'
}
node('master') {
echo 'Running on master'
} runs both nodes unconditionally.
Final Answer:
if (Jenkins.instance.getNode('agent1').toComputer().isOnline()) {
node('agent1') { echo 'Running on agent1' }
} else {
node('master') { echo 'Running on master' }
} -> Option A
Quick Check:
Check agent online status before node selection [OK]
Hint: Check agent online status via Jenkins API before node [OK]
Common Mistakes:
Using invalid syntax to check agent status
Relying on exceptions to handle offline agents
Running both nodes regardless of status
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.