Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 946
manish soni Avatar asked Aug 22 '19 05:08

manish soni


2 Answers

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'
}
like image 61
quietbird Avatar answered Sep 19 '22 12:09

quietbird


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

}
like image 29
Suresh Kandru Avatar answered Sep 19 '22 12:09

Suresh Kandru