如何在Groovy类中访问管道DSL(而不是在Jenkinsfile中)? [英] How to access pipeline DSL in groovy classes(and not in Jenkinsfile)?
问题描述
我想在groovy类中访问流水线步骤,例如"bat"或"echo",但我并不真正了解'script'变量用法的工作方式.这是只有此类的很小的存储库,我在groovy test(spock测试)中称呼此类.有人可以启发我正确使用'script'变量以及调用的外观如何吗?
I want to access pipeline steps like 'bat' or 'echo' in my groovy classes but I am not really aware of how the 'script' variable usage works. This is very small repository with only this class and I am calling this class in groovy test(spock test). Can somebody please enlighten me on correct usage of 'script' variable and how the call should look like ?
SoftwareInstallation.groovy
SoftwareInstallation.groovy
class SoftwareInstallation implements Serializable {
Script script
def runInstallationJar(repoDir){
def exitValues=[]
def configFiles=['software1.xml', 'software2.xml']
configFiles.each {
def status = script.bat returnStatus: true, script: " java -jar ${repoDir}\\resources\\software.jar --inputxml=${repoDir}\\resources\\${it}"
script.echo "Return status : ${status}"
if (status){
log 'success'
}else{
log 'failure'
}
exitValues.add(status)
}
return exitValues
}
}
ps-我确实有一个Jenkinsfile,但其中只包含一个gradle调用来运行测试.
ps- I do have a Jenkinsfile but that just contains a gradle call to run tests.
推荐答案
文章中有一个有关使用共享库扩展.
您将在其中找到以下代码:
There you'll find following code:
package org.foo
class Utilities implements Serializable {
def steps
Utilities(steps) {this.steps = steps}
def mvn(args) {
steps.sh "${steps.tool 'Maven'}/bin/mvn -o ${args}"
}
}
呼叫者
@Library('utils') import org.foo.Utilities
def utils = new Utilities(this)
node {
utils.mvn 'clean package'
}
没有共享库
您是在库中还是在脚本中定义类都没有关系.方法将是相同的:
Without shared library
It doesn't really matter whether you defined the class inside of a library or in your script. The approach would be the same:
class Utilities implements Serializable {
def steps
Utilities(steps) {this.steps = steps}
def mvn(args) {
steps.sh "${steps.tool 'Maven'}/bin/mvn -o ${args}"
}
}
def utils = new Utilities(this)
node {
utils.mvn 'clean package'
}
这篇关于如何在Groovy类中访问管道DSL(而不是在Jenkinsfile中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!