Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node

manish soni picture manish soni · Aug 22, 2019 · Viewed 13.3k times · Source

When i load another groovy file in Jenkinsfile it show me following error.

"Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node"

I made a groovy file which contains a function and i want to call it in my Declarative Jenkinsfile. but it shows an error.

My Jenkinsfile--->

def myfun = load 'testfun.groovy'
pipeline{
    agent any
    environment{
        REPO_PATH='/home/manish/Desktop'
        APP_NAME='test'
    }
    stages{
        stage('calling function'){
            steps{
                script{
                    myfun('${REPO_PATH}','${APP_NAME}')
                }
             }
         }
     }
  }

Result--

org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node

Suggest me what is the right way to do it.

Answer

quietbird picture quietbird · Nov 11, 2019

You either need to use a scripted pipeline and put "load" instruction inside the node section (see this question) or if you are already using a declarative pipeline (which seems to be the case), you can include it in "environment" section:

environment {
    REPO_PATH='/home/manish/Desktop'
    APP_NAME='test'
    MY_FUN = load 'testfun.groovy'
}