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

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.



Solution 1:[1]

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'
}

Solution 2:[2]

We have to wrap with node {}, so that jenkins executors will execute on node, Incase if we would like to execute on any specific agent node, we can mention like node('agent name'){}

example here :

node {

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}')
                }
             }
         }
     }
  }

}

Solution 3:[3]

I got this error message when I was calling a sh script that does not not exist in the repository / file system. Please look in the stack trace the following line:

at WorkflowScript.run(WorkflowScript:135)

The 135 marks the line in Jenkinsfile, on which the missing script or error is happening.

Solution 4:[4]

Loading the function in an initial script block inside the pipeline worked for me. Something like below:

def myfun

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 quietbird
Solution 2 rodee
Solution 3 PHZ.fi-Pharazon
Solution 4 devatherock