bash 由于创建了@tmp 路径,Jenkins 管道作业找不到脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41246161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:33:44  来源:igfitidea点击:

Jenkins Pipeline job can't find script due to @tmp path being created

gitbashjenkinsgroovy

提问by Alex

I am writing a pipeline job that will call another script to execute. The Jenkinsfile and script exist in the same directory and yet the job fails to find the script to run.

我正在编写一个管道作业,它将调用另一个脚本来执行。Jenkinsfile 和脚本存在于同一目录中,但作业无法找到要运行的脚本。

This is the relevant bit of script;

这是脚本的相关部分;

stage ('Update') {
    try {
        dir('jenkins/pipeline/update-jenkins-plugins-ppln') {
            sh 'ls -l'
            sh 'update-plugins.sh'
        }
}

which returns the following error;

返回以下错误;

[update-jenkins-plugins-ppln] Running shell script
+ ls -l
total 8
-rw-r--r-- 1 jenkins jenkins 2441 Dec 20 09:34 Jenkinsfile
-rwxr-xr-x 1 jenkins jenkins  506 Dec 19 14:06 update-plugins.sh
[Pipeline] sh
[update-jenkins-plugins-ppln] Running shell script
+ update-plugins.sh
/var/lib/jenkins/workspace/update-jenkins-plugins-ppln/jenkins/pipeline/update-jenkins-plugins-ppln@tmp/durable-11cefdd0/script.sh: 2: /var/lib/jenkins/workspace/update-jenkins-plugins-ppln/jenkins/pipeline/update-jenkins-plugins-ppln@tmp/durable-11cefdd0/script.sh: update-plugins.sh: not found

As you can see, the pathing I'm using is correct because according to the lsthe file I need update-plugins.shis in the directory I've pathed to. For some reason though, when actually searching for the script Jenkins is adding @tmp/durable-8d48734f/script.shonto the path.

正如您所看到的,我使用的路径是正确的,因为根据ls我需要的文件update-plugins.sh在我路径的目录中。但出于某种原因,在实际搜索脚本时 Jenkins 正在添加@tmp/durable-8d48734f/script.sh到路径中。

Various troubleshooting:

各种故障排除:

  • I read that you have to checkout the branch again even if you're already checking it out to get the Jenkinsfile, so I am.
  • I have ssh'd into the Jenkins box to check and yes, the script is there.
  • 我读到你必须再次签出分支,即使你已经签出它来获取 Jenkinsfile,所以我是。
  • 我已经 ssh 进入 Jenkins 框进行检查,是的,脚本在那里。

Why is Jenkins adding the @tmp bit, and is there a way to prevent this behavior?

为什么詹金斯添加@tmp 位,有没有办法防止这种行为?

回答by izzekil

I guess your pwd is not in PATH so you have to call it like this: sh './update-plugins.sh'

我猜你的密码不在 PATH 中,所以你必须这样称呼它: sh './update-plugins.sh'

回答by grubers

Have you tried using the jenkins workspace environment variable WORKSPACE(absolute path of the workspace)? With that your line would look something like this:

您是否尝试过使用 jenkins 工作区环境变量WORKSPACE(工作区的绝对路径)?有了这个,你的线看起来像这样:

sh '${WORKSPACE}/jenkins/pipeline/update-jenkins-plugins-ppln/update-plugins.sh'

回答by knack

I was having the same issue. I think @Oren technically answered your question about whyJenkins creates this tmpspace, but I can share some info about how I solved it.

我遇到了同样的问题。我认为@Oren 从技术上回答了您关于Jenkins为什么创建这个tmp空间的问题,但我可以分享一些关于我如何解决它的信息。

Basically, my Jenkins host symlinks bin/shto dash; not bash. So, using a POSIX-compliant shell script solved the issue for me.

基本上,我的 Jenkins 将符号链接托管bin/shdash; 不是bash。因此,使用符合 POSIX 标准的 shell 脚本为我解决了这个问题。

For example, I was trying to use shopt -s extglobto do some pattern matching:

例如,我试图用shopt -s extglob做一些模式匹配:

stage {
    def shellCommand = $/ "rm -rf ! (_data|_includes|_plugins|Gemfile|_config.yml|page-builder)"/$
    sh(returnStdout: true, script: shellCommand).trim()
}

Since dashdoesn't support extglob, replacing that with a POSIX-compliant findcommand worked:

由于dash不支持extglob,用符合 POSIX 的find命令替换它起作用:

stage {
    sh('find . -regextype posix-extended -not -regex ".*includes.*|.*data.*|.*plugins.*|.*config.yml|.*Gemfile.*"')
} 

回答by Alexander Samoylov

I am guessing this particular case has nothing to do with Jenkins pipelines, since it can be reproduced outside the Jenkins in the console. Once I got this issue due to DOS line-endingsin my script which I ran in a pipeline's "sh()". Look at this example:

我猜这个特殊情况与 Jenkins 管道无关,因为它可以在 Jenkins 之外的控制台中复制。一旦我由于我在管道的“sh()”中运行的脚本中的DOS 行结尾而遇到此问题。看这个例子:

$ cat -v dos_formatted.sh
#! /bin/sh^M
pwd^M

$ cat script.sh
./dos_formatted.sh

$ sh -xe script.sh
+ ./dos_formatted.sh
script.sh: 1: script.sh: ./dos_formatted.sh: not found

It illustrates well that the "not found" message is misleading. The script is on the right place and the permissions are sufficient, but when you run it from another script it fails with such an error message.

它很好地说明了“未找到”消息具有误导性。该脚本位于正确的位置并且权限足够,但是当您从另一个脚本运行它时,它会失败并显示此类错误消息。

The code of the Durable Task Plugin (https://github.com/jenkinsci/durable-task-plugin/blob/master/src/main/java/org/jenkinsci/plugins/durabletask/BourneShellScript.java) shows that the plugin runs the auto-generated script.sh as "sh -xe ...", therefore it's exactly our case.

Durable Task Plugin ( https://github.com/jenkinsci/durable-task-plugin/blob/master/src/main/java/org/jenkinsci/plugins/durabletask/BourneShellScript.java)的代码显示该插件将自动生成的 script.sh 作为“sh -xe ...”运行,因此这正是我们的情况。

The above described situation can happen if you "git clone" some (probably 3rd party) Git project and don't make sure that is was cloned with Unix-style LF.

如果您“git clone”某些(可能是第 3 方的)Git 项目并且不确保它是使用 Unix 风格的 LF 克隆的,则可能会发生上述情况。

回答by Nin

I was facing a similar issue where though the shell script is present on path /var/lib/jenkins/workspace/New_commit_test/Fibonacci.sh, Jenkins build was getting failed with error message : "/tmp/jenkins6688065235543884785.sh: 3: /tmp/jenkins6688065235543884785.sh: /var/lib/jenkins/workspace/New_commit_test/Fibonacci.sh: not found"

我遇到了一个类似的问题,尽管 shell 脚本存在于路径 /var/lib/jenkins/workspace/New_commit_test/Fibonacci.sh 上,但 Jenkins 构建失败并显示错误消息:“/tmp/jenkins6688065235543884785.sh: 3:/ tmp/jenkins6688065235543884785.sh:/var/lib/jenkins/workspace/New_commit_test/Fibonacci.sh:未找到”

so i changed my earlier command from : ${WORKSPACE}/Fibonacci.sh > New_commit_test.txt

所以我改变了我之前的命令: ${WORKSPACE}/Fibonacci.sh > New_commit_test.txt

to : (since i was using bash script here ) bash${WORKSPACE}/Fibonacci.sh > New_commit_test.txt

到:(因为我在这里使用 bash 脚本) bash${WORKSPACE}/Fibonacci.sh > New_commit_test.txt

which sorted issue here for me

这对我来说是哪个排序的问题

回答by Oren

The @tmp folder is there for jenkins job and stages statistics (duration of stages, etc), you can delete it if you want to be sure. I assume that your kind of issue is related for wrong path, double check it.

@tmp 文件夹用于 jenkins 作业和阶段统计信息(阶段持续时间等),如果您想确定,可以将其删除。我认为您的问题与错误的路径有关,请仔细检查。