Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Im trying to build a pipeline on Jenkins that runs a command on node and informs me of the following error:

 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)

i dont know the node command its an error command or its for another error this is de pipeline file:

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'
    }
like image 609
Mario Ramos García Avatar asked Aug 08 '19 10:08

Mario Ramos García


People also ask

What is binding in groovy?

The binding is defined in the Groovy API documentation as follows: “Represents the variable bindings of a script which can be altered from outside the script object or created outside of a script and passed into it.”.

What is missing property exception in groovy?

Class MissingPropertyExceptionAn exception occurred if a dynamic property dispatch fails with an unknown property. Note that the Missing*Exception classes were named for consistency and to avoid conflicts with JDK exceptions of the same name. See Also: Serialized Form.

How groovy is used in Jenkins?

It can be used to orchestrate your pipeline in Jenkins and it can glue different languages together meaning that teams in your project can be contributing in different languages. Groovy can seamlessly interface with the Java language and the syntax of Java and Groovy is very similar.


Video Answer


3 Answers

The error you see means that jenkins is finding the word api in your script and tries to interpret is a variable or command of jenkins, and doesn't find a match. I searched for the word api in your script and saw 2 issues:

  • You're trying to use a multiline string but you're wrapping it with single quotes. You need to use triple quotes instead (more info on multiline strings here). I think this is causing the error message you see, because pipeline doesn't recognise that the second line of your string is part of a string and not pipeline code.
  • You're using batch like variables (%VAR%) in a bash step. You should be using $VAR instead.

Try changing:

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

to:

   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'''
like image 123
Vasiliki Siakka Avatar answered Oct 13 '22 18:10

Vasiliki Siakka


You're mixing Windows-specific constructs/syntax in the shell task step. In addition to the suggestions made by @vasiliki-siakka: replace the backslash directory separators \ in your strings with forward slash /, and surround the variables with " (double quotes) to handle potential spaces in directory names:

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

OR
if your Jenkins runs on Windows, use the bat task step, and reference the variables in Windows style %VARIABLE_NAME%, but you still have to escape the backslashes like this \\ because the Jenkinsfile syntax is Groovy based. Untested example:

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}"
'''
like image 37
t0r0X Avatar answered Oct 13 '22 18:10

t0r0X


I had a similar issue -

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

In my JenkinsFile, I was referencing an object named config - i checked previous version and realised I'd deleted a config object

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

Search your jenkins file and see if you're using an undeclared object.

like image 33
David McEleney Avatar answered Oct 13 '22 18:10

David McEleney