1

我有处理结帐的带有 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)

在管道内声明和调用方法时效果很好。

4

1 回答 1

2

你能请你的设置吗

1)handleCheckout.groovy 必须位于vars文件夹中(必须检查) 2)handleCheckout.groovy 必须有def call(您必须更改) 3)在您的管道中,您必须运行 Jenkinsfilename(),在您的情况下handleCheckout

以防万一文档 - https://jenkins.io/doc/book/pipeline/shared-libraries/

因此,您必须将程序名称更改为def call并检查 Jenkins 文件位置(必须在共享库内的 vars 文件夹中)

于 2020-01-22T09:01:53.480 回答