简体   繁体   English

如何在Jenkins声明性管道中导入的groovy脚本中使用@Library?

[英]How to use @Library in an imported groovy script in Jenkins declarative pipeline?

What I have is a following: 我所拥有的是以下内容:

  1. global shared library created as described here . 作为全球描述共享库创建这里 Nothing special, one script in vars folder called deleteFile.groovy , tried it - works. 没有什么特别的, vars文件夹中的一个名为deleteFile.groovy脚本试过它 - 工作。 Library is called myOneLib 库名为myOneLib
  2. a pipeline script called 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
  1. a pipeline script called 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.groovyfirstPipe.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.

相关问题 Jenkins 声明式管道添加 groovy postbuild 脚本 - Jenkins declarative pipeline add groovy postbuild script 在jenkins声明式管道文件中重用groovy脚本 - Reusing groovy script in jenkins Declarative pipeline file 如何使用groovy在声明性jenkins管道中填充env变量 - How to use groovy to populate an env variable in a declarative jenkins pipeline 如何在 Jenkins 声明性管道中执行 Groovy 语句? - How to execute Groovy statements in a Jenkins declarative pipeline? 声明性Jenkins管道; 如何声明变量并在脚本或邮件通知中使用它? - Declarative Jenkins Pipeline; How to declare a variable and use it in script or mail notification? Jenkins声明式管道中的Groovy错误 - Groovy error in Jenkins declarative pipeline Jenkins 声明性管道,在从代理上运行 groovy 脚本 - Jenkins Declarative Pipeline, run groovy script on slave agent Jenkins 声明式管道:如何从“主动选择反应引用参数的 Groovy 脚本”调用函数? - Jenkins Declarative Pipeline: How to call functions from "Active choices reactive reference parameter's Groovy script"? 如何加载 Groovy 文件并通过 Jenkins 声明式管道脚本调用其中的方法 - How to load a Groovy File and call a method in it via Jenkins Declarative Pipeline Script 如何在声明式jenkins管道中编写presend脚本 - How to write presend script in declarative jenkins pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM