简体   繁体   English

如何在管道中处理 groovy 代码中的闭包作用域

[英]How to deal with closure scoping in groovy code in the pipeline

I've a method named "runTest", which is responsible for running certain tests in the pipeline, and I've another method named "chooseTest" which is responsible for parsing param and appending those tests in a map.我有一个名为“runTest”的方法,它负责在管道中运行某些测试,我有另一个名为“chooseTest”的方法,它负责解析参数并将这些测试附加到映射中。 In "chooseTest" method I also have switch-case which checks for which test needs to be appended in the map.在“chooseTest”方法中,我还有 switch-case 来检查哪些测试需要附加到地图中。 How would I use closure to call "runTest" method in that switch-case so that I can call it after I break out of the loop (for checking each test).我将如何使用闭包在该 switch-case 中调用“runTest”方法,以便我可以在退出循环后调用它(用于检查每个测试)。

I've tried making "runTest" itself a closure, but I ran into "Method too long error" which was a headache to debug, so I tried creating a closure variable in each of those if statement cases.我试过让“runTest”本身成为一个闭包,但我遇到了“方法太长错误”,这是一个令人头疼的调试问题,所以我尝试在每个 if 语句案例中创建一个闭包变量。 I ran into scoping issue which doesn't allow me to run those closure calls outside of the for-loop.我遇到了范围问题,这不允许我在 for 循环之外运行这些闭包调用。 I have attached example of my approaches below我在下面附上了我的方法的例子

// INSIDE of chooseTest method-->

switch (testName){
      case 'aTest':
           def runATest = {runTest('aTest')}
           break
      case 'bTest':
           def runBTest = {runTest('bTest')}
           break
}

runAtest.call()

//This is where I ran into scoping issue

//Another approach I tried (saw this approach elsewhere, not sure if this is even the correct approach syntactically)

def runTests = []
switch (testName){
      case 'aTest':
           runTests << {-> runTest('aTest')}
           break
      case 'bTest':
           runTests << {-> runTest('bTest')}
           break
}

runTests.each


I expect the closure to not have scoping errors, and when i call it outside of the case statement it invoke the chooseTest method.我希望闭包没有范围错误,当我在 case 语句之外调用它时,它会调用chooseTest 方法。

Well I am such a noobie guys, I could just define like such.好吧,我是个菜鸟,我可以这样定义。

def runAtest

switch(test){
    case 'atest':
        runAtest = {print('you a noob')}
        break
}

带有 GDSL 的 IntelliJ 中的 Jenkins 管道。 警告:“节点”不能应用于“(groovy.lang.closure<object> )'<div id="text_translate"><p> 我将 Jenkins 与 Pipelines 一起使用,并使用Jenkinsfile定义了一个<em>脚本化的管道</em>。 它看起来像这样:</p><pre> node { /* some stages */ }</pre><p> 我已将 Jenkins 附带的 GDSL 文件导入 IntelliJ。 现在我得到了语法突出显示,但整个文件在单个警告块中突出显示,IntelliJ 显示以下消息:</p><pre> 'node' cannot be applied to '(groovy.lang.closure&lt;Object&gt;)'</pre><p> 我认为可能是语法定义不支持节点 object 作为 root,但是如果我尝试以 root 身份编写pipeline ,则会出现相同的警告。</p></div></object> - Jenkins pipeline in IntelliJ with GDSL. Warning: 'node' cannot be applied to '(groovy.lang.closure<Object>)'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在try()的闭包内部对Groovy变量进行作用域设置以从catch()访问 - Scoping of a groovy variable inside a closure in try() to access from catch() 如何使用管道代码在从属节点上执行大量groovy命令 - How to execute bulk of groovy commands on slave node using pipeline code Groovy:如何从另一个闭包调用顶级范围内的闭包 - Groovy: how to call closure in top scope from another closure Jenkins管道作为类注释上的代码Groovy错误 - Jenkins Pipeline as Code Groovy Error on Class Annotation Jenkins Jira 步骤 - 声明式管道中的 Groovy 代码 - Jenkins Jira steps - Groovy code in Declarative Pipeline Jenkins共享库中的Groovy作用域 - Groovy scoping in Jenkins Shared Library 带有 GDSL 的 IntelliJ 中的 Jenkins 管道。 警告:“节点”不能应用于“(groovy.lang.closure<object> )'<div id="text_translate"><p> 我将 Jenkins 与 Pipelines 一起使用,并使用Jenkinsfile定义了一个<em>脚本化的管道</em>。 它看起来像这样:</p><pre> node { /* some stages */ }</pre><p> 我已将 Jenkins 附带的 GDSL 文件导入 IntelliJ。 现在我得到了语法突出显示,但整个文件在单个警告块中突出显示,IntelliJ 显示以下消息:</p><pre> 'node' cannot be applied to '(groovy.lang.closure&lt;Object&gt;)'</pre><p> 我认为可能是语法定义不支持节点 object 作为 root,但是如果我尝试以 root 身份编写pipeline ,则会出现相同的警告。</p></div></object> - Jenkins pipeline in IntelliJ with GDSL. Warning: 'node' cannot be applied to '(groovy.lang.closure<Object>)' Jenkins:如何在管道中运行Groovy脚本 - Jenkins : how to run groovy script in pipeline 如何为 Jenkins 管道有效地开发 groovy 脚本? - How to develop groovy script effectively for Jenkins pipeline? 如何在 Jenkins 声明性管道中执行 Groovy 语句? - How to execute Groovy statements in a Jenkins declarative pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM