<p>There is no perfect way to do this that I know of.  To explain why, take this pipeline script as an example:</p>

<pre><code>node('my-first-node') {
  stage('my-first-stage') {
    // some steps here 
  }
}

node('my-second-node') {
  stage('my-second-stage') {
    // some steps here
  }
}

node('my-first-node') {
  stage('my-third-stage') { // must be run after my-second-stage
    // some steps here
  }
}
</code></pre>

<p>This will run your stages in the correct order, but there is no guarantee that the node assigned to run my-first-stage will be the same node as the one assigned to run my-third-stage.  Furthermore, even if you can be confident that they are the same node (for instance, you only have one node with the "my-first-node" label in your executor pool), there is no guarantee that the assigned workspace folder will be the same for both stages either.  And even if you use the <a href="https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#ws-allocate-workspace" rel="nofollow noreferrer">workspace step to manually assign a workspace directory</a>, there is no guarantee that the assigned workspace directory will be the one you asked for either.</p>

<p>However, if you don't care that my-first-stage and my-third-stage run in the same workspace directory on the same node and you only care about the order that your steps run in, this works fine.</p>

CC BY-SA 4.0
2019-05-13T15:59:08.423Z
2019-05-13T15:59:08.423Z
jayhendren
<p>I found exactly what I was looking for- </p>

<p>[Jenkins plugin] (<a href="https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin" rel="nofollow noreferrer">https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin</a>)</p>

<p>Which let you create a lock between, slaves , different jobs and more..</p>

<p>(in contrast to the plugin description)</p>

CC BY-SA 4.0
2019-05-15T17:20:32.327Z
2019-05-15T17:20:32.327Z
amichib
<p>I need to run commands on my pipeline during a certain stage, on devices which connected to my Jenkins as slaves.
But I want them to run only when another one finished.
On the other hand, I want them to run all previous stages before they "wait" for another device to finish.</p>

<p>What is the best way to implement this lock? Again, it is the same job, but different slaves.</p>

<jenkins-pipeline><groovy>
Jenkins pipeline lock resources
amichib
# Jenkins pipeline lock resources

(Asked by: username_0 on 2019-05-12)

I need to run commands on my pipeline during a certain stage, on devices which connected to my Jenkins as slaves.
But I want them to run only when another one finished.
On the other hand, I want them to run all previous stages before they "wait" for another device to finish.

What is the best way to implement this lock? Again, it is the same job, but different slaves.

**Tags:** jenkins-pipeline, groovy


---

## 2 answers

### Answer by username_0 on 2019-05-15. Score: 4 | Accepted answer

I found exactly what I was looking for-

[Jenkins plugin] (<https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin>)

Which let you create a lock between, slaves , different jobs and more..

(in contrast to the plugin description)


---

### Answer by username_1 on 2019-05-13. Score: 0

There is no perfect way to do this that I know of.  To explain why, take this pipeline script as an example:
node('my-first-node') {
  stage('my-first-stage') {
    // some steps here 
  }
}

node('my-second-node') {
  stage('my-second-stage') {
    // some steps here
  }
}

node('my-first-node') {
  stage('my-third-stage') { // must be run after my-second-stage
    // some steps here
  }
}

This will run your stages in the correct order, but there is no guarantee that the node assigned to run my-first-stage will be the same node as the one assigned to run my-third-stage.  Furthermore, even if you can be confident that they are the same node (for instance, you only have one node with the "my-first-node" label in your executor pool), there is no guarantee that the assigned workspace folder will be the same for both stages either.  And even if you use the workspace step to manually assign a workspace directory, there is no guarantee that the assigned workspace directory will be the one you asked for either.
However, if you don't care that my-first-stage and my-third-stage run in the same workspace directory on the same node and you only care about the order that your steps run in, this works fine.


---

