简体   繁体   English

Jenkins 共享库全局/单例类

[英]Jenkins Shared Library Global/Singleton Class

I am looking for some guidance on the design of a Jenkins Shared Library, with respect to using a class, initializing it, and then being able to use that instance in any of my vars files.我正在寻找有关 Jenkins 共享库设计的一些指导,关于使用类、初始化它,然后能够在我的任何 vars 文件中使用该实例。

Structure结构

src
  - foo
   - class
     - Configuration.groovy
vars
  - cicd.groovy
  - doMore.groovy

Class班级

The following is a class I would like to initialize once, but then use anywhere, without having to pass it into each vars function, or reinitialize every time.下面是一个我想初始化一次,然后在任何地方使用的类,而不必将它传递给每个 vars 函数,或者每次都重新初始化。

package foo.class

public class Configuration {

    public String Foo

    public String Bar    

}

Vars瓦尔斯

In my cicd.groovy vars file, I have something like this:在我的cicd.groovy vars 文件中,我有这样的内容:

#!groovy
import foo.class.Configuration

def call () {

    return initCicd()
}

def initCicd() {
    configuration = new Configuration()
    configuration.Foo = 'FOO'
    return configuration
}

But, in other vars files like doMore.groovy , I would like to use the same configuration instance.但是,在doMore.groovy等其他 vars 文件中,我想使用相同的配置实例。

#!groovy
import foo.class.Configuration

def call () {

    println configuration.Foo
}

Is there a Singleton pattern that that works in Jenkins Shared Library, or a way to reference an instance across vars files or steps?是否有一种适用于 Jenkins 共享库的单例模式,或者一种跨 vars 文件或步骤引用实例的方法? If possible, please share an example.如果可能,请分享一个例子。

Thanks!谢谢!

You can simply use Groovy's @Singleton annotation for your Configuration class and use Configuration.instance wherever you want to access configuration settings.您可以简单地将 Groovy 的@Singleton注释用于您的Configuration类,并在您想要访问配置设置的任何地方使用Configuration.instance Consider following example:考虑以下示例:

.
├── src
│   └── foo
│       └── Configuration.groovy
└── vars
    ├── cicd.groovy
    └── doMore.groovy

src/foo/Configuration.groovy src/foo/Configuration.groovy

package foo

@Singleton
class Configuration {
    public String foo = 'foo_123'
    public String bar = 'bar_456'
}

vars/cicd.groovy vars/cicd.groovy

#!groovy

import foo.Configuration

def call() {
    return initCicd()
}

def initCicd() {
    println Configuration.instance.foo
    return Configuration.instance
}

vars/doMore.groovy变量/doMore.groovy

#!groovy

import foo.Configuration

def call() {
    println Configuration.instance.bar
}

In the pipeline script I simply call:在管道脚本中,我简单地调用:

cicd()
doMore()

And I get something like that in the console log:我在控制台日志中得到类似的信息:

Loading library default_jenkins_libs@master
Attempting to resolve master from remote references...
 > git --version # timeout=10
 > git ls-remote -h -t file:///var/jenkins_home/libraries # timeout=10
Found match: refs/heads/master revision 4fa988ccde542d77d19febd72f532ef996971a5d
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url file:///var/jenkins_home/libraries # timeout=10
Fetching without tags
Fetching upstream changes from file:///var/jenkins_home/libraries
 > git --version # timeout=10
 > git fetch --no-tags --progress file:///var/jenkins_home/libraries +refs/heads/*:refs/remotes/origin/*
Checking out Revision 4fa988ccde542d77d19febd72f532ef996971a5d (master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 4fa988ccde542d77d19febd72f532ef996971a5d
Commit message: "update"
 > git rev-list --no-walk 39890b4ca39bf32ebde8c7ad143b110bf16cf6b3 # timeout=10
[Pipeline] echo
foo_123
[Pipeline] echo
bar_456
[Pipeline] End of Pipeline
Finished: SUCCESS

The one downside of using singletons is that they can get modified anywhere and this change is populated to all callers.使用单例的一个缺点是它们可以在任何地方进行修改,并且此更改会填充到所有调用者。

I obtain a nullpointer exception using this solution at the moment of 我在使用此解决方案时获得了nullpointer异常

"Configuration.instance" 

Which jenkins version or Groovy are you using? 您正在使用哪个詹金斯版本或Groovy? I think that @Singleton annotation is not working. 我认为@Singleton注释不起作用。

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

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