类:groovy.lang.Binding中没有名为api的属性错误。

22

我正在尝试在Jenkins上构建一个流水线,该流水线在节点上运行一个命令,并通知我以下错误:

 groovy.lang.MissingPropertyException: No such property: api for class: groovy.lang.Binding
        at groovy.lang.Binding.getVariable(Binding.java:63)
        at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
        at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:289)
        at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:293)
        at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
        at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)

我不知道node命令是一个错误的命令还是用于另一个错误 这是管道文件:

def call() {
        pipeline { 
            agent any 
            
            parameters {
                string(name: 'branch', defaultValue: 'refs/heads/develop', description: 'git branch where fetch sourcecode')
            }
            
            environment {
                GIT_URL = getGitRepoURL()
                GIT_CREDENTIALS = '*******'
            }
            
            tools {
                nodejs "node"
            }
            
            triggers {
                cron('H 06 * * 1-5')
            }
            
            stages {
                stage ('Initialize'){
                    steps { 
                        echo 'initializing'
                        deleteDir()
                        bat '''
                            echo "PATH = %PATH%"
                            echo "M2_HOME = %M2_HOME%"
                        ''' 
                    }
                }
    
                stage ('Sourcecode'){
                    steps { 
                        echo 'fetching sourcecode from ' + env.GIT_URL
                        checkout([$class: 'GitSCM', branches: [[name: params.branch]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: env.GIT_CREDENTIALS, url: env.GIT_URL]]])            
                    }
                }
                
                stage ('Execute raml2html'){
                    steps {
                        sh 'cd ..\HelpDevelopsAPI
                        node raml2html -s %WORKSPACE% -c %WORKSPACE%\..\apidef-aqj-commons -o /server/api --cat .*.html --schema /server/schema/%JOB_BASE_NAME% --mock /server/mock/%JOB_BASE_NAME%
                        cd %WORKSPACE%'
                    }
                }
            }
        }
    }
        
    def getGitRepoURL() {
        String projectName = env.JOB_BASE_NAME
        print 'projectName '+projectName  +'\n'
        String[] projectParts = projectName.tokenize( '-' )
        String bian = projectParts[1]
        String name = projectParts[0]+'-'+projectParts[1]+'-'+projectParts[2]
        echo 'exampleurl'+bian+'/'+name+'.git'
        return 'exapleurl'+bian+'/'+name+'.git'
    }
4个回答

26

你看到的错误意味着 Jenkins 在你的脚本中找到了单词 api,并尝试将其解释为 Jenkins 的变量或命令,但没有找到匹配项。我在你的脚本中搜索了单词 api,发现了2个问题:

  • 你正在尝试使用多行字符串,但使用的是单引号包装。你需要改用三重引号(有关多行字符串的更多信息,请点击此处)。我认为这是你看到的错误消息的原因,因为 pipeline 没有识别出你字符串的第二行是字符串的一部分而不是 pipeline 代码。
  • 在 bash 步骤中,你正在使用类似批处理的变量(%VAR%),而应该使用 $VAR

请尝试更改:

   sh 'cd ..\HelpDevelopsAPI
       node raml2html -s %WORKSPACE% -c %WORKSPACE%\..\apidef-aqj-commons -o /server/api --cat .*.html --schema /server/schema/%JOB_BASE_NAME% --mock /server/mock/%JOB_BASE_NAME%
       cd %WORKSPACE%'
致:
   sh '''cd ..\HelpDevelopsAPI
       node raml2html -s $WORKSPACE -c $WORKSPACE\..\apidef-aqj-commons -o /server/api --cat .*.html --schema /server/schema/$JOB_BASE_NAME --mock /server/mock/$JOB_BASE_NAME
       cd $WORKSPACE'''

1
在我的情况下,一个多余的逗号留在了出现问题关键词之前的一行中(在我的情况下是echo),导致出现 No such property: echo ... - mirekphd

3

您在shell任务步骤中混合使用了Windows特定的构造/语法。除了@vasiliki-siakka所提出的建议外: 用斜杠/替换字符串中的反斜杠目录分隔符\,并用双引号"将变量括起来以处理目录名称中可能存在的空格:

sh '''
    cd ../HelpDevelopsAPI
    node raml2html -s "${WORKSPACE}" -c "${WORKSPACE}/../apidef-aqj-commons" -o /server/api --cat .*.html --schema "/server/schema/${JOB_BASE_NAME}" --mock "/server/mock/${JOB_BASE_NAME}"
    cd "${WORKSPACE}"
'''

或者
如果你的Jenkins运行在Windows上,使用bat任务步骤,并以Windows风格引用变量%VARIABLE_NAME%,但是你仍然需要像这样转义反斜杠\\,因为Jenkinsfile语法基于Groovy。未经测试的示例:

bat '''
    cd "%WORKSPACE%"
    node raml2html -s "%WORKSPACE%" -c "%WORKSPACE%\\..\\apidef-aqj-commons" -o \\server\\api --cat .*.html --schema "\\server\\schema\\%JOB_BASE_NAME%" --mock "\\server\\mock\\%JOB_BASE_NAME%"
    cd "${WORKSPACE}"
'''

1

我有一个类似的问题 -


No such property: config for class: groovy.lang.Binding error

在我的JenkinsFile中,我引用了一个名为config的对象 - 我检查了之前的版本,发现我已经删除了一个配置对象。
错误信息为:在类groovy.lang.Binding中没有名为config的属性。 请搜索您的Jenkins文件并查看是否使用了未声明的对象。

0

在声明式管道中,“def”部分是不被允许的,尽管它是脚本管道的一部分。请参考此博客“如何在脚本管道中使用embed”- 如何在Jenkins管道中使用'def'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接