我們正在使用 JCasC 定義如下 jenkins 共享庫:
controller:
JCasC:
configScripts:
jenkins-casc-unclassified: |
unclassified:
globalLibraries:
libraries:
- defaultVersion: "master"
implicit: true
name: "com.company.jenkins"
retriever:
modernSCM:
scm:
github:
configuredByUrl: true
credentialsId: "..."
id: "..."
repoOwner: "Company"
repository: "Company_CICD"
repositoryUrl: "https://github.com/company/Company_CICD.git"
traits:
- gitHubBranchDiscovery:
strategyId: 1
- gitHubPullRequestDiscovery:
strategyId: 1
- gitHubForkDiscovery:
strategyId: 1
trust: "gitHubTrustPermissions"
我們為Kubernetes Plugin做了一個方便的功能,可以讓我們大大縮短代碼。然而,試圖消除僅 2 行,我們遇到了一個奇怪的錯誤,我們想澄清一下:
java.lang.NoSuchMethodError: No such DSL method 'getPodTemplate'
奇怪的是,在不更改匯入或函式名稱的情況下,它會顯示出來。
例如,這有效:
// src/com/company/jenkins/Util.groovy
package com.onscale.jenkins
def getPodTemplate(String label, List<String> containers, List<String> volumes, String yaml) {
containers = containers.collect {
value -> return getContainerTemplate(value)
}
volumes = volumes.collect {
value -> return getVolume(value)
}
return [
label: label,
yaml: yaml,
containers: containers,
volumes: volumes
]
}
// Jenkinsfile
def util = new com.onscale.jenkins.Util()
podTemplate (
util.getPodTemplate(
'jenkinsbuild', // Label
['jnlp', 'docker', 'kubectl'], // Containers
['host-path'], // Volumes
podSpec
)
)
但這不會:
// src/com/company/jenkins/Util.groovy
package com.onscale.jenkins
def getPodTemplate(String label, List<String> containers, List<String> volumes, String yaml) {
containers = containers.collect {
value -> return getContainerTemplate(value)
}
volumes = volumes.collect {
value -> return getVolume(value)
}
return podTemplate(
label: label,
yaml: yaml,
containers: containers,
volumes: volumes
)
}
// Jenkinsfile
def util = new com.onscale.jenkins.Util()
util.getPodTemplate(
'jenkinsbuild', // Label
['jnlp', 'docker', 'kubectl'], // Containers
['host-path'], // Volumes
podSpec
)
兩者之間的所有變化是 getPodTemplate 是否回傳 podTemplate。
我們四處尋找。這個堆疊溢位讓我們感到困惑,因為我們沒有使用var/function.groovy創建共享庫的方法(我不確定每種“種類”的共享庫被稱為什么),我們使用“new”和 globalLibrary 進行匯入。
有任何想法嗎?
uj5u.com熱心網友回復:
當函式未編譯時,似乎會發生此作業 DSL 錯誤。在這種情況下,該函式不會編譯,因為 groovy 會執行此操作,您可以在不帶括號的情況下呼叫函式。
例子:
def foo(name, closure) {println name; println closure()}
可以通過以下所有方式呼叫。
foo(“asdf”,{return “asdf”})
foo “asdf” {return “asdf”}
foo(“asdf”) {return “asdf”}
帶括號和不帶括號
然而這個:
foo(“asdf”)
會拋出錯誤,MissingMethodException因為 foo 需要 2 個引數。而我最初認為它會回傳部分。
因此,當我們回傳沒有閉包的 podTemplate 作為最后一個引數時,我們實際上破壞了該方法的編譯,因此我們會收到 DSL 錯誤,因為共享庫無法編譯。
解決辦法是添加一個閉包的final引數,作為最后一個引數傳給podTemplate
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326197.html
下一篇:如何在ubuntu上安裝Snap
