title: How can I call a groovy script from a Jenkins file?
body: <p>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:</p>
  
  <pre><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()
    }
  }
  </code></pre>
  
  <p>file.groovy</p>
  
  <pre><code>#!groovy
  
  def firstTest(){
  
    node('test-node'){
      
      stage('build'){
        echo "Second stage"
      }
      
      stage('Cleanup workspace'){
        deleteDir()
      }
    }
  }
  </code></pre>
  
  <p>Looks like the Jenkinsfile is able to call file1.groovy but always gives me an error:</p>
  
  <pre><code>java.lang.NullPointerException: Cannot invoke method firstTest() on null object
  </code></pre>