0
0
Jenkinsdevops~15 mins

Labels for agent selection in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Labels for agent selection
📖 Scenario: You are setting up a Jenkins pipeline to run jobs on specific agents (nodes) based on labels. Labels help Jenkins pick the right machine to run your tasks.
🎯 Goal: Create a Jenkins pipeline script that uses labels to select the correct agent for running a job.
📋 What You'll Learn
Create a pipeline with a label selector for the agent
Define a label variable to specify the agent label
Use the label variable in the agent block
Print a message showing which agent label is used
💡 Why This Matters
🌍 Real World
In real Jenkins setups, labels help direct jobs to the right machines with needed software or resources.
💼 Career
Knowing how to use labels in Jenkins pipelines is essential for DevOps engineers to manage build and deployment environments efficiently.
Progress0 / 4 steps
1
Create a label variable
Create a variable called agentLabel and set it to the string "linux".
Jenkins
Need a hint?

Use def agentLabel = "linux" to create the label variable.

2
Use the label variable in the agent block
Add an agent block to the pipeline that uses the label stored in agentLabel to select the agent.
Jenkins
Need a hint?

Use agent { label agentLabel } to select the agent by label.

3
Add a stage to print the agent label
Inside the stages block, add a stage named Print Label with a step that prints the message "Agent label is: ${agentLabel}".
Jenkins
Need a hint?

Add a new stage with stage('Print Label') and use echo to print the label.

4
Print the final output
Run the pipeline and ensure the output includes the line Agent label is: linux.
Jenkins
Need a hint?

Look for the output line Agent label is: linux in the console log.