1. Introduction
Jenkins automates various software development processes as a widely used open-source automation server. This is achieved through pipelines that are versioned, scripted, and typically defined using domain-specific language (DSL). With these pipelines, understanding the difference between an agent and a node is important for effective pipeline configuration and execution.
While agent and node are used to define where and how the stages of pipelines are executed in Jenkins. They refer to different concepts in the Jenkins pipeline.
In this tutorial, we’ll learn the roles of agent and node in a Jenkins pipeline and explain how they contribute to the Continuous Integration/Continuous Deployment (CI/CD) process.
2. Understanding Jenkins Pipeline
Before we look at agent and node, it’s important to understand the basic structure of a Jenkins pipeline. A pipeline in Jenkins is a series of automated steps to build, test, and deploy an application. Furthermore, it consists of one or more stages. In particular, each of these stages performs specific tasks such as code checkout, compilation, testing, or deployment:
Additionally, the components of a Jenkins pipeline include:
- stages
- steps
- agent
- node
Now, let’s dive into the specifics of agent and node and explore their differences.
3. What Is an Agent?
An agent in a Jenkins pipeline refers to the location (machine, container, or environment) where the entire pipeline or a specific stage runs. Additionally, we define it at the top level of the pipeline or within individual stages. Thus, provides flexibility in specifying where tasks are executed.
Let’s consider an example agent at the top level:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
agent {
label 'test-node'
}
steps {
echo 'Testing...'
}
}
}
}
In this example, the agent any directive at the top level indicates that the pipeline can run on any available Jenkins node. Additionally, the agent directive within the Test stage specifies that this stage should run on a node labeled test-node.
Furthermore, the types of agents in a Jenkins pipeline include:
- any: runs on any available node
- label: runs on a node with the specified label
- docker: runs in a docker container
- none: has no global agent
4. What Is a Node?
In the Jenkins pipeline, a node represents a machine (physical or virtual) that is part of the Jenkins infrastructure and capable of executing pipeline tasks. It’s part of the Jenkins master-slave architecture. This is where the master schedules and manages tasks, and nodes (often referred to as slaves) perform the actual work.
Furthermore, we define node through agent directives. However, we can explicitly allocate and use a node within a script block:
pipeline {
agent none
stages {
stage('Build') {
steps {
script {
node('build-node') {
echo 'Building on build-node...'
}
}
}
}
}
}
In this example, the pipeline has no global agent (see the script where set as agent none). Additionally, the Build stage explicitly allocates the node labeled build-node for executing its step.
Node in Jenkins pipeline has unique characteristics some of which are:
- Labels: these are tags used to group and identify nodes based on capabilities (examples include: Linux, windows, and docker)
- Workspace: it’s the directory on the node where the pipeline runs and stores file
- Executor: the number of concurrent tasks a node can handle
5. Key Differences Between Agent and Node
The main difference between an agent and a node is that an agent represents the entire pipeline or a specific stage runs while a node represents a specific machine within the Jenkins infrastructure.
Further, here are some other differences between agent and node:
Property
agent
node
Scope
It’s more abstract and can encompass multiple nodes based on the agent type
It’s a more concrete entity that physically executes the tasks
Usage
It’s used in the pipeline to specify the execution environment
It’s used within script blocks to allocate specific machines for tasks
Flexibility
It provides higher-level control over pipeline execution
It offers granular control within a script block
6. Use Cases and Common Configuration Strategies
Even if we’ve learned the differences between agent and node, it’s important to know how to apply them in real-world projects. Let’s explore some use cases and configuration strategies of agents and nodes in Jenkins pipelines.
6.1. Use Case for Agent
Let’s explore an example of using an agent for different stages. In most cases, we could have different stages of the pipeline running on different types of machines or environments. For instance, the build stage can run on any available node, while the test stage runs on a specific node with particular capabilities:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
agent {
label 'test-node'
}
steps {
echo 'Testing...'
}
}
}
}
For this pipeline configuration, the agent any directive at the top level lets the build stage run on any available node. However, the test stage specifies an agent labeled test-node. This ensures it runs on a node with specific characteristics.
Additionally, for projects that require isolated environments, we can use docker containers as agents. In particular, it ensures consistency across builds and allows for easy setup of the required dependencies:
pipeline {
agent {
docker {
image 'maven:3-alpine'
}
}
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
}
}
In this configuration, the agent is configured to use a docker container with the Maven image. Further, the build stage runs within this container. It ensures all necessary tools and dependencies are available in a consistent environment.
6.2. Use Case for Node
Sometimes, we need a more granular control over the nodes where the tasks run. We can achieve this by defining nodes explicitly within script blocks. This approach is useful when we dynamically allocate nodes based on certain requirements:
pipeline {
agent none
stages {
stage('Build') {
steps {
script {
node('build-node') {
echo 'Building on build-node...'
}
}
}
}
}
}
As shown in this configuration, the pipeline has no global agent. In particular, it’s indicated by the agent none. Instead, the build stage explicitly allocates the node labeled build-node for executing its steps.
7. Conclusion
In this article, we’ve explored the roles of agent and node within a Jenkins pipeline. Furthermore, we highlighted their key differences and functions. Moreover, understanding these components is crucial for effectively configuring and executing pipelines.
Furthermore, we explored practical use cases and configuration strategies, demonstrating how to apply these concepts to real-world scenarios.
By leveraging the flexibility of the agent and the specificity of the nodes, we can optimize CI/CD processes for better performance and efficiency.