簡體   English   中英

向 Jenkins 共享庫提供參數

[英]Feeding parameters to Jenkins shared library

我有一個 Jenkins 共享庫,用於我的 Jenkinsfile。 我的庫有一個完整的管道,它有一個在我的管道中使用的函數(我們稱之為examFun() )。

我的詹金斯檔案:

@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 )。 這篇文章之后,我嘗試了以下內容:

我改變了我的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{} 運行代碼如下: Jenkinsfile

@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
}

當您收到錯誤“沒有此類 DSL 方法...”時,通常僅表示參數類型和實際傳遞的參數不兼容,這里就是這種情況。

函數examFun()需要一個數組,但您實際上傳遞的是一個ArrayList 正如這個答案中所解釋的,低級數組在 Groovy 中並不是真正慣用的。

試試這個:

def examFun(List<String> val){
  return [
     new randClass(name: val[0], place: 'London')
     new randClass(name: val[1], place: 'Berlin')
  ]
}

List是更通用的接口,由ArrayList實現。

現在,您的第一個 jenkinsfile 示例應該可以工作了:

jenkinsPipeline.examFun(['Name 1','Name 2'])

您實際上可以擁有額外的命名方法,即使您已經擁有一個call方法。 我一直在用這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM