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

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

问题描述

如何在 Jenkinsfile 中导入 Groovy 类?我尝试了几种方法,但都没有奏效.

How do I import a Groovy class within a Jenkinsfile? I've tried several approaches but none have worked.

这是我要导入的类:

Thing.groovy

class Thing {
    void doStuff() { ... }
}

这些是行不通的:

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()
}

推荐答案

可以通过load命令返回一个新的类实例,并使用对象调用doStuff"

You can return a new instance of the class via the load command and use the object to call "doStuff"

所以,你会在Thing.groovy"中拥有这个

So, you would have this in "Thing.groovy"

class Thing {
   def doStuff() { return "HI" }
}

return new Thing();

你会在你的 dsl 脚本中有这个:

And you would have this in your dsl script:

node {
   def thing = load 'Thing.groovy'
   echo thing.doStuff()
}

应该将HI"打印到控制台输出.

Which should print "HI" to the console output.

这能满足您的要求吗?

这篇关于如何将 Groovy 类导入 Jenkinsfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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