[JIRA] (JENKINS-51865) Stage locks are created for skipped stages in declarative pipeline

11 views
Skip to first unread message

thxmasj@gmail.com (JIRA)

unread,
Jun 11, 2018, 10:20:02 AM6/11/18
to jenkinsc...@googlegroups.com
Thomas Johansen created an issue
 
Jenkins / Bug JENKINS-51865
Stage locks are created for skipped stages in declarative pipeline
Issue Type: Bug Bug
Assignee: Unassigned
Components: lockable-resources-plugin
Created: 2018-06-11 14:19
Priority: Critical Critical
Reporter: Thomas Johansen

Given the following declarative pipeline:

pipeline {
  stages {
    stage('Example stage') {
      when { expression { false } }
      options { lock resource: 'example resource' }
      steps { // ... }
    }
  }
}

Even though 'example stage' is skipped due to the when conditional, a lockable resource 'example resource' is created (if it doesn't already exist) and a lock is acquired on it. I think this is counter intuitive – one would not expect the lock to be acquired when the stage is skipped.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.3.0#73011-sha1:3c73d0e)
Atlassian logo

thxmasj@gmail.com (JIRA)

unread,
Jun 18, 2018, 8:04:01 AM6/18/18
to jenkinsc...@googlegroups.com
Thomas Johansen updated an issue
Change By: Thomas Johansen
Given the following declarative pipeline:
{noformat}

pipeline {
  stages {
    stage('Example stage') {
      when { expression { false } }
      options { lock resource: 'example resource' }
      steps { // ... }
    }
  }
}
{noformat}

Even though 'example stage' is skipped due to the when conditional, a lockable resource 'example resource' is created (if it doesn't already exist) and a lock is acquired on it. I think this is counter intuitive -- one would not expect the lock to be acquired when the stage . The consequence  is also really bad – a skipped stage might actually make the whole build hang (possibly for a long time) because it needs to acquire a lock on a busy resource (typically used by another build) .

thxmasj@gmail.com (JIRA)

unread,
Jun 18, 2018, 8:06:01 AM6/18/18
to jenkinsc...@googlegroups.com
Thomas Johansen commented on Bug JENKINS-51865
 
Re: Stage locks are created for skipped stages in declarative pipeline

Andrew Bayer: This issue is possibly tagged incorrectly. I suspect that component=pipeline-model-definition-plugin might be correct?

andrew.bayer@gmail.com (JIRA)

unread,
Jun 18, 2018, 9:46:09 AM6/18/18
to jenkinsc...@googlegroups.com
Andrew Bayer assigned an issue to Andrew Bayer
 

This is expected - the lock is acquired before we know to skip the stage. It still gets released at the end of the stage, yes?

Change By: Andrew Bayer
Component/s: pipeline-model-definition-plugin
Component/s: lockable-resources-plugin
Assignee: Andrew Bayer

thxmasj@gmail.com (JIRA)

unread,
Jun 18, 2018, 11:57:01 AM6/18/18
to jenkinsc...@googlegroups.com
 
Re: Stage locks are created for skipped stages in declarative pipeline

Ok, got it. So this is a feature request. I was really happy when the 'beforeAgent' option was introduced to the when block. Guess I'm looking for a 'beforeLock' option too, then.

The lock is released as it should. The problem (or rather my problem) is that the lock is required at all. My current workaround is to rename the lock resource when the stage should be skipped, but it makes the code a little messy.

andrew.bayer@gmail.com (JIRA)

unread,
Nov 16, 2018, 1:24:02 PM11/16/18
to jenkinsc...@googlegroups.com

So the only way we could address this directly would be to add a beforeOptions flag to when - but I'm not sure that's actually worth doing. You can always put lock or timeout (and any other block-scoped options) in your steps instead. The more complexity we add to when flags (since we're already adding beforeInput in JENKINS-50880), the hairier the code gets, and this case is one that can largely be worked around (unlike with agent and input). The workaround would create scenarios where, say, timeout or lock wouldn't be in place for acquiring the agent or waiting for input, but it would still work fine for everything in steps. Does that sound reasonable to you?

This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

stephen.alan.connolly@gmail.com (JIRA)

unread,
Jan 30, 2019, 4:47:01 AM1/30/19
to jenkinsc...@googlegroups.com

Andrew Bayer perhaps we could work around this by using an expression in the quantity of the lock, e.g.

pipeline {
  stages {
    stage('Example stage') {
      when { expression { false
 } }
      options { lock resource: 'example resource', quantity: expr ? 1 : 0 }
      steps { // ... }
    }
  }
}

What I do not know is if declarative supports numerical expressions

stephen.alan.connolly@gmail.com (JIRA)

unread,
Jan 30, 2019, 5:45:02 AM1/30/19
to jenkinsc...@googlegroups.com

so `quantity:0` doens't work as that is the default value and seems to indicate "grab all".

I'm now trying:

pipeline {
  stages {
    stage('Example stage'
) {
      when { branch 'master' }
      options { lock resource: "${BRANCH_NAME=='master' ? 'example resource':'dummy'}" }
      steps { // ... }
    }
  }
}

We'll see if that is an acceptable workaround for my use case

stephen.alan.connolly@gmail.com (JIRA)

unread,
Jan 30, 2019, 5:57:01 AM1/30/19
to jenkinsc...@googlegroups.com
Stephen Connolly updated an issue
 
Change By: Stephen Connolly
Attachment: Screenshot 2019-01-30 at 10.56.27.png

stephen.alan.connolly@gmail.com (JIRA)

unread,
Jan 30, 2019, 5:58:02 AM1/30/19
to jenkinsc...@googlegroups.com
 
Re: Stage locks are created for skipped stages in declarative pipeline

Ok that is an acceptable workaround for my use case

f.modler@gmx.net (JIRA)

unread,
Mar 6, 2019, 11:53:02 AM3/6/19
to jenkinsc...@googlegroups.com

Stephen Connolly thanks for sharing your workaround.

Unfortunately this won't work in case the criteria to check of is calculcated in a previous stage, e.g.:

pipeline {
  stages {
    stage('Calculate criteria') {
      steps {
        script {
          someCriteria = true
        }
      }
    }
    stage('Example stage') {
      when {
        expression {
          return someCriteria
        }
      }
      options { lock resource: "${someCriteria ? 'example resource':'dummy'}" }
      steps { // ... }
    }
  }
}

This will fail in options with:

groovy.lang.MissingPropertyException: No such property: someCriteria for class: groovy.lang.Binding

Daniel.Pasto@gmail.com (JIRA)

unread,
Apr 6, 2019, 4:09:04 AM4/6/19
to jenkinsc...@googlegroups.com
D Pasto commented on Bug JENKINS-51865

It also doesn’t work because either you hardcore “dummy” and potentially block on it or you randomize and create all sorts of crap in your Jenkins config

f.modler@gmx.net (JIRA)

unread,
Apr 10, 2019, 12:30:02 PM4/10/19
to jenkinsc...@googlegroups.com

It also doesn’t work because either you hardcore “dummy” and potentially block on it

Those potential blocks/locks are very short-lived. You could also define a pool of multiple "dummy" resources to further reduce the (already very small) impact.

So this partial workaround is better than nothing.

Andrew Bayer

You can always put lock or timeout (and any other block-scoped options) in your steps instead.

Unfortunately, this is no solution/workaround for post blocks. E.g. you lock some external resource/server and in `post` you want to collect the server's logfiles (regardless of the build status).
So IMHO, beforeOptions is still needed.

f.modler@gmx.net (JIRA)

unread,
Apr 10, 2019, 12:31:02 PM4/10/19
to jenkinsc...@googlegroups.com
Falko Modler edited a comment on Bug JENKINS-51865
[~wgc123]
{quote}

It also doesn’t work because either you hardcore “dummy” and potentially block on it
{quote}

Those potential blocks/locks are very short-lived. You could also define a pool of multiple "dummy" resources to further reduce the (already very small) impact.

So this partial workaround is better than nothing.

[~abayer]
{quote}

You can always put lock or timeout (and any other block-scoped options) in your steps instead.
{quote}

Unfortunately, this is no solution/workaround for {{post}} blocks. E.g. you lock some external resource/server and in
` {{ post ` }} you want to collect the server's logfiles (regardless of the build status).

So IMHO, {{beforeOptions}} is still needed.

f.modler@gmx.net (JIRA)

unread,
Oct 15, 2019, 6:41:02 PM10/15/19
to jenkinsc...@googlegroups.com

f.modler@gmx.net (JIRA)

unread,
Nov 13, 2019, 6:12:02 AM11/13/19
to jenkinsc...@googlegroups.com

f.modler@gmx.net (JIRA)

unread,
Nov 13, 2019, 6:13:06 AM11/13/19
to jenkinsc...@googlegroups.com

f.modler@gmx.net (JIRA)

unread,
Nov 13, 2019, 6:13:06 AM11/13/19
to jenkinsc...@googlegroups.com

f.modler@gmx.net (JIRA)

unread,
Nov 13, 2019, 6:14:04 AM11/13/19
to jenkinsc...@googlegroups.com
Falko Modler edited a comment on Bug JENKINS-51865
 
Re: Stage locks are created for skipped stages in declarative pipeline
[~bitwiseman] I have just resolved this ticket because the fix is included in cluded in 1.4.0. I am not familiar with the overall ticket workflow, though.
So I leave it up to you (or [~abayer]) to close this issue.

f.modler@gmx.net (JIRA)

unread,
Nov 13, 2019, 6:14:04 AM11/13/19
to jenkinsc...@googlegroups.com

Liam Newman I have just resolved this ticket because the fix is in cluded in 1.4.0. I am not familiar with the overall ticket workflow, though.
So I leave it up to you (or Andrew Bayer) to close this issue.

Reply all
Reply to author
Forward
0 new messages