调用导入的Groovy库函数报错:No such property: git for class: groovy.lang.Binding
Jenkins共享库执行报错:MissingPropertyException: No such property: git for class: groovy.lang.Binding
错误日志
+ echo inside MAIN in stage Formatting inside MAIN in stage Formatting [Pipeline] } [Pipeline] // container Post stage Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: c454d67d-2abf-4f99-aa8b-281ea186c072 groovy.lang.MissingPropertyException: No such property: git for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:285) at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:375) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:379) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:355) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29) at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20) at WorkflowScript.run(WorkflowScript:181) at ___cps.transform___(Native Method)
共享库代码(checkoutGit.groovy)
def call(Map config) { println "Starting the script...1" }
Jenkinsfile代码片段
pipeline { agent { label 'stuff1' } options { ansiColor('xterm') parallelsAlwaysFailFast() buildDiscarder(logRotator(artifactDaysToKeepStr: '30')) throttleJobProperty( throttleEnabled: true, throttleOption: 'project', maxConcurrentPerNode: 1, maxConcurrentTotal: 30, ) } stages { stage('Formatting and Style') { agent { label 'stuff2' } when { expression { !params.Refresh } } steps { container('main') { sh 'echo "inside MAIN in stage Formatting"' checkoutGit( credentialsId: git-ssh, remoteUrl: 'ssh://git.t.com:123', refspec: '+$GERRIT_REFSPEC:$GERRIT_REFSPEC', branch: 'main' ) } } } } }
问题分析与解决
报错核心是Groovy解析时找不到git属性,原因是Jenkinsfile中checkoutGit的credentialsId参数值git-ssh未加引号。Groovy会将无引号的git-ssh视为表达式,尝试查找名为git的变量并执行减法操作,而当前上下文不存在git变量,因此抛出MissingPropertyException。
修复方法:将git-ssh用单引号或双引号包裹,明确为字符串类型:
checkoutGit( credentialsId: 'git-ssh', // 添加引号 remoteUrl: 'ssh://git.t.com:123', refspec: '+$GERRIT_REFSPEC:$GERRIT_REFSPEC', branch: 'main' )
内容的提问来源于stack exchange,提问作者AhmFM
相关产品推荐
相关产品推荐

