简体   繁体   English

找不到Jenkins共享库资源

[英]Jenkins Shared Library resource not found

I have just started looking into a shared libarary with jenkins in order to combine a load of scripts and pipelines across multiple repos that are pretty much identical. 我刚刚开始研究与jenkins共享的库,以便将几乎相同的多个仓库中的脚本和管道负载组合在一起。

I have the shared lib loaded and working but when tryign to execute the scripts i the resources folder i keep geting not found errors: 我已经加载并运行了共享库,但是当尝试执行脚本时,我在资源文件夹中一直找不到错误:

../releaseTagging-EO2DMYOPJ6JGB6JT5Q2RSFJWJWWPALA7F25H7CQNYBEV4ITTEB6Q@tmp/build.sh: not found

I am creating a copy of the file using the following: 我正在使用以下方法创建文件的副本:

createTempLocation(String path) {
  String tmpDir = pwd tmp: true
  return tmpDir + File.separator + new File(path).getName()
}

and

copyGlobalLibraryScriptcall(String srcPath, String destPath = null) {
  destPath = destPath ?: createTempLocation(srcPath)
  writeFile file: destPath, text: libraryResource(srcPath)
  echo "copyGlobalLibraryScript: copied ${srcPath} to ${destPath}"
  sh "chmod +x ${destPath}"
  echo "added executable permissions to ${destPath}"
  return destPath
}

I am then calling the last function thusly: 然后,我这样调用了最后一个函数:

runBuild(Map config) {
    def script = copyGlobalLibraryScript('build.sh')
    sh script
}

(i realise i can collapse the above function in to one line) (我意识到我可以将上述功能折叠成一行)

This in turn then gets called via (trimed the whole file to relevent part): 然后依次通过调用(将整个文件修剪到相关部分):

pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    timestamps {
                        checkout scm

                        bbNotify( key: buildKey, name: BuildName) {
                            runBuild()
                        }

                        stash includes: '**', name: 'RelToSTAN'
                    }
                }
            }
}

This all fails with the error at the top of the question, however when sshing on to the build server i can find that file int he location specified. 这一切都会因问题顶部的错误而失败,但是,当切换到构建服务器时,我可以在指定位置找到该文件。

I dont understand why Jenkins cannot find it and execute it. 我不明白为什么詹金斯找不到它并执行它。

The issue will be the following: When using a java File object it'll always refer to some location on the Jenkins master. 问题将是:使用java File对象时,它将始终引用Jenkins主对象上的某个位置。 And of course it usually cannot run inside the sandbox. 当然,它通常无法在沙箱中运行。

On the other hand the readFile and writeFile methods always refer to some path on the build agent reserved by the node block where the call is encapsulated. 另一方面, readFilewriteFile方法始终引用构建代理的某个路径,该路径由封装了调用的node块保留。

Long story short: Do not use the File class. 长话短说:不要使用File类。 Unfortunately you'll need to create the temp path manually. 不幸的是,您需要手动创建临时路径。 But that shouldn't be too hard. 但这不应该太难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM