How to Configure Jenkins Pipeline to Run Jobs on Specific Slave Nodes


2 views

When working with Jenkins distributed builds, you often need to control which jobs run on which nodes. This becomes particularly important when you have specialized hardware requirements or need to segregate environments (like staging vs production).

The most common approach is using node labels:

pipeline {
    agent {
        label 'staging-node'
    }
    stages {
        stage('Build') {
            steps {
                echo "Running on staging node"
            }
        }
    }
}

First, configure your slave node in Jenkins:

  1. Go to Manage Jenkins > Manage Nodes and Clouds
  2. Click on your slave node
  3. Add labels like "staging-node" in the Labels field
  4. Save the configuration

For precise control, you can specify the exact node name:

pipeline {
    agent {
        node {
            label 'staging-slave-01'
        }
    }
    // Rest of your pipeline
}

You can combine node selection with conditional logic:

pipeline {
    agent none
    stages {
        stage('Deploy to Staging') {
            when {
                branch 'staging'
            }
            agent {
                label 'staging-node'
            }
            steps {
                echo "Deploying to staging environment"
            }
        }
    }
}

If your job isn't running on the intended node:

  • Verify the node is online in Jenkins > Manage Nodes
  • Check for label typos (case-sensitive)
  • Ensure the node has sufficient executors available
  • Review node restrictions in the configuration

For production environments, consider:

pipeline {
    options {
        lock(resource: 'staging-server', inversePrecedence: true) 
    }
    agent {
        label 'staging-node'
    }
    // Pipeline continues
}

This ensures exclusive access to critical resources and prevents concurrent deployments that might conflict.


When working with Jenkins distributed builds, you need to explicitly specify which slave node should execute your pipeline. The configuration happens in either the Jenkinsfile (for Pipeline projects) or in the project configuration UI (for freestyle projects).

The most common approach is to use the agent directive with a label that matches your slave node:

pipeline {
    agent {
        label 'staging-node' // matches your slave node's label
    }
    stages {
        stage('Build') {
            steps {
                echo 'Running on staging node'
            }
        }
    }
}

If you know the exact node name, you can specify it directly:

pipeline {
    agent {
        node {
            label 'specific-slave-name'
            customWorkspace '/var/jenkins/workspace'
        }
    }
    stages {
        // your stages here
    }
}

To confirm your job is running on the correct node, add this diagnostic step:

stage('Verify Node') {
    steps {
        script {
            echo "Running on node: ${env.NODE_NAME}"
            echo "Node labels: ${env.NODE_LABELS}"
        }
    }
}

When your slave node has specific tools installed, configure tool locations in Jenkins global configuration first, then reference them:

pipeline {
    agent {
        label 'node-with-special-tools'
    }
    tools {
        maven 'M3'
        jdk 'JDK11'
    }
    // rest of your pipeline
}

If your job isn't running on the intended slave:

  • Verify the slave node is online (Manage Jenkins > Nodes)
  • Check the node's label matches your pipeline specification
  • Ensure the node has sufficient executors available
  • Confirm the node meets all job requirements (RAM, disk space, etc.)