我有一个Jenkins管道作业,其中包含以下代码:
import hudson.model.*
import hudson.util.*
import hudson.scm.*
import hudson.scm.SubversionChangeLogSet.LogEntry
stage 'Build'
node('master'){
svn 'http://mysvn/url'
def build = Thread.currentThread()?.executable
def changeSet= build.getChangeSet()
.
.
}
groovy.lang.MissingPropertyException: No such property: executable for class: java.lang.Thread
我不熟悉Thread.currentThread()?.executable
的语法
什么是'?'操作员意味着
我谷歌并发现 jenkins job-dsl插件,但没有找到任何关于此操作符的内容。
我还尝试了脚本控制台插件:http://localhost:8080/script 我也因为同样的原因而失败了。
Pipeline Plugin是否支持Jenkins DSL-JOB?我应该导入一些东西才能使它工作吗?
答案 0 :(得分:2)
Here是相关的票证和来自cloudbees的答案。从那里回来:
def changeLogSets = currentBuild.rawBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
echo "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}"
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
echo " ${file.editType.name} ${file.path}"
}
}
}
答案 1 :(得分:0)
这是一个常规的操作员,'?'是为了防止NullpointerExceptions。仅当第一个不为空时才执行以下操作。
Sandbox功能是为了防止某些调用,所以任何人都可以在没有管理员批准的情况下添加脚本,但非常有限......
答案 2 :(得分:0)
def build = Thread.currentThread()?.executable
首先,上述句子可以这样解释,
Thread.currentThread()
将获取当前正在运行的线程,在正常情况下,它将是Jenkins Executor
类的实例,此类中有一个属性,
/**
* {@link hudson.model.Queue.Executable} being executed right now, or null if the executor is idle.
*/
@GuardedBy("lock")
private Queue.Executable executable;
Jenkins AbstractBuild
实现此接口,这意味着您实际上将获得AbstractBuild
实例。
然而,这句话不适用于管道相关的工作,因为管道项目与旧的Jenkins工作相比具有不同的结构。它不会扩展AbstractBuild
类。
这就是您的脚本无法正常工作的原因。
关于你的要求,因为没有AbstrctBuild
类,所以实际上很多方法都不能使用,就像你使用的方法一样。
不知道是否有一种聪明的方法可以在管道工作中获取changset,或者您可能需要重新调整您的工作以适应管道插件。
BR,
添