我正在将Jenkinsfile从脚本转换为声明性文件。脚本中有一个使用@nonCPS
批注的函数。批注似乎不起作用,因为当我运行该作业时,它抛出了NotSerializableException
,而我发现应该避免使用nonCPS。我相信注释不能以声明式语法运行。
script {
withCredentials(...) {
gitHub = new GitHubUtilities(steps, token)
}
...
selectorChoices = createReleasesChoiceList(gitHub, owner, repository, filter())
...
}
....
@NonCPS
def createReleasesChoiceList(gitHub, owner, repository, isPrerelease) {
def releases = gitHub.getReleases(owner, repository, isPrerelease)
def selectorChoices = []
def utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'H:mm:ss'Z'")
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
def estFormat = new SimpleDateFormat("MMMM dd, yyyy h:mm a")
estFormat.setTimeZone(TimeZone.getTimeZone("EST5EDT"))
releases.each {
selectorChoices.push("$it.tag_name-" + estFormat.format(utcFormat.parse(it.published_at)))
}
return selectorChoices.join("\r\n")
}
@NonCPS
def filter() {
return {
!it.prerelease && !it.body.contains("deployed to production")
}
}
抛出:
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.ClosureCallEnv.locals
....
Caused: java.io.NotSerializableException: java.io.StringWriter
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)...
有没有一种方法可以使用声明性语法中的注释,还是将方法内部移入脚本块的唯一方法? (我尝试用方法定义移动整个方法,但似乎在脚本块中似乎不允许定义方法)