Jenkins pipeline sh fail with “cannot run program nohup” on windows

别来无恙 提交于 2019-11-26 12:43:32

问题


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

I have the following stage in my Jenkins pipeline :

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

I tried changing Jenkins parameter->shell executable with

C:\\Windows\\System32\\bash.exe

but same error...

how to run sh script using windows 10\'s bash?


回答1:


From a very quick search, it looks like your error is related to the following issue : 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.

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"

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"



回答2:


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).




回答3:


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)




回答4:


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.




回答5:


If you are executing on Windows, just change sh to bat. it will work as expected. Example:

pipeline { agent any stages { stage ('Compile Stage') {

        steps {
            withMaven(maven : 'apache-maven-3.6.1') {
                bat'mvn clean compile'
            }
        }
    }

} }



来源:https://stackoverflow.com/questions/45140614/jenkins-pipeline-sh-fail-with-cannot-run-program-nohup-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!