1

I have jenkins_library with handleCheckout.groovy which handles checkout:

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]]
        ])
    }
}

I'm trying to call it from pipeline after imporing jenkins-library like that:

@Library('jenkins-util-lib') _

 stage('prepare') {
            sh "env | sort"
            handleCheckout()

It ends with error:

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)

Works well while method is declared and called inside the pipeline.

1 Answers1

2

Could you please your setup

1) handleCheckout.groovy must be located in vars folder (has to check) 2) handleCheckout.groovy must have def call (you have to change) 3) Inside your pipeline you have to run Jenkinsfilename(), in your case handleCheckout

Just in case doc - https://jenkins.io/doc/book/pipeline/shared-libraries/

So at the and you have to change procedure name to def call and check Jenkins file location (must be in vars folder inside shared library)

max.ivanch
  • 339
  • 2
  • 5
  • 1) Was there already 2) Did the trick I wasn't aware procedure needs to have `def call` to be called from pipeline. Works like a charm now, thanks. – Daniel Surowiec Jan 22 '20 at 09:49
  • Jenkins creates an object handleCheckout based on the file name of your groovy file under /vars. In your original syntax, you defined handleCheckout as a property of the object handleCheckout. You should see the def call() ... a bit like the constructor of this object. It supports the use of handleCheckout as a build step in declarative syntax. – Mzzl Jan 24 '20 at 15:36