How to use the Groovy Expression from the Lockable Resources Plugin in Jenkins Freestyle projects

a 夏天 提交于 2019-12-13 15:22:16

问题


This question was asked in relation to an old version of Lockable Resources Plugin. The error in the old versions have been fixed since version 2.1.

The answers for how to properly use the Groovy Expression in the freestyle jobs are however not described. I will add an answer to explain this.

The original question follows, for those with an interest:

I'm was initially attempting to get the Lockable Resources Plugin to lock on a specific resource while also evaluation the value of a system environment variable. Evaluation the system environment variable is working perfectly, however, I can't seem to get a lock on the correct resource.

This question focused on the locking problem, when used in a free-style project!

I have created a three resources called A_TEST, B_TEST and C_TEST. None of them have any Labels. They are all visible from my Jenkins_URL/lockable-resources/ where they can be taken and released without problems.

In my Jenkins Job configuration, I have selected the This build requires lockable resources option, which allows me to specify a Resource, Label or Groovy Expression (and Additional classpath). It additionally allows me to specify Reserved resources variable name and Number of resources to request.

According to the documentation, the Groovy script must return a boolean value, so I determined to try it out:

Test 1

The first test I did was to verify the basic functionality, by setting the following:

  • Resource = B_TEST
  • Groovy Expression = Not checked
  • Number of resources to request = 1

This results in the job executing with a lock on the B_TEST resource. The console output reads:

[lockable-resources] acquired lock on [B_TEST]

Test 2

In this test I set the following:

  • Resource = B_TEST
  • Groovy Expression = Checked
  • Groovy Script = return false
  • Number of resources to request = 1

When attempting to execute the job, this wrongly waits forever with the text: (pending--Waiting for the resourced [B_TEST])

Test 3

In this test I set the following:

  • Resource = B_TEST
  • Groovy Expression = Checked
  • Groovy Script = return true
  • Number of resources to request = 1

This results in the wrong resource A_TEST to be locked. The console output reads:

[lockable-resources] acquired lock on [A_TEST]

Test 4

After rereading the help for each option in the plugin, I discovered that the plugin apparently only allows me to specify either a Resource, Label or Groovy Expression So in this test I set the following:

  • Groovy Expression = Checked
  • Groovy Script = return false
  • Reserved resources variable name = MyResourceName

This results in the job executing wrongly without a lock on any resource. The console output reads:

[lockable-resources] acquired lock on []

Test 5

So in this test I set the following:

  • Groovy Expression = Checked
  • Groovy Script = return true
  • Reserved resources variable name = MyResourceName

This results in the job wrongly locking on all resource. The console output reads:

[lockable-resources] acquired lock on [A_TEST, B_TEST, C_TEST]

Test 6

According to the documentation of the plugin, in Jenkins-Pipelines this plugin can be used as this:

echo 'Starting'
lock('my-resource-name') {
  echo 'Do something here that requires unique access to the resource'
  // any other build will wait until the one locking the resource leaves this block
}
echo 'Finish'

so I started experimenting with the Groovy Script containing variations of the lock('B_TEST') call, but this mostly lead to more confusion and errors while the job attempted to start, such as this:

No signature of method: Script1.lock() is applicable for argument types: (java.util.LinkedHashMap) values: [[resource:B_TEST]] 
Possible solutions: each(groovy.lang.Closure), wait(), run(), run(), any(), find())

But I guess this all makes good sense, as the lock(){ } call seems suited to take and release the lock only during its capture.

The Question

The big question now is, how does all of this actually work? My guess is that somewhere there's a groovy command to specify the Resources/Labels you want to reserve, and the return value (true/false) determines to actually take the lock or not. I have attempted to dive into the source, but didn't have any luck.

Any answer is greatly appreciated.


回答1:


Case 1: Using resource name itself

You need to just list the resources required in 'Resources'.

Case 2: Resource name from build parameters

Groovy Expression: Checked

Groovy Script: 'resourceName==buildParam1 || resourceName==buildParam2'




回答2:


First a word of warning. Debugging Groovy in this plugin is hard, and can quickly fill up your Jenkins log. The only posibility is to use println from within the script, and all output ends up in the jenkins log, as it's evaluated before the jobs console log is initialized.

The script is continuously called by the job scheduler, each time with a new resource name. The task of the script is to evaluate the resource name, and return either true or false. A true will cause the job to execute - provided all other pre-run conditions are met.

To test for a particular resource, here either Printer1 or Printer2:

if (resourceName.startsWith("Printer")) {
//   println "Gotit"
   return true
}
return false

To test for string parameters passed to the job, simply specify the string parameter name:

if (resourceName.equals(SELECTED_PRINTER)) {
   return true
}
return false



回答3:


You can simply use the Groovy script: For example you can write:

return resourceName.contains(RESOURCELOCK)

and you don't need to set any other field (Resources/Label)

Given that RESOURCELOCK is a parameter. Then you can simply set the value of RESOURCELOCK and acquire the correct lock. For example if RESOURCELOCK is set to A_TEST, it acquire A_TEST.



来源:https://stackoverflow.com/questions/43808722/how-to-use-the-groovy-expression-from-the-lockable-resources-plugin-in-jenkins-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!