简体   繁体   中英

Jenkins 2.0 Pipeline and Job DSL

I have a Jenkins Pipeline Job which has this code :

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

The code is with unchecked 'sandbox' (as it presented on the picture). 在此处输入图片说明 and I get this error :

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

I am not familiar with the syntax for Thread.currentThread()?.executable what does the '?' operator means.

I google it and found out about jenkins job-dsl plugin and didn't find anything about this operator.

I also tried the Script Console Plugin at : http://localhost:8080/script and I fail for the same reason.

Does Pipeline Plugin support the Jenkins DSL-JOB ? should I import something in order to make it work ?

Here is the related ticket and the answer from cloudbees.. Ping back from there:

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

It's a groovy operator, '?' is to prevent NullpointerExceptions. It does the following only if the first is not null.

The Sandbox feature is to prevent certain calls so anybody can add scripts without admin approval, but very limited...

def build = Thread.currentThread()?.executable

Firstly, the above sentence can be explained like this,

Thread.currentThread() will get the current running thread, in normal case, it will be a instance of Jenkins Executor class, there is a attribute inside this class,

    /**
 * {@link hudson.model.Queue.Executable} being executed right now, or null if the executor is idle.
 */
@GuardedBy("lock")
private Queue.Executable executable;

Jenkins AbstractBuild implement this interface, so it means you will actually get AbstractBuild instance back.

However, this sentence is not work for pipeline related jobs since pipe line project have different structure compared with old Jenkins jobs. It don't extend AbstractBuild class.

This is why your script is not working.

About your requirement, since there is no AbstrctBuild class, so a lot of methods are actually cannot be used, like the one you used.

No idea if there is a smart way to get the changset inside pipeline job, or maybe you need restructure your job to adapt pipeline plugin.

Br,

Tim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM