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

Declarative pipeline, lock() in stage options is executed before when clause

      In a a declarative pipeline i have a stage grouping several other stages and need to hold a lock for the duration of the group.  I see I can add lock() to the options of the group and it works fine in that scenario.  However the group also has a When clause so I can turn that group off with a job parameter.  

      The problem is that when the "when" evaluates false so the stage will be skipped, it wait for the lock anyway.  If the stage will be skipped for any reason, we should not block on the lock.  The "when" defines a "beforeAgent" attribute for a similar need, so that might be an approach, but I'm not seeing a scenario where you would want to do it in the current order.

       

      My pipeline looks like:

      pipeline {

      agent none
      stages {

            stage ('Build')

      {      }

            stage('Deploy-and-Test') { // Group Stage Deploy and test the Build - lock the deployment VM so we don't step on other runs
                when {
                     expression {
                          "${params.Deploy}" == "true"
                     }
                    beforeAgent true
                }
                options {
                     lock(quantity: 1, resource: "${params.DeployAgent}", variable: 'myDeployAgent')
                }
                agent none

                stages { ...}

           }}}

       

      When I run it withthe param to disable the stage I see:


      [Pipeline] stage[Pipeline] { (Deploy-and-Test)[Pipeline] lockTrying to acquire lock on [DeploymentVM_Win]
      Lock acquired on [DeploymentVM_Win][Pipeline] {Stage "Deploy-and-Test" skipped due to when conditional

      Since the stage is being skipped there is no point in holding the lock, but it the lock is already allocated, this will block anyway.  The lock() should be processed after the decision whether to execute the stage.

          [JENKINS-55485] Declarative pipeline, lock() in stage options is executed before when clause

          D Pasto created issue -
          D Pasto made changes -
          Summary Original: Declarative pipeline, lock() in stage aoptions is executed before when clause New: Declarative pipeline, lock() in stage options is executed before when clause
          D Pasto made changes -
          Description Original: In a a declarative pipeline i have a stage grouping several other stages and need to hold a lock for the duration of the group.  I see I can add lock() to the options of the group and it works fine in that scenario.  However the group also has a When clause so I can turn that group off with a job parameter.  

          The problem is that when the "when" evaluates false so the stage will be skipped, it wait for the lock anyway.  If the stage will be skipped for any reason, we should not block on the lock.

           

          My pipeline looks like:

          ---

          pipeline {

          agent none
           stages {

                stage ('Build') {

               }

                stage('Deploy-and-Test') { // Group Stage Deploy and test the Build - lock the deployment VM so we don't step on other runs
                    when {
                         expression {
                              "${params.Deploy}" == "true"
                         }
                        beforeAgent true
                    }
                    options {
                         lock(quantity: 1, resource: "${params.DeployAgent}", variable: 'myDeployAgent')
                    }
                    agent none

                    stages \{ ...}

               }}}

           

          When I run it withthe param to disable the stage I see:

          ---
          [Pipeline] stage[Pipeline] { (Deploy-and-Test)[Pipeline] lockTrying to acquire lock on [DeploymentVM_Win]
          Lock acquired on [DeploymentVM_Win][Pipeline] {Stage "Deploy-and-Test" skipped due to when conditional
          ---

          Since the stage is being skipped there is no point in holding the lock, but it the lock is already allocated, this will block anyway.  The lock() should be processed _after_ the decision whether to execute the stage.
          New: In a a declarative pipeline i have a stage grouping several other stages and need to hold a lock for the duration of the group.  I see I can add lock() to the options of the group and it works fine in that scenario.  However the group also has a When clause so I can turn that group off with a job parameter.  

          The problem is that when the "when" evaluates false so the stage will be skipped, it wait for the lock anyway.  If the stage will be skipped for any reason, we should not block on the lock.  The "when" defines a "beforeAgent" attribute for a similar need, so that might be an approach, but I'm not seeing a scenario where you would want to do it in the current order.

           

          My pipeline looks like:



          pipeline {

          agent none
           stages {

                stage ('Build')

          {      }

                stage('Deploy-and-Test') { // Group Stage Deploy and test the Build - lock the deployment VM so we don't step on other runs
                     when {
                          expression {
                               "${params.Deploy}" == "true"
                          }
                         beforeAgent true
                     }
                     options {
                          lock(quantity: 1, resource: "${params.DeployAgent}", variable: 'myDeployAgent')
                     }
                     agent none

                    stages \{ ...}

               }}}

           

          When I run it withthe param to disable the stage I see:


           [Pipeline] stage[Pipeline] { (Deploy-and-Test)[Pipeline] lockTrying to acquire lock on [DeploymentVM_Win]
           Lock acquired on [DeploymentVM_Win][Pipeline] {Stage "Deploy-and-Test" skipped due to when conditional
           —

          Since the stage is being skipped there is no point in holding the lock, but it the lock is already allocated, this will block anyway.  The lock() should be processed _after_ the decision whether to execute the stage.
          Andy Mrichko made changes -
          Priority Original: Minor [ 4 ] New: Major [ 3 ]
          D Pasto made changes -
          Link New: This issue duplicates JENKINS-51865 [ JENKINS-51865 ]
          Tobias Gruetzmacher made changes -
          Resolution New: Duplicate [ 3 ]
          Status Original: Open [ 1 ] New: Closed [ 6 ]

            Unassigned Unassigned
            wgc123 D Pasto
            Votes:
            3 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: