jenkinsci / lockable-resources-plugin

Lock resources against concurrent use
https://plugins.jenkins.io/lockable-resources
MIT License
87 stars 182 forks source link

Declarative Pipeline: cannot lock via label due to mandatory 'resource' field. #278

Open MacMan314 opened 2 years ago

MacMan314 commented 2 years ago

Version report

Jenkins and plugins versions report:

Jenkins: 2.289.3.2
OS: Linux - 5.4.156-83.273.amzn2.x86_64
---
ace-editor:1.1
active-directory:2.24
analysis-model-api:10.2.5
ant:1.11
antisamy-markup-formatter:2.1
apache-httpcomponents-client-4-api:4.5.13-1.0
artifactory:3.13.2
async-http-client:1.7.24.3
authentication-tokens:1.4
aws-credentials:1.29
aws-java-sdk:1.11.995
blueocean-commons:1.24.7
bootstrap4-api:4.6.0-3
bouncycastle-api:2.20
branch-api:2.6.3
build-environment:1.7
build-name-setter:2.2.0
caffeine-api:2.9.1-23.v51c4e2c879c8
checks-api:1.7.0
cloudbees-administrative-monitors:1.0.1
cloudbees-analytics:1.30
cloudbees-assurance:2.276.0.6
cloudbees-blueocean-default-theme:0.8
cloudbees-folder:6.16
cloudbees-folders-plus:3.15
cloudbees-github-reporting:1.22
cloudbees-groovy-view:1.13
cloudbees-jenkins-advisor:3.3.1
cloudbees-jsync-archiver:5.18
cloudbees-license:9.57
cloudbees-monitoring:2.11
cloudbees-nodes-plus:1.22
cloudbees-platform-common:1.11
cloudbees-platform-data:1.21
cloudbees-plugin-usage:2.10
cloudbees-ssh-slaves:2.9
cloudbees-support:3.28
cloudbees-template:4.49
cloudbees-uc-data-api:4.44
cloudbees-unified-ui:1.10
cloudbees-view-creation-filter:1.6
cloudbees-workflow-template:3.12
cloudbees-workflow-ui:2.6
command-launcher:1.6
config-file-provider:3.8.1
credentials:2.5
credentials-binding:1.26
data-tables-api:1.10.23-3
display-url-api:2.3.5
durable-task:1.35
echarts-api:5.0.2-1
email-ext:2.83
envinject:2.4.0
envinject-api:1.8
file-operations:1.11
font-awesome-api:5.15.3-3
forensics-api:1.0.0
gerrit-code-review:0.4.7
git:4.7.2
git-client:3.7.2
git-server:1.9
github:1.33.1
github-api:1.123
github-branch-source:2.11.2
gradle:1.36
handlebars:3.0.8
handy-uri-templates-2-api:2.1.8-1.0
infradna-backup:3.38.35
ivy:2.1
jackson2-api:2.12.3
javadoc:1.6
jaxb:2.3.0.1
jdk-tool:1.5
jira:3.3
jira-steps:1.6.0
jjwt-api:0.11.2-5.143e44951c52
jobConfigHistory:2.28.1
jquery3-api:3.6.0-1
jsch:0.1.55.2
junit:1.50
kube-agent-management:1.1.50
kubernetes:1.30.0
kubernetes-client-api:5.4.1
kubernetes-credentials:0.9.0
ldap:2.7
lockable-resources:2.11
mailer:1.34
mapdb-api:1.0.9.0
matrix-project:1.19
maven-plugin:3.12
metrics:4.0.2.8
momentjs:1.1.1
nectar-license:8.31
nectar-rbac:5.58
node-iterator-api:1.5.0
oauth-credentials:0.4
okhttp-api:3.14.9
operations-center-agent:2.289.0.3
operations-center-client:2.289.0.10
operations-center-cloud:2.289.0.2
operations-center-context:2.289.0.6
pipeline-build-step:2.13
pipeline-graph-analysis:1.11
pipeline-input-step:2.12
pipeline-milestone-step:1.3.2
pipeline-model-api:1.8.5
pipeline-model-definition:1.8.5
pipeline-model-extensions:1.8.5
pipeline-rest-api:2.19
pipeline-stage-step:2.5
pipeline-stage-tags-metadata:1.8.5
pipeline-stage-view:2.19
plain-credentials:1.7
plasticscm-plugin:3.6
plugin-util-api:2.3.0
popper-api:1.16.1-2
scm-api:2.6.4
script-security:1.77
sloccount:1.25
snakeyaml-api:1.27.0
ssh-credentials:1.19
sshd:3.0.3
structs:1.23
support-core:2.74
tics:2020.1.0.39705
timestamper:1.13
token-macro:2.15
trilead-api:1.0.13
unique-id:2.2.0
variant:1.4
warnings-ng:9.0.1
workflow-aggregator:2.5
workflow-api:2.46
workflow-basic-steps:2.23
workflow-cps:2.92
workflow-cps-checkpoint:2.10
workflow-cps-global-lib:2.19
workflow-durable-task-step:2.39
workflow-job:2.41
workflow-multibranch:2.24
workflow-scm-step:2.13
workflow-step-api:2.23
workflow-support:3.8
Linux controller, problem exists without agent.

Reproduction steps

Results

Expected result:

One resource should become locked .

Actual result:

[Pipeline] End of Pipeline org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed: test_script.groovy: 27: Missing required parameter: "resource" @ line 27, column 15. lock(label: 'shared_label', variable: 'LABEL_LOCKED') { ^

MacMan314 commented 2 years ago

An update to this issue, a workaround is to put the lock() step inside a script {} block. This will work:

stage("Functional lock by label") {
  steps {
    script {
      lock(label: 'shared_label', variable: 'LABEL_LOCKED', quantity: 1) {
        echo "Lock acquired: ${LABEL_LOCKED}"
      }
    }
  }
}

This will not:

stage("Bugged lock") {
  steps {
    lock(label: 'shared_label', variable: 'LABEL_LOCKED', quantity: 1) {
      echo "Lock acquired: ${LABEL_LOCKED}"
    }
  }
}
jimklimov commented 2 years ago

Thanks, solutions (PRs, at least for docs - better for code) are also welcome ;)

One more known workaround is to specify a resource: null parameter to lock()

mark-reiche commented 1 year ago

I'm trying to fix that thing. I already managed to build a test case that reproduces the issue. Now, if I am able to also fix it, I'll submit my first PR :D Edit: https://github.com/mark-reiche/lockable-resources-plugin/tree/pr-issue-278-lock-via-label

mPokornyETM commented 1 year ago

@mark-reiche can you open the PR please. I am interested for your solution. We have currently documented the workaround proposed by @jimklimov .

When there are no solutions I will propose to close this issue, because a applicable workaround is documented.

Thx for contributing

mPokornyETM commented 1 year ago

See also #441