如何在Jenkinsfile中导入Groovy类?我尝试了几种方法,但都没有成功。
这是我想导入的类:
Thing.groovy
class Thing { void doStuff(){...} }以下是不起作用的事情:
Jenkinsfile-1
节点{加载 ./Thing.groovy def thing = new Thing()}Jenkinsfile-2
import Thing 节点{ def thing = new Thing()}Jenkinsfile -3
节点{评估(新文件(./thing.groovy)) $ b def thing = new Thing()}解决方案
您可以通过load命令返回类的新实例,并使用该对象调用doStuff。
所以,你可以在Thing.groovy中找到这个
class Thing { def doStuff(){returnHI} } return new Thing();你可以在你的dsl脚本中找到它:
节点{ def thing = load'Thing.groovy' echo thing.doStuff()}这应该在控制台输出中打印HI。
满足您的要求?
How do I import a Groovy class within a Jenkinsfile? I've tried several approaches but none have worked.
This is the class I want to import:
Thing.groovy
class Thing { void doStuff() { ... } }These are things that don't work:
Jenkinsfile-1
node { load "./Thing.groovy" def thing = new Thing() }Jenkinsfile-2
import Thing node { def thing = new Thing() }Jenkinsfile-3
node { evaluate(new File("./Thing.groovy")) def thing = new Thing() }解决方案
You can return a new instance of the class via the load command and use the object to call "doStuff"
So, you would have this in "Thing.groovy"
class Thing { def doStuff() { return "HI" } } return new Thing();And you would have this in your dsl script:
node { def thing = load 'Thing.groovy' echo thing.doStuff() }Which should print "HI" to the console output.
Would this satisfy your requirements?
更多推荐
如何将Groovy类导入到Jenkinfile中?
发布评论