【发布时间】:2020-11-06 17:37:15
【问题描述】:
我有一个用于 Jenkinsfile 的 Jenkins 共享库。我的库有一个完整的管道,它有一个在我的管道中使用的函数(我们称之为examFun())。
我的 Jenkins 文件:
@Library('some-shared-lib') _
jenkinsPipeline{
exampleArg = "yes"
}
我的共享库文件(名为 jenkinsPipeline.groovy):
def examFun(){
return [
new randClass(name: 'Creative name no 1', place: 'London')
new randClass(name: 'Creating name no 2', place: 'Berlin')
]
}
def call(body) {
def params= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = params
body()
pipeline {
// My entire pipeline is here
// Demo stage
stage("Something"){
steps{
script{
projectName = params.exampleArg
}
}
}
}
}
class randClass {
String name
String place
}
它工作正常,但examFun() 的值是硬编码的,将来可能需要更改。因此,我希望能够从我的 Jenkinsfile 中更改它们(就像我可以更改 exampleArg 一样)。按照this的文章,我尝试了以下:
我将examFun() 更改为
def examFun(String[] val){
return [
new randClass(name: val[0], place: 'London')
new randClass(name: val[1], place: 'Berlin')
]
}
并从我的 Jenkinsfile 中调用它,如下所示
@Library('some-shared-lib') _
jenkinsPipeline.examFun(['Name 1','Name 2'])
jenkinsPipeline{
exampleArg = "yes"
}
但这不起作用(可能是因为我的库是通过 def call(body){} 构建的)。之后,我尝试将 jenkinsPipeline.groovy 拆分为 2 个文件:第一个文件包含我的管道部分,第二个文件包含 examFun() 和 randClass。我修改了我的 Jenkinsfile 如下:
@Library('some-shared-lib') _
def config = new projectConfig() // New groovy filed is called 'projectConfig.groovy'
config.examFun(["Name 1","Name 2"])
jenkinsPipeline{
exampleArg = "yes"
}
但又没有成功。错误总是说
No such DSL method 'examFun()' found among steps
更新 - 新错误
感谢 zett42 ,我已经成功解决了这个错误。但是,我遇到了下面提到的另一个错误:
java.lang.NullPointerException: Cannot invoke method getAt() on null object
为了解决这个问题,就我而言,我需要将examFun() 分配给我的Jenkinsfile 中的一个变量,并将其作为jenkinsPipeline{} 中的参数传递。功能代码如下:
Jenkins 文件:
@Library('some-shared-lib') _
def list = jenkinsPipeline.examFun(['Name 1','Name 2'])
def names = jenkinsPipeline.someFoo(list)
jenkinsPipeline{
exampleArg = "yes"
choiceArg = names
}
jenkinsPipeline.groovy:
def examFun(List<String> val){
return [
new randClass(name: val[0], place: 'London')
new randClass(name: val[1], place: 'Berlin')
]
}
def call(body) {
def params= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = params
body()
pipeline {
// My entire pipeline is here
parameters{
choice(name: "Some name", choices: params.choiceArg, description:"Something")
}
// Demo stage
stage("Something"){
steps{
script{
projectName = params.exampleArg
}
}
}
}
}
def someFoo(list) {
def result = []
list.each{
result.add(it.name)
}
return result
}
class randClass {
String name
String place
}
【问题讨论】:
标签: jenkins groovy jenkins-shared-libraries