如何使用可锁定资源插件中的Groovy表达式 [英] How to use the Groovy Expression from the Lockable Resources Plugin

查看:205
本文介绍了如何使用可锁定资源插件中的Groovy表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初尝试获取可锁定资源插件锁定特定资源,同时评估系统环境变量的值。评估系统环境变量是完美的,但是,我似乎无法锁定正确的资源。



这个问题主要针对锁定问题,在自由式项目中使用!



我已经创建一个名为 A_TEST B_TEST C_TEST 的三个资源。他们都没有任何标签。他们都可以从我的 Jenkins_URL / lockable-resources / 中看到,他们可以在没有任何问题的情况下被发布和发布。



在我的Jenkins Job配置中,我选择了此构建需要可锁定资源选项,该选项允许我指定资源标签 Groovy表达式(和其他类路径)。它还允许我指定预留资源变量名称要请求的资源数量



根据文档,Groovy脚本必须返回一个布尔值,所以我决定尝试一下:
$ b

测试1



我做的第一个测试是验证基本功能,通过设置以下内容:


  • 资源= B_TEST

  • Groovy Expression =未检查

  • 要请求的资源数量= 1



  • 导致作业使用B_TEST资源上的锁执行。
    $ b


    [lockable-resources]获得了[B_TEST]上的锁定



    测试2



    在这个测试中,我设置了以下内容:


    • 资源= B_TEST

    • Groovy表达式=已检查

    • Groovy脚本= 返回false

    • 要请求的资源数量= 1



    尝试执行该作业时,错误地等待文本:(等待资源[B_TEST])



    < h2>测试3

    在此测试中,我设置了以下内容:


    • 资源= B_TEST

    • Groovy Expression =已检查

    • Groovy Script = return true
    • >请求资源数量= 1


    这会导致错误资源 A_TEST <强>被锁定。控制台输出显示如下:

    lockquote
    可锁定资源在[A_TEST]上获取锁定



    测试4



    重新读取插件中每个选项的帮助后,我发现插件显然只允许指定 a 资源标签 Groovy表达式测试我设置了以下内容:


    • Groovy表达式=已检查

    • Groovy Script = return false

    • 预留资源变量名称= MyResourceName

      $ b

      这会导致作业错误地执行 而不锁定任何资源。控制台输出显示如下:

      lockquote
      可锁定资源获得了[]

      $上的锁定b
      $ b

      测试5



      所以在这个测试中我设置了以下内容:


      • Groovy Expression = Checked

      • Groovy Script = return true = MyResourceName


      这会导致所有资源上的作业错误锁定。控制台输出显示如下:

      lockquote
      [lockable-resources]获得[A_TEST,B_TEST,C_TEST]的锁定



      测试6



      根据插件的文档,在Jenkins-Pipelines中,这个插件可以用作这:

        echo'Starting'
      lock('my-resource-name'){
      echo '在这里做一些需要对资源进行独特访问的东西'
      //任何其他的构建都会等到锁定资源的块离开这个块
      }
      echo'Finish'

      所以我开始尝试使用包含锁的变体的 Groovy Script ('B_TEST')调用,但这大多会导致在作业尝试启动时出现更多混淆和错误,例如:

      <$ p $没有方法的签名:Script1.lock()适用于参数类型:(java.util.LinkedHashMap)values:[[resource:B_TEST]]
      P每个(groovy.lang.Closure),wait(),run(),run(),any(),find())

      但我想这一切都很有道理,因为 lock(){} 调用似乎只适用于释放锁在其捕获过程中。

      问题



      现在最大的问题是,所有这些实际上是如何工作的?
      我的猜测是某处有一个groovy命令来指定要保留的资源/标签,并且返回值(true / false)决定是否实际取锁。我试图潜入源,但没有任何运气。



      任何答案都非常感谢。

      例如,您可以编写:


      return resourceName.contains(RESOURCELOCK)


      并且您不需要设置任何其他字段(资源/标签)



      鉴于RESOURCELOCK是一个参数。然后,您可以简单地设置RESOURCELOCK的值并获取正确的锁定。例如,如果RESOURCELOCK设置为A_TEST,它将获取A_TEST。


      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.

      解决方案

      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.

      这篇关于如何使用可锁定资源插件中的Groovy表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

      查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆