108

How do I tell Jenkins to run a specific project on a particular slave?

I've set up a Jenkins master node, and a slave node that I want to use for staging an application. But I can't figure out how to configure the project to run on the slave node I created.

Lorin Hochstein
  • 4,868
  • 15
  • 54
  • 72
  • 1
    http://stackoverflow.com/questions/9214501/jenkins-running-a-single-job-in-master-as-well-as-slave – kaji Feb 14 '12 at 04:05

4 Answers4

103

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.

See this reference documentation:

gareth_bowles
  • 8,867
  • 9
  • 33
  • 42
  • Thanks. I used quite a while before found your answer. It should have added to this page https://wiki.jenkins-ci.org/display/JENKINS/Distributed+builds – Surasin Tancharoen Jun 04 '15 at 13:56
29

If you are running a Pipeline job, you first want to add a label (e.g. 'slave') to the slave node (or agent as it seems to be called now).

Then, in the pipeline script, you specify the label the job runs on:

Declarative pipeline:

pipeline {
    agent {label 'slave'}
    stages {
        ...
    }
}

Scripted pipeline:

node (label: 'slave') {
    ...
}

This job will now run on any node with the label 'slave'. If you only want the job to run on this particular slave, don't reuse the label. And of course the label doesn't have to be 'slave'; it can be whatever you want.

Update:

In the scripted pipeline, if your node is named "My Node", you can also do this:

node ('My Node') {
    ...
}

If you only want the code block to run on that particular node, this is useful. However, using labels is more flexible, and can make it easier to add nodes to share the workload.

jpyams
  • 435
  • 4
  • 7
  • This was so hard to find, the documentation for scripted syntax is dying in favour of declarative, I cannot migrate yet so thank you! – Mitchell Currie Mar 06 '18 at 05:37
8

If you want to use Node/Label as a parameter then install NodeLabel Parameter Plugin

Alex Okrushko
  • 181
  • 1
  • 2
  • 1
    Note that if choose "Node" parameter for a job and you want to call it from Groovy then you need to create a special parameter: `params += new org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterValue('testServer', testServerLabel);` (instead of the usual `StringParameterValue`). – Nux Jun 07 '19 at 14:49
  • @Nux I can't know for sure because, well, "crisis adverted", but I'm assuming seeing your comment saved me hours of headache. Thanks! – aakoch Nov 20 '19 at 16:12
1

Easy way to do this issue.

pipeline {
agent {label 'node1'}

stages {
    stage('Hello') {
        steps {
            echo 'Hello World'
        }
    }
}

}

enter image description here

enter image description here

Willie Cheng
  • 111
  • 3