我有一个像这样结构的项目:
/ / Jenkinsfile / build_tools / / pipeline.groovy # Functions which define the pipeline / reporting.groovy # Other misc build reporting stuff / dostuff.sh # A shell script used by the pipeline / domorestuff.sh # Another pipeline supporting shell-script
是否可以在/ build_tools中导入groovy文件,以便我可以在Jenkinsfile中使用这两个文件中的函数?
理想情况下,我想要一个看起来像这样的(Jenocode)Jenkins文件:
from build_tools.pipeline import build_pipeline build_pipeline(project_name="my project",reporting_id=12345)
我坚持的一点是你如何在我的伪代码的#1行上编写一个等效的伪装导入语句.
PS.为什么我这样做:build_tools文件夹实际上是许多项目共享的git子模块.我试图让每个项目访问一组通用的构建工具,以阻止每个项目维护者重新发明这个轮子.
解决方法
加载共享groovy代码的最佳支持方式是
shared libraries.
如果您有这样的共享库:
simplest-jenkins-shared-library master % cat src/org/foo/Bar.groovy package org.foo; def awesomePrintingFunction() { println "hello world" }
将其推入源代码控制,在jenkins工作中甚至全局配置它(这是使用管道时通过Jenkins UI执行的唯一操作之一),如此屏幕截图所示:
然后使用它,例如,像这样:
pipeline { agent { label 'docker' } stages { stage('build') { steps { script { @Library('simplest-jenkins-shared-library') def bar = new org.foo.Bar() bar.awesomePrintingFunction() } } } } }
此构建的控制台日志输出当然包括:
hello world
还有很多其他方法可以编写共享库(比如使用类)并使用它们(比如定义变量,这样你就可以在Jenkinsfiles中以超级流畅的方式使用它们).您甚至可以将非groovy文件作为资源加载.查看the shared library docs了解这些扩展用例.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。