5

I want to collect functions common for pipelines in a separate file. I created directories' structure:

vars/
...commonFunctions.groovy
pipeline.jenkinsfile
anotherPipeline.jenkinsfile

commonFunctions.groovy:

def buildDocker(def par, def par2) {
  println("build docker...")
}

return this;

In pipeline.jenkinsfile I want to call buildDocker function. How can I do this? I tried simply commonFunctions.buildDocker(par1, par2) in the pipelines, but get MethodNotFound error.

UPD:

relevant part of pipeline.jenkinsfile:

    stage('Checkout') {
        steps {
            checkout([$class           : 'GitSCM',
                      branches         : [[name: gitCommit]],
                      userRemoteConfigs: [[credentialsId: gitCredId, url: gitUrl]]
            ])
        }
    }

    stage("Build Docker") {
        steps {
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                script {
                    // here want to call function from another file
                    commonFunctions.buildDocker(par1, par2)
                }
            }
        }
    }
lleviy
  • 406
  • 5
  • 16
  • Did you define a shared library? Did you use `@Library('...') _` in your Jenkinsfile? – MaratC Apr 18 '21 at 09:22
  • @MaratC I tried `@Library('commonFunctions') _`, but get `ERROR: Could not find any definition of libraries [commonFunctions] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: Loading libraries failed` – lleviy Apr 18 '21 at 09:54
  • For shared Library you have to add this into Jenkins configuration and also need to maintain java like folder structure. See https://www.jenkins.io/doc/book/pipeline/shared-libraries/ – np2807 Apr 19 '21 at 14:33

2 Answers2

8

First try to load that file in pipeline.jenkinsfile like this and use it like this. So your answer would be this

load("commonFunctions.groovy").buildDocker(par1, par2)

Make sure you add return this to the end of groovy script which is in your case commonFunctions.groovy inside file

Dashrath Mundkar
  • 7,956
  • 2
  • 28
  • 42
  • With this approach I get `NoSuchFileException: {PATH_TO_JENKINS_JOB}/vars/commonFunctions.groovy` – lleviy Apr 17 '21 at 19:13
  • @lleviy then just remove ```vars/``` and try – Dashrath Mundkar Apr 17 '21 at 19:18
  • 1
    your approach with `vars/` in path worked when I added Checkout step to my pipeline and `return this;` to the end of my groovy script (edited the question to mark your solution as correct). Thanks! – lleviy Apr 18 '21 at 09:57
1

You can try also this plugin: Pipeline Remote Loader You do not need to use checkout scm

Here is an example from the documentation how to use it:

stage 'Load a file from GitHub'
def helloworld = fileLoader.fromGit('examples/fileLoader/helloworld', 
        'https://github.com/jenkinsci/workflow-remote-loader-plugin.git', 'master', null, '')

stage 'Run method from the loaded file'
helloworld.printHello()
Catalin
  • 366
  • 2
  • 8