简体   繁体   English

使用从另一个 Groovy 文件加载的枚举(Jenkins 管道问题)

[英]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 暴露给调用脚本

  • Adding @Field (not legal Groovy)添加@Field(不合法的 Groovy)
  • Changing the enum definition to public def PermissionType (not legal Groovy)将枚举定义更改为 public def PermissionType(不合法的 Groovy)
  • Removing and adding public to the enum definition删除和添加 public 到枚举定义
  • Changing the case (though I believe enums need to start with an upper case character?)更改大小写(尽管我认为枚举需要以大写字符开头?)

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)我只有这个问题(我现在可以忍受)

  • You can only load the file ones in a script otherwise you get a duplicate class exception您只能在脚本中加载文件,否则会出现重复的类异常
  • You can't use the type in an external method, only the values - OK for me since any methods taking in the type are local to the class definition您不能在外部方法中使用该类型,只能使用值 - 对我来说可以,因为任何采用该类型的方法都是类定义的本地方法

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.

相关问题 在Jenkins管道脚本中将另一个groovy文件中的类用作类型 - Using class from another groovy file as a type in a Jenkins pipeline script 如何使用 Groovy 访问 Jenkins 管道中 json 文件中的内部字段 - How to access the inner fields in a json file in Jenkins pipeline using Groovy 如何在 jenkins 管道 groovy 文件中使用 withcredentials 设置多个凭据 - How to Set multiple credentials using withcredentials in jenkins pipeline groovy file 使用Jenkins管道中的Groovy创建包含一些内容的文件 - Create a file with some content using Groovy in Jenkins pipeline 从 groovy 管道文件在 linux Jenkins 服务器上执行 powershell 脚本? - Execute powershell script on a linux Jenkins server from groovy pipeline file? Jenkins-pipeline从groovy中的属性文件中提取和设置变量 - Jenkins-pipeline Extract and Set variables from properties file in groovy Jenkins groovy 管道 - 需要执行 jar 文件的命令标准输出 - Jenkins groovy pipeline - Need stdout of command from executing jar file 如何在 Jenkins 文件(管道)中使用共享库中的 groovy 常量? - How to use groovy constant from Shared Libray in Jenkins file (pipeline)? 如何使用Groovy和Kubernetes插件从Jenkins Pipeline运行Kafka? - How to run Kafka from Jenkins Pipeline using Groovy and Kubernetes plugin? 使用 Jenkins 共享管道中的另一个 class - Using another class from Jenkins Shared Pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM