Search code examples
groovyjenkins-pipelinejenkins-groovy

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

Solution

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