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?

          abayer, this issue was marked as duplicate of 43336, but actually is not.

          The current issue says that the lock does not include the post section.

          In our case I have something like this:

          stage('tests') {
            options {
              lock('something')
            }
            
            parallel {
              stage('run1') {...}
              stage('run2') {...}
            }
            post {
              cleanup {...}
            }
          } 

          However, the port runs after releasing the lock (so, I get some errors when two build run in parallel).

          Vasileios Karavasilis added a comment - abayer , this issue was marked as duplicate of 43336, but actually is not. The current issue says that the lock does not include the post section. In our case I have something like this: stage( 'tests' ) { options { lock( 'something' ) } parallel { stage( 'run1' ) {...} stage( 'run2' ) {...} } post { cleanup {...} } } However, the port runs after releasing the lock (so, I get some errors when two build run in parallel).

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

              Created:
              Updated:
              Resolved: