我所拥有的是以下内容:描述here创建如何在Jenkins声明式管道中的导入的groovy脚本中使用@Library?
- 全局共享库。没什么特别的,
vars
文件夹中的一个脚本叫做deleteFile.groovy
,试过了 - 工作。图书馆被称为myOneLib
- 称为
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
管道脚本正在调用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")
}
}
}
}
}
现在,当我像这样运行构建时,出现以下错误:
ERROR: Could not find any definition of libraries [myOneLib]
但是,当我只是将@Library('myOneLib') _
行移动到caller.groovy
的顶部一切正常。
所以我的问题是如何在导入/包含脚本中使用@Library
?还是有其他方式来指定全局库?
更多注意事项:caller.groovy
和firstPipe.groovy
在同一个git仓库中,如果我消除了全局库的使用情况,一切正常。我正在使用声明式管道,并希望继续这样做。
使用'library'步骤动态加载导入的脚本以处理Jenkins Groovy运行时如何工作的一些怪异可能更容易。 – mkobit
@mkobit非常感谢提示,我不能相信我错过了https://jenkins.io/doc/book/pipeline/shared-libraries/#loading-libraries-dynamically您是否会推荐您的评论来回答?或者我的问题太简单明了了,我应该删除它? :) – cantSleepNow
我会添加它作为答案。 – mkobit