我有处理结帐的带有 handleCheckout.groovy 的 jenkins_library:
def handleCheckout = {
if (env.gitlabMergeRequestId) {
sh "echo 'Merge request detected. Merging...'"
checkout([
$class : 'GitSCM',
branches : [[name: "${env.gitlabSourceNamespace}/${env.gitlabSourceBranch}"]],
extensions : [
[$class: 'PruneStaleBranch'],
[$class: 'CleanCheckout'],
[
$class : 'PreBuildMerge',
options: [
fastForwardMode: 'NO_FF',
mergeRemote : env.gitlabTargetNamespace,
mergeTarget : env.gitlabTargetBranch
]
]
],
userRemoteConfigs: [
[
credentialsId: env.CREDENTIALS_ID,
name : env.gitlabTargetNamespace,
url : env.gitlabTargetRepoSshURL
],
[
credentialsId: env.CREDENTIALS_ID,
name : env.gitlabSourceNamespace,
url : env.gitlabSourceRepoSshURL
]
]
])
} else {
sh "echo 'No merge request detected. Checking out current branch'"
checkout([
$class : 'GitSCM',
branches : [[name: "${env.sourceBranch}"]],
extensions : [
[$class: 'PruneStaleBranch'],
[$class: 'CleanCheckout']
],
userRemoteConfigs: [[credentialsId: env.CREDENTIALS_ID, url: env.GIT_URL]]
])
}
}
在导入 jenkins-library 之后,我试图从管道中调用它:
@Library('jenkins-util-lib') _
stage('prepare') {
sh "env | sort"
handleCheckout()
它以错误结束:
No signature of method: handleCheckout.call() is applicable for argument types: () values: []
Possible solutions: wait(), any(), wait(long), main([Ljava.lang.String;), any(groovy.lang.Closure), each(groovy.lang.Closure)
在管道内声明和调用方法时效果很好。