本文介绍了在Jenkins声明式管道中如何在导入的groovy脚本中使用@Library?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我拥有的是以下内容:What I have is a following: 按照这里。没有什么特别的, vars 文件夹中的一个脚本名为 deleteFile.groovy ,尝试过它 - 起作用。库称为 myOneLib 管道脚本称为 firstPipe.groovyglobal shared library created as described here. Nothing special, one script in vars folder called deleteFile.groovy, tried it - works. Library is called myOneLiba pipeline script called 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.groovya pipeline script called caller.groovy that is calling firstPipe.groovypipeline { 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:但是当我简单地移动 @Library ('myOneLib')_ 到 caller.groovy 的顶部都可以。but when I simply move the line @Library('myOneLib') _ to the top of caller.groovy everything works.所以我的问题是如何使用 @Library 在导入/包含脚本中?或者是否有其他方式来指定全局库?So my question is how do use the @Library in the imported/included script? Or is there some other way to specify the global library?几个注释: caller.groovy 和 firstPipe.groovy 位于同一个git仓库中,如果我消除了全局库的使用,那么一切正常。我正在使用声明式管道,并希望继续这样做。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. 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.在 firstPipe中。 groovy 你可以这样做:In your firstPipe.groovy you could do something like: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请参阅 动态加载库 的扩展部分共享库文档。 这篇关于在Jenkins声明式管道中如何在导入的groovy脚本中使用@Library?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 00:18
查看更多