更新时间:2023-12-05 15:18:46
根据 文档
外部库可以从 resources/目录加载附属文件使用 libraryResource 步骤.参数是一个相对路径名,类似于 Java 资源加载:
def request = libraryResource 'com/mycorp/pipeline/somelib/request.json'
文件以字符串形式加载,适合传递给某些API或使用 writeFile 保存到工作区.
建议使用独特的包结构,这样您就不会意外与另一个库冲突.
我认为以下方法可行
def functions = libraryResource 'com/mycorp/pipeline/somelib/functions.sh'writeFile 文件:'functions.sh',文本:函数sh "源函数.sh && foofoo"
We are using the Pipeline Shared Libraries plugin to factorize the code common to our different Jenkins pipelines.
From its documentation, it offers a resources
top-level folder for non-Groovy files. As we rely on different bash functions, we would like to host them in a separate .sh
file (thus they could also be used by other processes than Jenkins).
The same documentation tells us about using libraryResource
step to load those resource files. We can successfully call this method within our Groovy script, giving it our resource file name as argument (function.sh
). But from here, we were not able to find a way to invoke the foofoo
function defined in function.sh
from the same Groovy script.
sh "foofoo" #error: foofoo is not defined
We also tried to first source it like this:
sh "source function.sh && foofoo"
But it fails at the source
step, stating that function.sh
is not found.
What would be the correct procedure to invoke a bash function defined in function.sh
According to the documentation
External libraries may load adjunct files from a resources/ directory using the libraryResource step. The argument is a relative pathname, akin to Java resource loading:
def request = libraryResource 'com/mycorp/pipeline/somelib/request.json'
The file is loaded as a string, suitable for passing to certain APIs or saving to a workspace using writeFile.
It is advisable to use an unique package structure so you do not accidentally conflict with another library.
I assume the following will work
def functions = libraryResource 'com/mycorp/pipeline/somelib/functions.sh'
writeFile file: 'functions.sh', text: functions
sh "source function.sh && foofoo"