函数的作用
用于判断某个参数或者字段是否存在。
用法
例子一
def projectStr = "P1,P2,P3"
pipeline {agent anyparameters {extendedChoice(defaultValue: "${projectStr}",description: '选择要发布的项目',multiSelectDelimiter: ',',name: 'SELECT_PROJECTS',quoteValue: false,saveJSONParameterToFile: false,type: 'PT_CHECKBOX',value: "${projectStr}",visibleItemCount: 1000)}stages {stage('Git Pull') {steps {script {// 判断参数字段是否存在if (this.hasProperty("SELECT_PROJECTS")) {echo "${SELECT_PROJECTS}"} else {echo "SELECT_PROJECTS is null"}}}}}
}
出现的场景: 我想通过参数配置进来一个选择框列表,根据选择的情况做不同的处理;在过程中,我发首次构建Job的时候会报错误。错误如下:
groovy.lang.MissingPropertyException: No such property: SELECT_PROJECTS for class: groovy.lang.Bindingat 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:78)at ___cps.transform___(Native Method)at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)at sun.reflect.GeneratedMethodAccessor323.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)at com.cloudbees.groovy.cps.Next.step(Next.java:83)at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:177)at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:166)at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:166)at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:187)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:420)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:95)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:750)
原因是:可能是因为首次构建,parameters
中的extendedChoice
还没有初始化好导致本次构建上线文没有SELECT_PROJECTS
变量 。我又没有做提前判断,直接取值去判断的。 代码如下:
def projectStr = "P1,P2,P3"
pipeline {agent anyparameters {extendedChoice(defaultValue: "${projectStr}",description: '选择要发布的项目',multiSelectDelimiter: ',',name: 'SELECT_PROJECTS',quoteValue: false,saveJSONParameterToFile: false,type: 'PT_CHECKBOX',value: "${projectStr}",visibleItemCount: 1000)}stages {stage('Git Pull') {steps {script {// 不是使用hasProperty函数判断的,而是直接用变量判断的,所以会报错if (null == SELECT_PROJECTS) {echo "${SELECT_PROJECTS}"} else {echo "SELECT_PROJECTS is null"}}}}}
}
遇到的问题
问题一
没有执行的权限,解决方法授权执行即可,具体怎么授权自行百度。
错误如下:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods hasProperty java.lang.Object java.lang.Stringat org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:243)at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:123)at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:178)at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:182)at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)at WorkflowScript.run(WorkflowScript:78)at ___cps.transform___(Native Method)at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:90)at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)at sun.reflect.GeneratedMethodAccessor301.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)at com.cloudbees.groovy.cps.Next.step(Next.java:83)at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:177)at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:166)at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:166)at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:187)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:420)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:95)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:750)