0

I would like to integrate a Global library into my build flow. I have written a basic function

srv/core/jenkins/Checks.groovy:

package core.jenkins

class Checks implements Serializable {
def script

Checks(script) {
    this.script = script
}

def fileExists(){
    script.echo "File exists in the repo."
    }
}

And it is exposed as a global var

vars/fileExisits.groovy:

def call() {
    new core.jenkins.Checks(this).fileExists()
}

While configuring the Global Shared Library settings in Jenkins, I have the following settings:

enter image description here

Now in my jenkinsfile, Im doing something like this:

pipeline {
    agent { label 'master' }
    stages {
        stage('Check for md files'){
            steps {
                sh 'echo hello'
                script {
                    checks.fileExists()
                }
            }
        }
    }
}

This always gives the error

groovy.lang.MissingPropertyException: No such property: checks for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at 

For it to work, I have to add the lines to the top of my Jenkinsfile

import core.jenkins.Checks
def checks = new Checks(this)

Is there a way for me to invoke the function fileExists from a library without having to add the above 2 lines always ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jason Stanley
  • 386
  • 1
  • 3
  • 20

1 Answers1

3

Just replace:

checks.fileExists()

with:

fileExists()

All Groovy scripts that implements def call() methods and are stored in the vars/ folder can be triggered by their script file name. Alternatively, if you would like to keep checks.fileExists() syntax, then you need to create vars/checks.groovy script file and implement def fileExists() method inside of it.

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131