Jenkinsfile方法被隐式执行

时间:2017-09-07 22:26:47

标签: jenkins groovy jenkins-pipeline

有一种奇怪的Jenkinsfile模式here

def call(body) {
  // evaluate the body block, and collect configuration into the object
  def config = [:]
  body.resolveStrategy = Closure.DELEGATE_FIRST
  body.delegate = config
  body()
  // ...
}

据我所知,call函数没有在程序的其他地方直接调用。 Groovy如何执行它?

1 个答案:

答案 0 :(得分:1)

在groovy中有一个叫做隐式调用的概念,看看这个例子:

from cassandra.util import OrderedMapSerializedKey

def pandas_factory(colnames, rows):

    # Convert tuple items of 'rows' into list (elements of tuples cannot be replaced)
    rows = [list(i) for i in rows]

    # Convert only 'OrderedMapSerializedKey' type list elements into dict
    for idx_row, i_row in enumerate(rows):

        for idx_value, i_value in enumerate(i_row):

            if type(i_value) is OrderedMapSerializedKey:

                rows[idx_row][idx_value] = dict(rows[idx_row][idx_value])

    return pd.DataFrame(rows, columns=colnames)

您提供的class C { def call(whatever) { println whatever } } def c = new C() c(1) 中的方法以完全相同的方式调用。另见here