如何在共享 jenkins 库中引用 shell 脚本

How to refer to shell script inside shared jenkins library

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

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

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

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

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

如共享库插件 docs 中所述,库存储库具有非常特定的结构。 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

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

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

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