我可以将groovy脚本从相对目录导入Jenkinsfile吗?
发布时间:2020-12-14 16:25:59 所属栏目:大数据 来源:网络整理
导读:我有一个像这样结构的项目: // 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 # A
我有一个像这样结构的项目:
/ / 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了解这些扩展用例. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |