gpt4 book ai didi

java - Groovy StreamingTemplateEngine 使用 withCredentials 函数给出错误

转载 作者:行者123 更新时间:2023-11-30 05:21:25 28 4
gpt4 key购买 nike

我创建了调用以下函数的 Jenkins 管道...它使用 StreamingTemplateEngine 对象创建了一个 Template 变量...但它给出了一个错误

def call() {
def name = "abc"
def binding = [
firstname: "Grace",
lastname: "Hopper",
]

def text = 'Dear <% out.print firstname %> ${lastname}'
def template = new groovy.text.StreamingTemplateEngine().createTemplate(text)

print template.make(binding)

def response = template.make(binding)

withCredentials([string(credentialsId: 'Token', variable: 'TOKEN')]) {
println("test")
println(response)
}
}

上面的代码第一次成功打印响应,但最后给出了以下错误

an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@3678d955
in field com.cloudbees.groovy.cps.impl.CpsClosureDef.capture
in object com.cloudbees.groovy.cps.impl.CpsClosureDef@23a3d63c
in field com.cloudbees.groovy.cps.impl.CpsClosure.def
in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@6d8ad313
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@76f2b368
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@76f2b368
Caused: java.io.NotSerializableException: groovy.text.StreamingTemplateEngine$StreamingTemplate

如果我删除 withCredentials 函数,那么它就可以正常工作。

最佳答案

Jenkins Pipeline 以连续传递风格 (groovy-cps) 运行 Groovy 代码。它期望每个局部变量都是可序列化的,因此它可以安全地序列化每个计算并在 Jenkins 重新启动等情况下恢复它。

如果使用不可序列化对象,Jenkins Pipeline 提供了 @NonCPS 注释,可以与方法一起使用来标记这部分代码不可序列化,并且不应转换为CPS代码解释。

"Pipeline scripts may mark designated methods with the annotation @NonCPS. These are then compiled normally (except for sandbox security checks), and so behave much like “binary” methods from the Java Platform, Groovy runtime, or Jenkins core or plugin code. @NonCPS methods may safely use non-Serializable objects as local variables, though they should not accept nonserializable parameters or return or store nonserializable values. You may not call regular (CPS-transformed) methods, or Pipeline steps, from a @NonCPS method, so they are best used for performing some calculations before passing a summary back to the main script. Note in particular that @Overrides of methods defined in binary classes, such as Object.toString(), should in general be marked @NonCPS since it will commonly be binary code calling them."


Source: https://github.com/jenkinsci/workflow-cps-plugin#technical-design

您可以将 StreamingTemplateEngine 部分提取到单独的 @NonCPS 方法,该方法需要模板作为文本以及绑定(bind)映射。像这样的东西应该可以安全使用:

import com.cloudbees.groovy.cps.NonCPS

def call() {
def name = "abc"
def binding = [
firstname: "Grace",
lastname: "Hopper",
]

def text = 'Dear <% out.print firstname %> ${lastname}'

def response = parseTemplate(text, binding)

withCredentials([string(credentialsId: 'Token', variable: 'TOKEN')]) {
println(response)
}
}

@NonCPS
String parseTemplate(String text, Map bindings) {
new groovy.text.StreamingTemplateEngine().createTemplate(text)
.make(bindings)
.toString()
}

关于java - Groovy StreamingTemplateEngine 使用 withCredentials 函数给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59526894/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com