2016-08-02 325 views
1

我有詹金斯管道作业,其具有这样的代码:詹金斯2.0管道和工作DSL

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() 
    . 
    . 
} 

该代码是与未经检查的“沙盒”(因为它呈现在图片)。 enter image description here 和我得到这个错误:“?”

groovy.lang.MissingPropertyException: No such property: executable for class: java.lang.Thread 

我不熟悉的语法Thread.currentThread()?.executable 什么是运营商手段。

我google了一下,找到约jenkins job-dsl插件并没有发现任何关于这个操作符。

我也试过脚本控制台插件在:http://localhost:8080/script 我失败出于同样的原因。

Pipeline Plugin是否支持Jenkins DSL-JOB?我应该导入一些东西以使其工作?

回答

0

这是一个时髦的运营商,'?'是为了防止NullpointerExceptions。它仅在第一个不为null时才执行以下操作。

沙箱功能是为了防止某些呼叫所以任何人都可以添加脚本,而无需管理员的批准,但非常有限......

0
def build = Thread.currentThread()?.executable 

首先,上面这句话可以这样解释,

Thread.currentThread()将获得当前正在运行的线程,在正常情况下,这将是詹金斯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实例。

但是,这句话不适用于管道相关工作,因为管道工程与旧詹金斯工作相比具有不同的结构。它不延伸AbstractBuild类。

这就是为什么你的脚本不工作。

关于您的要求,由于没有AbstrctBuild类,所以很多方法实际上无法使用,就像您使用的那样。

不知道是否有一个聪明的方式来获得流水线作业内的变化,或者你需要重新调整你的工作以适应管道插件。

BR,

+0

感谢您的解释。 Jenkins职位dsl是否可以在流水线上使用? Jenkins管道似乎依赖于这个模块,我可以使用这个API吗? –

+0

你是什么意思在管道中使用dsl作业?任何示例? – Tim

+0

'maven(“test -Dproject.name = $ {project}/$ {branchName}”) }' –

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}" 
     } 
    } 
} 
+0

我会试试它,并让你知道:)谢谢你的帮助 –