bash Jenkins 管道 sh 在 Windows 上因“无法运行程序 nohup”而失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45140614/
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
Jenkins pipeline sh fail with "cannot run program nohup" on windows
提问by flopic
I have windows 10 and I want to execute the sh command in the Jenkinsfile from Jenkins pipeline using bash for Ubuntu for windows, but it doesn't work
我有 Windows 10,我想在 Jenkins 管道中使用 bash for Ubuntu for windows 执行 Jenkinsfile 中的 sh 命令,但它不起作用
I have the following stage in my Jenkins pipeline :
我的 Jenkins 管道中有以下阶段:
stage('sh how to') {
steps {
sh 'ls -l'
}
}
The error message is :
错误信息是:
[C:\Program Files (x86)\Jenkins\workspace\pipelineascode] Running shell script Cannot run program "nohup" (in directory "C:\Program Files (x86)\Jenkins\workspace\pipelineascode"): CreateProcess error=2, Le fichier spécifié est introuvable
[C:\Program Files (x86)\Jenkins\workspace\pipelineascode] 运行 shell 脚本无法运行程序“nohup”(在目录“C:\Program Files (x86)\Jenkins\workspace\pipelineascode”中):CreateProcess error=2 , Le fichier spécifié est introuvable
I tried changing Jenkins parameter->shell executable with
我尝试更改 Jenkins 参数-> shell 可执行文件
C:\Windows\System32\bash.exe
C:\Windows\System32\bash.exe
but same error...
但同样的错误...
how to run sh script using windows 10's bash?
如何使用 Windows 10 的 bash 运行 sh 脚本?
回答by John-Philip
From a very quick search, it looks like your error is related to the following issue : JENKINS-33708
从非常快速的搜索来看,您的错误似乎与以下问题有关:JENKINS-33708
The main cause looks like the sh
step is not supported on the Windows. You may use bat
or install Cygwin
for instance.
主要原因似乎sh
是 Windows 不支持该步骤。例如,您可以使用bat
或安装Cygwin
。
Nevertheless two solutions were proposed in the previous link, suggesting you to do the following steps :
尽管如此,在上一个链接中提出了两种解决方案,建议您执行以下步骤:
- Install git-bash
- Ensure the Git\bin folder (i.e.:
C:\Program Files\Git\bin
) is in the global search path, in order for Jenkins to find sh.exe - Make nohup available for Jenkins, doing the following in git-bash (adapt your paths accordingly) :
- mklink "C:\Program Files\Git\bin\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
- mklink "C:\Program Files\Git\bin\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
- mklink "C:\Program Files\Git\bin\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
- mklink "C:\Program Files\Git\bin\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"
- 安装git-bash
- 确保 Git\bin 文件夹(即
C:\Program Files\Git\bin
:)在全局搜索路径中,以便 Jenkins 找到 sh.exe - 为 Jenkins 提供 nohup,在 git-bash 中执行以下操作(相应地调整您的路径):
- mklink "C:\Program Files\Git\ bin\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
- mklink "C:\Program Files\Git\ bin\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
- mklink "C:\Program Files\Git\ bin\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
- mklink "C:\Program Files\Git\ bin\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"
Depending on your installation you may have to use these paths :
根据您的安装,您可能必须使用这些路径:
- mklink "C:\Program Files\Git\cmd\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
- mklink "C:\Program Files\Git\cmd\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
- mklink "C:\Program Files\Git\cmd\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
- mklink "C:\Program Files\Git\cmd\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"
- mklink "C:\Program Files\Git\ cmd\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
- mklink "C:\Program Files\Git\ cmd\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
- mklink "C:\Program Files\Git\ cmd\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
- mklink "C:\Program Files\Git\ cmd\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"
回答by sonicwave
With Git for Windows, I had to add C:\Program Files\Git\bin
to the PATH
environment variable of the slave node in Jenkins (to get access to sh
), then add C:\Program Files\Git\usr\bin
to the PATH
locally on the Windows slave too (to get access to nohup
).
对于 Windows 的 Git,我必须添加C:\Program Files\Git\bin
到PATH
Jenkins 中从节点的环境变量(以访问sh
),然后也添加C:\Program Files\Git\usr\bin
到PATH
Windows 从节点上的本地变量(以访问nohup
)。
回答by thSoft
With Git for Windows 2.16.2, I was able to add C:\Program Files\Git\usr\bin to the PATH (rather than C:\Program Files\Git\bin) and consequently my sh commands work in both FreeStyle and Pipeline builds. No mklink was necessary. (Source)
使用 Git for Windows 2.16.2,我能够将 C:\Program Files\Git\usr\bin 添加到 PATH(而不是 C:\Program Files\Git\bin),因此我的 sh 命令在 FreeStyle 和管道构建。不需要 mklink。(来源)
回答by Mike
Switching sh to bat worked for me - I am running Jenkins on Windows. But only after I had resolved an issue caused by the fact I had not configured my tools (maven and the JDK) correctly in Jenkins either.
将 sh 切换为 bat 对我有用 - 我在 Windows 上运行 Jenkins。但只有在我解决了由于我没有在 Jenkins 中正确配置我的工具(maven 和 JDK)而导致的问题之后。
回答by Raghwendra Sonu
If you are executing on Windows, just change sh to bat. it will work as expected. Example:
如果您在 Windows 上执行,只需将 sh 更改为 bat。它会按预期工作。例子:
pipeline { agent any stages { stage ('Compile Stage') {
管道{代理任何阶段{阶段('编译阶段'){
steps {
withMaven(maven : 'apache-maven-3.6.1') {
bat'mvn clean compile'
}
}
}
} }
} }
回答by IRayTrace
My observation is that the agent seems to be trying to run nohup in the context where the agent.jar is run, not in the container. It didn't matter what I put in the container, the error message was the same. By putting nohup and sh in the PATH where the jenkins agent is running, I see a change in behavior.
我的观察是代理似乎试图在运行 agent.jar 的上下文中运行 nohup,而不是在容器中。我在容器中放了什么并不重要,错误消息是一样的。通过将 nohup 和 sh 放在 jenkins 代理运行的 PATH 中,我看到行为发生了变化。
git config core.sparsecheckout # timeout=10 git checkout -f c64c7bf905b6a4f5a8f85eb23bbd108f4c805386 sh: /home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins-log.txt: No such file or directory sh: /home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins-result.txt.tmp: No such file or directory mv: cannot stat '/home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins-result.txt.tmp': No such file or directory
git config core.sparsecheckout # timeout=10 git checkout -f c64c7bf905b6a4f5a8f85eb23bbd108f4c805386 sh:/home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jtxt 目录:/home/jenkins/durable-9fedc317/jtxt /workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins-result.txt.tmp: 没有这样的文件或目录 mv: cannot stat '/home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins- result.txt.tmp': 没有那个文件或目录
I am seeing a folder /home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317 which contains a file "script.sh" with the contents "docker inspect -f . repositoryname:tagname"
我看到一个文件夹 /home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317,其中包含一个文件“script.sh”,内容为“docker inspect -f .repositoryname:tagname”
When this docker command is run manually on the command line, it always produces a single line of output consisting of a single period character. I have no doubt this is not what the jenkins system is looking for.
当这个 docker 命令在命令行上手动运行时,它总是产生一行由单个句点字符组成的输出。我毫不怀疑这不是詹金斯系统正在寻找的。
回答by Sweavo
So you want the job running under WSL. If you want all jobs running under WSL have you considered installing Jenkins under WSL? Then everything is already in GNU land and you don't have to bridge the envrionment/culture of windows to GNU from within your Jenkins configuration.
所以你希望作业在 WSL 下运行。如果您希望所有作业都在 WSL 下运行,您是否考虑过在 WSL 下安装 Jenkins?然后一切都已经在 GNU 土地上,您不必从 Jenkins 配置中将 Windows 的环境/文化桥接到 GNU。