我可以将 groovy 脚本从相对目录导入 Jenkinsfile 吗? [英] Can I import a groovy script from a relative directory into a Jenkinsfile?
问题描述
我有一个这样结构的项目:
<预><代码>//詹金斯档案/构建工具//pipeline.groovy # 定义管道的函数/reporting.groovy # 其他杂项构建报告内容/dostuff.sh #管道使用的shell脚本/domorestuff.sh # 另一个支持 shell-script 的管道是否可以在/build_tools 中导入 groovy 文件,以便我可以使用 Jenkinsfile 中这两个文件中的函数?
理想情况下,我想要一个看起来像这样的 Jenkins 文件(伪代码):
from build_tools.pipeline import build_pipelinebuild_pipeline(project_name="我的项目",reporting_id=12345)
我遇到的问题是你如何在我的伪代码的第 1 行编写一个与假装 import 语句等效的工作.
附注.为什么我这样做: build_tools 文件夹实际上是许多项目共享的 git 子模块.我试图让每个项目访问一组通用的构建工具,以阻止每个项目维护者重新发明这个轮子.
加载共享 groovy 代码的最佳支持方式是通过
然后使用它,例如,像这样:
管道{代理{标签码头工人"}阶段{阶段('构建'){脚步 {脚本 {@Library('simplest-jenkins-shared-library')def bar = new org.foo.Bar()bar.awesomePrintingFunction()}}}}}
此构建的控制台日志输出当然包括:
你好世界
还有很多其他方法可以编写共享库(例如使用类)和使用它们(例如定义 vars,以便您可以以非常巧妙的方式在 Jenkinsfiles 中使用它们).您甚至可以将非 groovy 文件加载为资源.查看共享库文档,了解这些扩展用例.
I've got a project structured like this:
/
/ 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
Is it possible to import the groovy files in /build_tools so that I can use functions inside those 2 files in my Jenkinsfile?
Ideally, I'd like to have a Jenkins file that looks something like this (pseudocode):
from build_tools.pipeline import build_pipeline
build_pipeline(project_name="my project", reporting_id=12345)
The bit I'm stuck on is how you write a working equivalent of that pretend import statement on line #1 of my pseudocode.
PS. Why I'm doing this: The build_tools folder is actually a git submodule shared by many projects. I'm trying to give each project access to a common set of build tooling to stop each project maintainer from reinventing this wheel.
The best-supported way to load shared groovy code is through shared libraries.
If you have a shared library like this:
simplest-jenkins-shared-library master % cat src/org/foo/Bar.groovy
package org.foo;
def awesomePrintingFunction() {
println "hello world"
}
Shove it into source control, configure it in your jenkins job or even globally (this is one of the only things you do through the Jenkins UI when using pipeline), like in this screenshot:
and then use it, for example, like this:
pipeline {
agent { label 'docker' }
stages {
stage('build') {
steps {
script {
@Library('simplest-jenkins-shared-library')
def bar = new org.foo.Bar()
bar.awesomePrintingFunction()
}
}
}
}
}
Output from the console log for this build would of course include:
hello world
There are lots of other ways to write shared libraries (like using classes) and to use them (like defining vars so you can use them in Jenkinsfiles in super-slick ways). You can even load non-groovy files as resources. Check out the shared library docs for these extended use-cases.
这篇关于我可以将 groovy 脚本从相对目录导入 Jenkinsfile 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!