0
0
Jenkinsdevops~30 mins

Agent availability and offline handling in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Agent availability and offline handling
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use println("Agent: ${name} is ${status}") to print the status.