Certified Jenkins Engineer

Agents and Nodes in Jenkins

Types of Agents

Agents extend the Jenkins controller by running executors on remote nodes. In your Jenkinsfile, you declare the agent block to control where and how each pipeline stage executes. Agents handle the communication protocol, authentication, and tool resolution required for build and test jobs.

Controller-Worker Architecture

Jenkins controllers delegate work to agents over SSH or JNLP. All required build tools must be installed on the agent node.

The image illustrates the Jenkins architecture, showing the relationship between the Jenkins Controller Node and Jenkins Worker Nodes (Linux and Windows) using SSH and JNLP connections.

Static vs. Containerized Agents

Besides static (permanent) agents, you can leverage Docker or Kubernetes to provision agents on demand. Container-based agents launch a clean environment each build, ensuring consistency and eliminating dependency conflicts.

The image describes aspects of Jenkins architecture, highlighting the use of Docker containers for build agents, ideal builds with specific dependencies, and ensuring isolated environments.

Common Agent Types

Below is a quick reference of the most widely used Jenkins agent types:

Agent TypeDescriptionUse Case
Permanent AgentsDedicated nodes with pre-installed tools (e.g., JDK, Node.js)Stable, consistent environments
Docker AgentsEphemeral containers spun up per build with required dependenciesIsolation, reproducibility
Cloud-Based AgentsOn-demand VMs or pods in cloud providers (AWS, Azure, Kubernetes)Elastic scaling, pay-per-use
Label-Based AgentsNodes tagged by labels to match specific pipeline requirementsDecoupling builds from physical topology

Warning

Permanent agents remain online even when idle, consuming resources. Consider ephemeral Docker or cloud agents for variable workloads.

The image lists four types of agents: Permanent Agents, Docker Agents, Cloud-Based Agents, and Label-Based Agents, each with a corresponding icon.

Agent Declarations in a Jenkinsfile

Use the declarative pipeline syntax to specify where your pipeline runs:

1. agent any

Runs the entire pipeline on any available agent.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo Running on ${NODE_NAME}'
            }
        }
    }
}

2. Label-Based Agent

Targets a node with a specific label. Ensure a node labeled my-agent exists.

pipeline {
    agent { label 'my-agent' }
    stages {
        stage('Build') {
            steps {
                sh 'echo Running on ${NODE_NAME}'
            }
        }
    }
}

3. Docker Agent

Spins up a Docker container using the specified image and arguments.

Note

Ensure Docker is installed and the Jenkins user has permissions to run containers on this agent.

pipeline {
    agent {
        docker {
            image 'node:latest'               // Node.js image
            args  '-v $HOME/.npm:/root/.npm'  // Mount NPM cache
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm test'
            }
        }
    }
}

4. Default and Stage-Level Agents

Defines a global agent and overrides it for specific stages.

pipeline {
    agent { label 'my-agent' } // Default for all stages
    stages {
        stage('Build') {
            agent { label 'nodejs-agent' } // Stage-specific agent
            steps {
                sh 'echo Running on ${NODE_NAME}'
            }
        }
        stage('Test') {
            steps {
                sh 'echo Tests running on default agent'
            }
        }
    }
}

This approach lets you mix general-purpose agents with specialized environments for individual stages.

Watch Video

Watch video content

Previous
Demo Load TrivyScanScript Library in Jenkins Pipeline