简体   繁体   English

在Jenkins Pipeline中,从vars文件夹中的共享库返回一个值(映射或列表)

[英]In Jenkins Pipeline return a value ( map or list ) from shared library in vars folder

I have a Jenkins Pipeline which invokes a groovy function inside vars folder. 我有一个Jenkins Pipeline,它在vars文件夹内调用一个groovy函数。

a) Is it a right approach to return a value , say a list or map from the script and access it in the pipeline a)从脚本返回值(例如列表或映射)并在管道中访问它是否正确?

b) Even if its not the right approach is there a way to achieve this functionality? b)即使不是正确的方法,是否有办法实现此功能?

For utility functions I would use the src folder, however I don't see a reason why it shouldn't work with the vars folder. 对于实用程序功能,我将使用src文件夹,但是我看不出为什么它不适用于vars文件夹的原因。

From within Jenkinsfile call: def result = yourClass{ yourArg } and add a return value to the definition if vars like: def call(body) { return true } 在Jenkinsfile内调用: def result = yourClass{ yourArg }并在定义中添加一个返回值,如var一样: def call(body) { return true }

Yes, there is nothing wrong with that approach. 是的,这种方法没有错。 Many method calls in shared libraries return values to be manipulated in the pipeline. 共享库中的许多方法调用都返回要在管道中操纵的值。

You return the value just like you would for any other method call. 您将返回该值,就像对任何其他方法调用一样。 In a declarative pipeline, you can't assign that return value to anything unless you are in a script{} tag. 在声明性管道中,除非您位于script {}标记中,否则您无法将该返回值分配给任何内容。 But you could always use a GString to print it. 但是您始终可以使用GString进行打印。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM