【问题标题】:How to refer to shell script inside shared jenkins library如何在共享 jenkins 库中引用 shell 脚本
【发布时间】:2021-05-18 20:37:18
【问题描述】:

我有一个 Jenkinsfile,它使用共享的 Jenkins 库并调用下面定义的 groovy 文件中的方法。

shared-jenkins-lib
    |
    |-----------vars
    |            |-------pipeline_script.groovy
    |
    |-----------scripts
                 |-------test_script.groovy

这是库的结构,pipeline_script.groovy 有一个方法 test 从 jenkinsfile 调用。

def test(){
    dockerArgs = "--entrypoint /bin/bash"
    dockerCommand = "`../scripts/test_script.sh`"
    dockerOut = sh (script: "docker run ${dockerArgs} ${image} ${dockerCommand}", returnStatus: true)
}

我参考了test_script.sh,但似乎在该位置找不到文件。 我在 docker 中运行 jenkins。引用脚本的正确方法是什么

【问题讨论】:

    标签: docker jenkins jenkins-pipeline


    【解决方案1】:

    正如共享库插件的docs 中所述,库 repo 具有非常特定的结构。 repo 的根目录可以有以下 3 个文件夹:

    +- src                     # Groovy source files
    |   +- org
    |       +- foo
    |           +- Bar.groovy  # for org.foo.Bar class
    +- vars
    |   +- foo.groovy          # for global 'foo' variable
    |   +- foo.txt             # help for 'foo' variable
    +- resources               # resource files (external libraries only)
    |   +- org
    |       +- foo
    |           +- bar.json    # static helper data for org.foo.Bar
    

    如果你想从你的库方法中引用一个帮助代码/数据,你应该把它放在resources 目录中,然后你可以使用libraryResource 步骤检索它,也在文档中描述。

    【讨论】:

      【解决方案2】:

      您需要将工作流脚本对象传入test()

        def test(workFlowScript wfs) {
           ...
           wfs.sh ''
           wfs.echo ''
           wfs.withCredentials()
        }
      
      // your Jenkisnfile which call test()
      test(this) // this is the instance of workFlowScript
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-23
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多