[英]Using an enum loaded from another Groovy file (Jenkins Pipeline issue)
I have the following Groovy script as part of a Jenkins pipeline我有以下 Groovy 脚本作为 Jenkins 管道的一部分
permissions.groovy权限.groovy
enum PermissionType {
ANONYMOUS,
AUTHENTICATED
}
def get_job_permissions(PermissionType permission) {
...
}
return this
I load this file into another Groovy file as part of my Jenkins pipeline and call get_job_permissions passing through one of the enums as a parameter.我将此文件作为 Jenkins 管道的一部分加载到另一个 Groovy 文件中,并调用 get_job_permissions 作为参数传递其中一个枚举。
pipeline.groovy管道.groovy
def job_permissions = load 'permissions.groovy'
job_permissions.get_job_permissions(job_permissions.PermissionType.AUTHENTICATED)
Jenkins fails on this with the following error (I've verified that in this case 'Script3' is the call to get_job_permissions with the enum parameter). Jenkins 失败并出现以下错误(我已经验证,在这种情况下,'Script3' 是使用 enum 参数调用 get_job_permissions)。
groovy.lang.MissingPropertyException: No such property: PermissionType for class: Script3
I know the script load and call is correct, as I can change the signature of get_job_permissions to the following, pass through a random string in pipeline.groovy and the call goes through correctly.我知道脚本加载和调用是正确的,因为我可以将 get_job_permissions 的签名更改为以下内容,在 pipeline.groovy 中传递一个随机字符串并且调用正确通过。
def get_job_permissions(def permission) {
...
}
If I don't change the signature, and still pass through a random string, Jenkins fails the build as it can't find the method it thinks I'm calling (which is true, it's not there, it's expecting a PermissionType type).如果我不更改签名,并且仍然传递一个随机字符串,Jenkins 将导致构建失败,因为它找不到它认为我正在调用的方法(这是真的,它不存在,它期待 PermissionType 类型) .
I've tried a number of different things to expose PermissionType to the calling script我尝试了许多不同的方法来将 PermissionType 暴露给调用脚本
None of these solutions allow me to reference the enum type from the calling script, and I understand this is because I'm trying to access a type by referencing it through an instance of the script.这些解决方案都不允许我从调用脚本中引用枚举类型,我理解这是因为我试图通过脚本实例引用它来访问类型。
But if I can't do it this way, what is the best way to do it?但如果我不能这样做,最好的方法是什么?
Thanks谢谢
I managed to get something to work - I certainly know it's probably not the right, or even good, way to do it, but it unblocked me and gave me what I needed.我设法让一些东西工作 - 我当然知道这可能不是正确的,甚至不是很好的方法,但它让我畅通无阻,并给了我所需的东西。
Rather than define the enum in a script as you normally would而不是像通常那样在脚本中定义枚举
enum PermissionType {
ANONYMOUS,
AUTHENTICATED
}
I created a class containing the enum with member variables initialised to the values within the enum.我创建了一个包含枚举的类,成员变量初始化为枚举中的值。
permissions.groovy权限.groovy
public class PermissionTypes {
public enum Values {
ANONYMOUS,
AUTHENTICATED
}
public final PermissionTypes.Values ANONYMOUS = PermissionTypes.Values.ANONYMOUS
public final PermissionTypes.Values AUTHENTICATED = PermissionTypes.Values.AUTHENTICATED
}
@Field final PermissionTypes Permissions = new PermissionTypes()
I can then expose an instance of that class in the script, load it as normal and I finally get access to the enum values.然后我可以在脚本中公开该类的实例,正常加载它,最后我可以访问枚举值。
pipeline.groovy管道.groovy
def job_permissions = load 'permissions.groovy'
job_permissions.get_job_permissions(job_permissions.Permissions.AUTHENTICATED)
I think we can all agree this is slightly bonkers, but it gave me what I needed.我想我们都同意这有点疯狂,但它给了我我需要的东西。
Only issues I have with this (which I can live with for now)我只有这个问题(我现在可以忍受)
Would still love to know the right way to do this :)仍然很想知道这样做的正确方法:)
I ran into this problem recently and found a different solution that looks less hacky.我最近遇到了这个问题,并找到了一个看起来不那么笨拙的不同解决方案。
enum PermissionType {
ANONYMOUS,
AUTHENTICATED
}
def get_job_permissions(PermissionType permission) {
...
}
// Do this before you return out to make the enum available as well
this.PermissionType = PermissionType
return this
I prefer to use:我更喜欢使用:
MyEnumClass.groovy: MyEnumClass.groovy:
package cl.mypackage.utils
class MyEnumClass {
static enum MyEnum {
FOO, BAR, QWERTY
}
}
How to use:如何使用:
import cl.mypackage.utils.MyEnumClass
def in_some_place() {
def fooEnum = MyEnumClass.MyEnum.FOO
}
Regards问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.