22

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'
    }
John Smith
  • 7,243
  • 6
  • 49
  • 61
Mario Ramos García
  • 755
  • 3
  • 8
  • 20

4 Answers4

25

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'''
t0r0X
  • 4,212
  • 1
  • 38
  • 34
Vasiliki Siakka
  • 1,185
  • 8
  • 15
  • 1
    In my case a redundant comma was left over in a line preceding the offending keyword (in my case `echo`, resulting in `No such property: echo ...`). – mirekphd Aug 07 '23 at 07:12
3

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}"
'''
t0r0X
  • 4,212
  • 1
  • 38
  • 34
1

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.

David McEleney
  • 3,397
  • 1
  • 26
  • 32
0

the 'def' section in the declarative pipeline is not allowed though it is part of the scripted pipeline. refer this blog "how to use embed with scripted pipeline" - How can I use `def` in jenkins pipeline

Vikk_King
  • 81
  • 1
  • 7