Limiting Jenkins pipeline to running only on specific nodes

jenkins run job on specific node
jenkins restrict where this project can be run
‘jenkins’ is reserved for jobs with matching label expression
jenkins pipeline node
jenkins pipeline node parameter
jenkins pipeline agent
jenkins exclude node
jenkins pipeline run on specific node

I'm building jobs that will be using Jenkins piplines extensively. Our nodes are designated per project by their tags, but unlike regular jobs the pipeline build does not seem to have the "Restrict where this project can be run" checkbox. How can I specify on which node the pipeline will run the way I do for regular jobs?

You specify the desired node or tag when you do the node step:

node('specialSlave') {
   // Will run on the slave with name or tag specialSlave
}

See https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#node-allocate-node for an extended explanation of the arguments to node.

Edit 2019: This answer (and question) was made back in 2017, back then there only was one flavor of Jenkins pipeline, scripted pipeline, since then declarative pipeline has been added. So above answer is true for scripted pipeline, for answers regarding declarative pipeline please see other answers below.

How to trigger a jenkins build on specific node using pipeline plugin , But run on random node instead. I'm wondering if I should set "Restrict where this project can be run" on my "test_job". So I set it to "� Limiting Jenkins pipeline to running only on specific nodes (2) . For the record let's have the declarative pipeline example here as well (choosing a node which has the label 'X'):

For the record let's have the declarative pipeline example here as well (choosing a node which has the label 'X'):

pipeline {
    agent { label 'X' }
...
...
}

Tell Jenkins to run a specific project on a particular slave node , 3 Answers. Set the "Restrict where this job can be run" check box in your job configuration and specify the name of your slave. If you add more slaves later, you can set labels for each slave and specify those in your job configs. pipeline is Declarative Pipeline-specific syntax that defines a "block" containing all content and instructions for executing the entire Pipeline. 2 agent is Declarative Pipeline-specific syntax that instructs Jenkins to allocate an executor (on a node) and workspace for the entire Pipeline.

To be clear, because Pipeline has two Syntax, there are two ways to achieve that.

Declarative
pipeline {
    agent none

    stages {
        stage('Build') {
            agent { label 'slave-node​' }
            steps {
                echo 'Building..'
                sh '''
                '''
            }
        }
    }

    post {
        success {
            echo 'This will run only if successful'
        }
    }
}
Scripted
node('your-node') {
  try {

    stage 'Build'
    node('build-run-on-this-node') {
        sh ""
    }
  } catch(Exception e) {
    throw e
  }
}

Restricting Pipeline Job to run on specific slaves, To use Google Groups Discussions, please enable JavaScript in your browser settings, and then refresh this page. <plaintext/><xmp>. ↰ Jenkins Users. Delete. Jenkins 2.x or later (older versions back to 1.642.3 may work but are not recommended) Pipeline plugin, [] which is installed as part of the "suggested plugins" (specified when running through the Post-installation setup wizard after installing Jenkins).

Job Restrictions, Allows restricting execution of jobs on nodes depending on criterias jobs according to the specified name pattern (e.g. allow running only "QA_. Solves the issue with restricting Pipelines within Folders (JENKINS-36626)� Chris Williams added a comment - 2017-03-09 19:16 Jesse Glick Hey Jesse, I get that it's a no-no to try and modify the env global across multiple nodes. I'm curious what the expectations/best practices are for builds where we use multiple nodes and expect that the local env vars would be available within that node block scope.

Pipeline Syntax, If true, run the container on the node specified at the top-level of the Pipeline, in the Only run the steps in post if the current Pipeline's or stage's run has a different Scripted provides very few limits, insofar that the only limits on structure and� The content driving this site is licensed under the Creative Commons Attribution-ShareAlike 4.0 license. UI f8815d2 / API 290e611 2020-07-29T01:26:06.000Z

Pipeline: Nodes and Processes, For example, if you have specific knowledge that a given process is going to be Allocates an executor on a node (typically a slave) and runs further code in the or any other label expression like linux && 64bit to restrict where this step builds. windows && jdk9: This block may be executed only on any Windows agent� Jenkins agent nodes offloads builds from the master node, thus performing all the pipeline work specified in the node block. Detailed information on this can be found at Jenkins Distributed builds .

Comments
  • Did you find a solution for this? I am setting up multibranch pipeline and would like to restrict the build on specific slave, thanks
  • This answer is the only one worked. I tried this: { node { label 'labelName' } }, but not work.
  • And if you want to run on a node with one of several labels: "agent { label 'X||Y' }"
  • @nsantos it keeps surprising me how bad Jenkins docs are. It doesn't mention that several labels thing in the docs. Thanks!
  • @nsantos any idea how to do label.startswith 'Prefix'?