[英]How to use @Library in an imported groovy script in Jenkins declarative pipeline?
What I have is a following: 我所拥有的是以下内容:
vars
folder called deleteFile.groovy
, tried it - works. vars
文件夹中的一个名为deleteFile.groovy
脚本试过它 - 工作。 Library is called myOneLib
myOneLib
firstPipe.groovy
firstPipe.groovy
的管道脚本 @Library('myOneLib') _
def execute(String zCmakeListsPath){
stage('some kind of stage 2') {
echo "Hello from stage 1 with " + zCmakeListsPath
echo "var attempt ${env.mySrcDir}"
}
stage('second stage'){
echo "and one from stage 2"
echo "param was " + zCmakeListsPath
echo "var attempt ${env.myBuildDir}"
//call function from global lib
deleteFile 'for 3rd party global library now'
}
}
return this
caller.groovy
that is calling firstPipe.groovy
caller.groovy
的管道脚本,它调用firstPipe.groovy
pipeline {
agent any
environment {
myBuildDir = "thisShoulbBeBuild"
mySrcDir = "andHereIsSrc"
}
stages {
stage('first') {
steps {
script{
echo 'beggining with ' + myBuildDir
def rootDir = pwd()
echo 'rootDir is ' + rootDir
def example = load "${rootDir}/fullPipe/firstPipe.groovy"
example.execute("rasAlGhul")
}
}
}
}
}
Now, when I run the build like this, I get the following error: 现在,当我像这样运行构建时,我收到以下错误:
ERROR: Could not find any definition of libraries [myOneLib]
错误:找不到库的任何定义[myOneLib]
but when I simply move the line @Library('myOneLib') _
to the top of caller.groovy
everything works. 但是当我只是招行
@Library('myOneLib') _
至顶caller.groovy
一切正常。
So my question is how do use the @Library
in the imported/included script? 所以我的问题是如何在导入/包含的脚本中使用
@Library
? Or is there some other way to specify the global library? 或者是否有其他方法来指定全局库?
Few more notes: caller.groovy
and firstPipe.groovy
are in the same git repo, and if I eliminate usage of the global library, everything works fine. 几个注释:
caller.groovy
和firstPipe.groovy
在同一个git repo中,如果我不使用全局库,一切正常。 I'm using declarative pipeline and would like to continue to do so. 我正在使用声明性管道,并希望继续这样做。
For this use case, it will make more sense to use the library
step to dynamically load it at runtime. 对于此用例,使用
library
步骤在运行时动态加载它会更有意义。
In your firstPipe.groovy
you could do something like: 在您的
firstPipe.groovy
您可以执行以下操作:
final myOneLib = library('myOneLib')
def execute(String zCmakeListsPath){
stage('some kind of stage 2') {
echo "Hello from stage 1 with " + zCmakeListsPath
echo "var attempt ${env.mySrcDir}"
}
stage('second stage'){
echo "and one from stage 2"
echo "param was " + zCmakeListsPath
echo "var attempt ${env.myBuildDir}"
//call function from global lib
myOneLib.deleteFile 'for 3rd party global library now'
}
}
return this
See the Loading libraries dynamically section of the Extending with Shared Libraries documentation . 请参阅“ 使用共享库扩展”文档中的“ 动态加载库”部分 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.