如何将Groovy类导入到Jenkinfile中?

编程入门 行业动态 更新时间:2024-10-25 12:19:42
本文介绍了如何将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中?

本文发布于:2023-08-02 15:25:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   导入到   Groovy   Jenkinfile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!