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

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

XMLWordPrintable

    • 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') {
                 ^
      

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

              Created:
              Updated:
              Resolved: