Jenkins管道,对象上的@Lazy属性在第一次调用时为NULL
@Lazy在Jenkins管道内的第一次调用中似乎没有返回值。你知道为什么吗 代码: 运行管道时,我得到以下结果:Jenkins管道,对象上的@Lazy属性在第一次调用时为NULL,jenkins,groovy,jenkins-pipeline,jenkins-groovy,Jenkins,Groovy,Jenkins Pipeline,Jenkins Groovy,@Lazy在Jenkins管道内的第一次调用中似乎没有返回值。你知道为什么吗 代码: 运行管道时,我得到以下结果: 调用作者 [管道]上海 +git-rev解析头 [管道]上海 +git--无寻呼机显示-s--格式=%an 9242efd51b83b4202863a04ac0b3c45c256a3948 [管道]回声 作者是: [管道]回声 Author1:null [管道]回声 作者2: 您可以清楚地看到定义了a。回来了。但是jenkinstatus.author直到第二次调用才真正得到应用
调用作者
[管道]上海
+git-rev解析头
[管道]上海
+git--无寻呼机显示-s--格式=%an 9242efd51b83b4202863a04ac0b3c45c256a3948
[管道]回声
作者是:
[管道]回声
Author1:null
[管道]回声
作者2:
您可以清楚地看到定义了
a
。回来了。但是jenkinstatus.author直到第二次调用才真正得到应用。Jenkins CPS转换使用Groovy注释做了一些有趣的事情。我从未能够让@Lazy
处理共享库类中的字段@Singleton
在整个运行时只有一个类时有效,但在添加第二个类时失败@Delegate
根本不起作用
FWIW,这是(我)对一个非常类似问题的公认答案: 这是一个众所周知的詹金斯问题: 该项目自2017年8月开始开放。看起来它不会很快被修复: 不确定是否有任何特定的地方详细记录了Groovy语言支持(或缺少),但无论如何,我不希望解决这个问题或类似的问题。未来的重点是允许外部流程执行,而不是在CPS引擎上浪费更多的时间,除非出现安全漏洞或严重倒退
class JenkinsStatus implements Serializable {
def pipeline
@Lazy String author = {
this.pipeline.echo "Call to Author"
def commit = this.pipeline.sh(returnStdout: true, script: 'git rev-parse HEAD')
def a = this.pipeline.sh(returnStdout: true, script: "git --no-pager show -s --format='%an' ${commit}").trim()
this.pipeline.echo("inside Author is: ${a}")
a
}()
}
pipeline {
agent any
stages {
stage( "Checkout repo") {
steps {
// SCM checkout() here.
}
}
}
post {
always {
script {
JenkinsStatus jstatus = [
pipeline: this
]
echo "Author1: ${jstatus.author}"
echo "Author2: ${jstatus.author}"
}
}
}
}