我必须更新模板文件中的一个变量,所以使用groovy模板引擎进行如下操作:
> def installerTemplate=readFile "${template}"
> def binding=[
> 'exeName': exeName
> ]
> def engine=new groovy.text.SimpleTemplateEngine()
> def tpl=engine.createTemplate(installerTemplate).make(binding)
> writeFile file: "${target}", text: "${tpl}".toString()
但是在这里,我得到了以下权限错误! 我在这里看到了类似的问题,但大多数地方解决方案是更改Jenkins安全配置,但在我的情况下,这不是选项。 所以有没有其他方法可以解决这个问题,而无需触及Jenkins配置。
或者如果你可以在groovy中建议一些其他方法而不是使用模板!
非常感谢任何建议
谢谢, Ganesh
> org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
> Scripts not permitted to use new groovy.text.SimpleTemplateEngine
> at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectNew(StaticWhitelist.java:184)
> at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:148)
> at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:197)
> at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:202)
> at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.constructorCall(SandboxInvoker.java:21)
> at WorkflowScript.createInstallCmdFile(WorkflowScript:76)
> at WorkflowScript.run(WorkflowScript:55)
> at ___cps.transform___(Native Method)
> at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:96)
> at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:77)
> at sun.reflect.GeneratedMethodAccessor425.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:174)
> at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
> at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
> at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
> at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
> at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:19)
> at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:35)
> at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32)
> at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
> at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:32)
> at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
> at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:331)
> at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:82)
> at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243)
> at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231)
> at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
> at java.util.concurrent.FutureTask.run(FutureTask.java:266)
> at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
> at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
> at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
答案 0 :(得分:1)
使用许多签名需要确认并添加到白页,SimpleTemplateEngine也属于它们;
我有一项任务要做你需要的事情;
这就是我得到的:
private static Object binding1(Map map, String string) {
Template template = new StreamingTemplateEngine().createTemplate(string)
return template.make(map.withDefault { "<not found>" })
}
但是,由于您没有jenkins的管理员权限,您可以实现以下内容:
private static Object binding2(Map map, String string) {
for (def data : map) {
string = string.replace("\${" + data.getKey() + "}", data.getValue().toString())
}
return string
}
希望它有所帮助。