title: jenkinsfile loading local variables in groovy script
body: <p>Not a lot of experience programming but happy to learn.</p>
  
  <p>Question: Sending jenkinsfile build error data to mattermost works just fine locally in the file, but when loaded as a .groovy script it fails "silently". Any guidance on why / how I can post this information to a channel in a loaded groovy script?</p>
  
  <p>Jenkinsfile</p>
  
  <pre><code>#!groovy
  properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '5', artifactNumToKeepStr: '5']]])
  
  node('node_name'){
  
      def err = null
      currentBuild.result = "SUCCESS"
      role_name = "deploy"
  
      try {
          timeout(60){
              stage "${role_name}"
                  deleteDir()
                  checkout scm
                  sh 'introduce error here'
          }
      }
      catch (error){
          err = error
          currentBuild.result = "FAILURE"
          load "ci/curlBuildFailed.groovy"
      }
      finally {
          if (err){
              throw err
          }
      }
  }
  </code></pre>
  
  <p>curlBuildFailed.groovy</p>
  
  <pre><code>sh "curl -i -X POST -d \'payload={" +
  "\"text\": \"${env.JOB_NAME}: ${env.BUILD_URL} - build failed with ${err}!\", " +
  "\"username\": \"JenkinsBot\", " +
  "\"icon_url\": \"$FAIL_BUILD\"}\' $DEVOPS_NOTIFY_URL "
  </code></pre>
  
  <p>Running the above produces this:</p>
  
  <pre><code>[cure-deploy] Running shell script
  + introduce error here
  /home/jenkins/remote/workspace/blablabla/script.sh: line 2: introduce: command not found
  [Pipeline] }
  [Pipeline] // dir
  [Pipeline] }
  [Pipeline] // timeout
  [Pipeline] load
  [Pipeline] { (ci/curlBuildFailed.groovy)
  [Pipeline] }
  [Pipeline] // load
  [Pipeline] }
  [Pipeline] // wrap
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] }
  [Pipeline] // load
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  ERROR: script returned exit code 127
  Finished: FAILURE
  </code></pre>
  
  <p>Notes:</p>
  
  <ul>
  <li>Dropping the contents of <code>curlBuildFailed.groovy</code> into the same
  location as <code>load "ci/curlBuildFailed.groovy"</code> creates the desired
  output.  </li>
  <li>Have narrowed down the culprit to <code>${err}</code> in
  <code>curlBuildFailed.groovy</code>, as soon as I remove that variable it posts
  to mattermost just fine.</li>
  <li>(We have about 30 ansible roles similar to this one so trying to abstract the duplicate code to a easier to maintain structure)</li>
  <li>Have searched around for a few hours and it seems similar to these but I hadn't found a solve yet: </li>
  <li><a href="https://github.com/jenkinsci/workflow-basic-steps-plugin/blob/master/CORE-STEPS.md#plain-catch-blocks" rel="nofollow noreferrer">jenkins plain catch blocks</a></li>
  <li><a href="https://kokosaraie.blogspot.com/" rel="nofollow noreferrer">don't think I need a 'returns this;' but who knows</a></li>
  <li><a href="https://stackoverflow.com/questions/42246034/passing-variables-in-jenkinsfile-closure">Passing Variables in Jenkinsfile Closure</a></li>
  </ul>
  
  <p>Thank you for your time!
  - Sam</p>