Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-45138

How do I `lock` outside of a step in a declarative pipeline job?

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • None
    • Jenkins ver. 2.63
      Lockable Resource Plugin v2.0

      In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

      The documentation for pipelines seems to imply that `lock` must be used within a `step`, but could you confirm?

      I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

      step('foo') {
          lock('foo-resource') {
              bat '... acquire the resource...'
              bat '... use the resource...'
              bat '... cleanup the resource...'
          }
      }
      

      I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

      step('foo') {
          lock('foo-resource') {
              bat '... acquire the resource...'
              bat '... use the resource...'
          }
      }
      post {
          always {
              bat '... cleanup the resource...'
          }
      }
      

      I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.


      Well, I guess it's not valid around a `stage`.

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                 stage('foo') {
                 ^
      
      WorkflowScript: 30: Nothing to execute within stage "foo" @ line 30, column 9.
                 stage('foo') {
                 ^
      

          [JENKINS-45138] How do I `lock` outside of a step in a declarative pipeline job?

          Anthony Mastrean created issue -
          Anthony Mastrean made changes -
          Description Original: The [documentation](https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin) seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          ```groovy
          step('foo') \{
           lock('foo-resource') \{
           bat '... acquire the resource...'
           bat '... use the resource...'
           bat '... cleanup the resource...'
           }
          }
          ```

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          ```groovy
          step('foo') \{
           lock('foo-resource') \{
           bat '... acquire the resource...'
           bat '... use the resource...'
           }
          }
          post \{
           always \{
           bat '... cleanup the resource...'
           }
          }
          ```

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

          ![image](https://user-images.githubusercontent.com/217842/27551048-e7e4a2f6-5a6f-11e7-8a6f-86e7bb670193.png)
          New: The [documentation](https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin) seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           bat '... cleanup the resource...'
           }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           }
          }
          post {
           always {
           bat '... cleanup the resource...'
           }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

          ![image](https://user-images.githubusercontent.com/217842/27551048-e7e4a2f6-5a6f-11e7-8a6f-86e7bb670193.png)
          Anthony Mastrean made changes -
          Description Original: The [documentation](https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin) seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           bat '... cleanup the resource...'
           }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           }
          }
          post {
           always {
           bat '... cleanup the resource...'
           }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

          ![image](https://user-images.githubusercontent.com/217842/27551048-e7e4a2f6-5a6f-11e7-8a6f-86e7bb670193.png)
          New: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           bat '... cleanup the resource...'
           }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           }
          }
          post {
           always {
           bat '... cleanup the resource...'
           }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

          ![image](https://user-images.githubusercontent.com/217842/27551048-e7e4a2f6-5a6f-11e7-8a6f-86e7bb670193.png)
          Anthony Mastrean made changes -
          Attachment New: freestyle-configuration.png [ 38649 ]
          Anthony Mastrean made changes -
          Description Original: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           bat '... cleanup the resource...'
           }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           }
          }
          post {
           always {
           bat '... cleanup the resource...'
           }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

          ![image](https://user-images.githubusercontent.com/217842/27551048-e7e4a2f6-5a6f-11e7-8a6f-86e7bb670193.png)
          New: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           bat '... cleanup the resource...'
           }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           }
          }
          post {
           always {
           bat '... cleanup the resource...'
           }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!
          Anthony Mastrean made changes -
          Description Original: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           bat '... cleanup the resource...'
           }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
           lock('foo-resource') {
           bat '... acquire the resource...'
           bat '... use the resource...'
           }
          }
          post {
           always {
           bat '... cleanup the resource...'
           }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!
          New: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!
          Anthony Mastrean made changes -
          Summary Original: What are valid scopes for `lock` in a declarative pipeline job? New: How do I `lock` outside of a step in a declarative pipeline job?
          Anthony Mastrean made changes -
          Description Original: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!
          New: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!

          ----

          Well, I guess it's not valid around a `stage`.

          {code}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                     stage('foo') {
                     ^

          WorkflowScript: 30: Nothing to execute within stage "test-contract" @ line 30, column 9.
                     stage('foo') {
                     ^
          {code}
          Anthony Mastrean made changes -
          Description Original: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!

          ----

          Well, I guess it's not valid around a `stage`.

          {code}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                     stage('foo') {
                     ^

          WorkflowScript: 30: Nothing to execute within stage "test-contract" @ line 30, column 9.
                     stage('foo') {
                     ^
          {code}
          New: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!

          ----

          Well, I guess it's not valid around a `stage`.

          {code}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                     stage('foo') {
                     ^

          WorkflowScript: 30: Nothing to execute within stage "foo" @ line 30, column 9.
                     stage('foo') {
                     ^
          {code}
          Anthony Mastrean made changes -
          Description Original: The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!

          ----

          Well, I guess it's not valid around a `stage`.

          {code}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                     stage('foo') {
                     ^

          WorkflowScript: 30: Nothing to execute within stage "foo" @ line 30, column 9.
                     stage('foo') {
                     ^
          {code}
          New: In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!

          The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] for pipelines seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          Well, I guess it's not valid around a `stage`.

          {code}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                     stage('foo') {
                     ^

          WorkflowScript: 30: Nothing to execute within stage "foo" @ line 30, column 9.
                     stage('foo') {
                     ^
          {code}
          Anthony Mastrean made changes -
          Description Original: In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png|thumbnail!

          The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] for pipelines seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          Well, I guess it's not valid around a `stage`.

          {code}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                     stage('foo') {
                     ^

          WorkflowScript: 30: Nothing to execute within stage "foo" @ line 30, column 9.
                     stage('foo') {
                     ^
          {code}
          New: In the freestyle/UI-based job configuration, it looks like a lock is scoped to the entire build, not one step.

           !freestyle-configuration.png!

          The [documentation|https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin] for pipelines seems to imply that `lock` must be used within a `step`, but could you confirm?

          I have a situation where I need to lock a resource while performing a step. I need to always cleanup that resource, so that my step works correctly.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I need to move the cleanup step to a `post / always`, so the step reports the correct exit code, but now it's outside of the lock.

          {code}
          step('foo') {
              lock('foo-resource') {
                  bat '... acquire the resource...'
                  bat '... use the resource...'
              }
          }
          post {
              always {
                  bat '... cleanup the resource...'
              }
          }
          {code}

          I've got other jobs waiting for this resource and, based on the timing, they're trying to use the resource that this job is now cleaning up.

          ----

          Well, I guess it's not valid around a `stage`.

          {code}
          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 30: Unknown stage section "lock". Starting with version 0.5, steps in a stage must be in a steps block. @ line 30, column 9.
                     stage('foo') {
                     ^

          WorkflowScript: 30: Nothing to execute within stage "foo" @ line 30, column 9.
                     stage('foo') {
                     ^
          {code}

            Unassigned Unassigned
            anthonymastrean Anthony Mastrean
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: