Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a groovy script from a Jenkins file?

Tags:

jenkins

groovy

I am trying to separate out the contents from a Jenkinsfile into a groovy script to make. But it fails to call these scripts: Here is the code:

#!/usr/bin/env groovy

node('test-node'){

  stage('Checkout') {
    echo "${BRANCH_NAME} ${env.BRANCH_NAME}"
    scm Checkout

  }

  stage('Build-all-targets-in-parallel'){

    def workspace = pwd()
    echo workspace
    parallel(
      'first-parallel-target' :
       {
         // Load the file 'file1.groovy' from the current directory, into a variable called "externalMethod".
         //callScriptOne()
         def externalMethod = load("file1.groovy")
         // Call the method we defined in file1.
          externalMethod.firstTest()
       },
       'second-parallel-target' :
      {
         //callScriptTwo()
         def externalMethod = load("file2.groovy")
         // Call the method we defined in file1.
         externalMethod.testTwo()
        }
    )
  }
  stage('Cleanup workspace'){
    deleteDir()
  }
}

file.groovy

#!groovy

def firstTest(){

  node('test-node'){
    
    stage('build'){
      echo "Second stage"
    }
    
    stage('Cleanup workspace'){
      deleteDir()
    }
  }
}

Looks like the Jenkinsfile is able to call file1.groovy but always gives me an error:

java.lang.NullPointerException: Cannot invoke method firstTest() on null object
like image 395
user_dev Avatar asked Apr 10 '17 13:04

user_dev


People also ask

Is a Jenkins file a Groovy file?

The Jenkinsfile is written using the Groovy Domain-Specific Language and can be generated using a text editor or the Jenkins instance configuration tab. The Declarative Pipelines is a relatively new feature that supports the concept of code pipeline. It enables the reading and writing of the pipeline code.


2 Answers

If you want to have methods available in your Jenkinsfile from an external file you need to do the following

In your file1.groovy, return references to the methods

def firstTest() {
    // stuff here
}

def testTwo() {
    //more stuff here
}
...

return [
    firstTest: this.&firstTest,
    testTwo: this.&testTwo
]

EDIT

evaluate does not seem to be required

def externalMethod = evaluate readFile("file1.groovy")

or

def externalMethod = evaluate readTrusted("file1.groovy")

And as mentioned by @Olia

def externalMethod = load("file1.groovy")

should work

Here is a reference on readTrusted. Note that no parameter substitution is allowed (does a lightweight checkout)

From lightweight checkout:

If selected, try to obtain the Pipeline script contents directly from the SCM without performing a full checkout. The advantage of this mode is its efficiency; however, you will not get any changelogs or polling based on the SCM. (If you use checkout scm during the build, this will populate the changelog and initialize polling.) Also build parameters will not be substituted into SCM configuration in this mode. Only selected SCM plugins support this mode.

At least that works for me

like image 81
Rik Avatar answered Sep 25 '22 20:09

Rik


It looks like you missed return within the scripts you are loading:

return this

Please, check it here: https://jenkins.io/doc/pipeline/steps/workflow-cps/#load-evaluate-a-groovy-source-file-into-the-pipeline-script

So, your called loaded file will have a structure like:

def exampleMethod() {
    //do something
}

def otherExampleMethod() {
    //do something else
}
return this

This way you shouldn't get null object

like image 43
Olia Avatar answered Sep 24 '22 20:09

Olia