缺少必需的上下文类 hudson.FilePath 也许您忘记用提供此内容的步骤包围代码,例如:节点

man*_*oni 9 groovy pipeline function-call jenkins jenkins-pipeline

当我在 Jenkinsfile 中加载另一个 groovy 文件时,它显示以下错误。

“缺少所需的上下文类 hudson.FilePath 也许您忘记用提供此内容的步骤包围代码,例如:节点”

我制作了一个包含函数的 groovy 文件,我想在我的声明性 Jenkinsfile 中调用它。但它显示一个错误。

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}')
                }
             }
         }
     }
  }
Run Code Online (Sandbox Code Playgroud)

结果 -

org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: 缺少必需的上下文类 hudson.FilePath 也许你忘了用提供这个的步骤来包围代码,例如:节点

建议我什么是正确的方法。

小智 6

您需要使用脚本化管道并将“加载”指令放入节点部分(请参阅此问题),或者如果您已经在使用声明性管道(似乎是这种情况),则可以将其包含在“环境”部分中:

environment {
    REPO_PATH='/home/manish/Desktop'
    APP_NAME='test'
    MY_FUN = load 'testfun.groovy'
}
Run Code Online (Sandbox Code Playgroud)