如何将Groovy类导入到Jenkinfile中? [英] How do I import a Groovy class into a Jenkinfile?

查看:332
本文介绍了如何将Groovy类导入到Jenkinfile中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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