通过 Jenkins 脚本管道中的动态密钥获取环境 属性
Get env property by dynamic key in Jenkins scripted pipeline
我在 Jenkins 脚本化管道中动态获取 env
属性时遇到问题。我在管道中有一个 String 键。我们称它为 key
。
key
是通过连接两个字符串获得的:
def key = env.BRANCH_NAME + ".HOST";
然后我想通过那个键获得另一个 env
属性:
def remoteHost = env[key];
但这对我不起作用。
原问题。
我的存储库中有三个分支:dev
、testing
和 staging
。我还有三个全局属性:dev.HOST
、testing.HOST
、staging.HOST
。我想写这样的东西:
timestamps {
node() {
String remoteHost;
stage('Defining a remote host') {
def key = env.BRANCH_NAME + ".HOST";
remoteHost = env[key];
echo "remote host:" + remoteHost;
}
}
但正如我所说,env[key] 在那里不起作用。我得到下一个堆栈跟踪:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getAt java.lang.Object java.lang.String
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:279)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetArray(SandboxInterceptor.java:475)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:484)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetArray(Checker.java:489)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getArray(SandboxInvoker.java:45)
at com.cloudbees.groovy.cps.impl.ArrayAccessBlock.rawGet(ArrayAccessBlock.java:21)
at WorkflowScript.run(WorkflowScript:36)
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 jdk.internal.reflect.GeneratedMethodAccessor131.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=14=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:136)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
我怎样才能 属性 正确?或者我该如何解决这个问题?
詹金斯版本:2.249.2
虽然这在普通 Groovy 上有效,但使用以下语法 env[key]
在这里不起作用,因为管道 运行 在沙箱内,不允许此类访问.
你有两个选择:
1 - 使用 Groovy gstrings 并将 env[key]
更改为 env."${key}"
完全避免了沙箱错误。
2 - 如果您仍然喜欢这种访问方式,请在名为 'In-process Script Approval' 的 Jenkins 管理部分将该方法列入白名单。在这样做并再次 运行 管道之后,您应该不会再看到堆栈跟踪了。
我在 Jenkins 脚本化管道中动态获取 env
属性时遇到问题。我在管道中有一个 String 键。我们称它为 key
。
key
是通过连接两个字符串获得的:
def key = env.BRANCH_NAME + ".HOST";
然后我想通过那个键获得另一个 env
属性:
def remoteHost = env[key];
但这对我不起作用。
原问题。
我的存储库中有三个分支:dev
、testing
和 staging
。我还有三个全局属性:dev.HOST
、testing.HOST
、staging.HOST
。我想写这样的东西:
timestamps {
node() {
String remoteHost;
stage('Defining a remote host') {
def key = env.BRANCH_NAME + ".HOST";
remoteHost = env[key];
echo "remote host:" + remoteHost;
}
}
但正如我所说,env[key] 在那里不起作用。我得到下一个堆栈跟踪:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getAt java.lang.Object java.lang.String
at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:279)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetArray(SandboxInterceptor.java:475)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:484)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetArray(Checker.java:489)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getArray(SandboxInvoker.java:45)
at com.cloudbees.groovy.cps.impl.ArrayAccessBlock.rawGet(ArrayAccessBlock.java:21)
at WorkflowScript.run(WorkflowScript:36)
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 jdk.internal.reflect.GeneratedMethodAccessor131.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access[=14=]1(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:136)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:59)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
我怎样才能 属性 正确?或者我该如何解决这个问题?
詹金斯版本:2.249.2
虽然这在普通 Groovy 上有效,但使用以下语法 env[key]
在这里不起作用,因为管道 运行 在沙箱内,不允许此类访问.
你有两个选择:
1 - 使用 Groovy gstrings 并将 env[key]
更改为 env."${key}"
完全避免了沙箱错误。
2 - 如果您仍然喜欢这种访问方式,请在名为 'In-process Script Approval' 的 Jenkins 管理部分将该方法列入白名单。在这样做并再次 运行 管道之后,您应该不会再看到堆栈跟踪了。