如何重构常见的Jenkins JobDSL代码? [英] How to refactor common Jenkins JobDSL code?

查看:301
本文介绍了如何重构常见的Jenkins JobDSL代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的JobDSL规范:

I have the following JobDSL spec:

job {
  steps {
    gradle('generateLock saveLock', '-PdependencyLock.includeTransitives=true', true) { node ->
      node / wrapperScript('${NEBULA_HOME}/gradlew')
    }
    gradle('check', '', true) { node ->
      node / wrapperScript('${NEBULA_HOME}/gradlew')
    }
  }
}

我想将通用代码重构成一个函数:

I'd like to refactor the common code, say, into a function:

def gradlew(String tasks, String options) {
  gradle(tasks, options, true) { node ->
    node / wrapperScript('${NEBULA_HOME}/gradlew')
  }
}

但是 gradle 函数在 gradlew 函数中是不可见的。什么是正确的做法?

But the gradle function isn't visible from within the gradlew function. What's the right way to do this?

推荐答案

大括号形成一个Groovy闭包。每个闭包都有一个方法调用指向的委托对象。代表可以通过代表属性进行访问。您可以将该委托传递给帮助函数以访问它的方法。

The curly brackets form a Groovy closure. Each closure has a delegate object to which method calls are directed. And the delegate can be accessed via the delegate property. You can pass that delegate to the helper function to get access to it's methods.

def gradlew(def context, String tasks, String options = '') {
  context.gradle(tasks, options, true) { node ->
    node / wrapperScript('${NEBULA_HOME}/gradlew')
  }
}
job {
  steps {
    gradlew(delegate, 'generateLock saveLock', '-PdependencyLock.includeTransitives=true')
    gradlew(delegate, 'check')
  }
}

这篇关于如何重构常见的Jenkins JobDSL代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆