Blog

Role of agent and node in Jenkins pipeline

Role-of-agent-and-node-in-Jenkins-pipeline
Jenkins

Role of agent and node in Jenkins pipeline

“agent” and “node” are terms used to define where and how the stages of your pipeline will be executed in Jenkins. They refer to different concepts but are related to the framework and environment in which your pipeline runs.

Agent:
An agent in a Jenkins pipeline describes the machine, container, or environment in which a stage or block of stages will be executed. It is used to assign resources and control where the pipeline runs. Agents can be defined at the top level of a pipeline or within a specific stage.

Using an agent at the top level:


pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                // Build steps here
            }
        }
    }
}

In the above example, the agent any line means that the pipeline can run on any available agent.

Node:
In Jenkins a node represents a single machine or workspace that is one part of the Jenkins environment. Nodes are used to share the workload of a Jenkins build or pipeline. They can be physical machines, virtual machines, or containers. A “node” block can be used within a pipeline stage to specify where that specific stage should run.

Using a node within a stage:

pipeline {
    agent any

    stages {
        stage('Build') {
            agent {
                node {
                    label 'my-node'
                }
            }
            steps {
                // Build steps here
            }
        }
    }
}

In the above example, the node block within the agent block specifies that the “Build” stage should run on a node with the label “my-node”.

In abstract, while “agent” is a top-level directive that describes the default agent/environment for the entire pipeline, “node” is used within a specific stage to specify where that stage should run, allowing you to fine-tune the execution environment for different parts of your pipeline.

Spread the love

Leave your thought here

Your email address will not be published. Required fields are marked *