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

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

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      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.

        Attachments

          Issue Links

            Activity

            wgc123 D Pasto created issue -
            wgc123 D Pasto made changes -
            Field Original Value New Value
            Summary Declarative pipeline, lock() in stage aoptions is executed before when clause Declarative pipeline, lock() in stage options is executed before when clause
            wgc123 D Pasto made changes -
            Description 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.
            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.
            amrichko Andy Mrichko made changes -
            Priority Minor [ 4 ] Major [ 3 ]
            wgc123 D Pasto made changes -
            Link This issue duplicates JENKINS-51865 [ JENKINS-51865 ]
            tgr Tobias Gruetzmacher made changes -
            Resolution Duplicate [ 3 ]
            Status Open [ 1 ] Closed [ 6 ]

              People

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

                Dates

                Created:
                Updated:
                Resolved: