Run a command shell in jenkins

前端 未结 6 2120
甜味超标
甜味超标 2020-12-16 10:08

I\'m trying to execute a command shell in Jenkins, I\'m working on Windows 7. In the console output I have this:

Building in workspace C:\\Program Files (x86         


        
相关标签:
6条回答
  • 2020-12-16 10:48

    I was running a job which ran a shell script in Jenkins on a Windows machine. The job was failing due to the error given below. I was able to fix the error thanks to clues in Andrejz's answer.

    Error :

    Started by user james
    Running as SYSTEM
    Building in workspace C:\Users\jamespc\.jenkins\workspace\myfolder\my-job
    [my-job] $ sh -xe C:\Users\jamespc\AppData\Local\Temp\jenkins933823447809390219.sh
    The system cannot find the file specified
    FATAL: command execution failed
    java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessImpl.create(Native Method)
        at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:478)
        at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:154)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
    Caused: java.io.IOException: Cannot run program "sh" (in directory "C:\Users\jamespc\.jenkins\workspace\myfolder\my-job"): CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
        at hudson.Proc$LocalProc.<init>(Proc.java:250)
        at hudson.Proc$LocalProc.<init>(Proc.java:219)
        at hudson.Launcher$LocalLauncher.launch(Launcher.java:937)
        at hudson.Launcher$ProcStarter.start(Launcher.java:455)
        at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:109)
        at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:66)
        at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
        at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
        at hudson.model.Build$BuildExecution.build(Build.java:206)
        at hudson.model.Build$BuildExecution.doRun(Build.java:163)
        at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
        at hudson.model.Run.execute(Run.java:1853)
        at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
        at hudson.model.ResourceController.execute(ResourceController.java:97)
        at hudson.model.Executor.run(Executor.java:427)
    Build step 'Execute shell' marked build as failure
    Finished: FAILURE
    

    Solution :

    1 - Install Cygwin and note the directory where it gets installed.

    It was C:\cygwin64 in my case. The sh.exe which is needed to run shell scripts is in the "bin" sub-directory, i.e. C:\cygwin64\bin.

    2 - Tell Jenkins where sh.exe is located.

    Jenkins web console > Manage Jenkins > Configure System > Under shell, set the "Shell executable" = C:\cygwin64\bin\sh.exe > Click apply & also click save.

    That's all I did to make my job pass. I was running Jenkins from a war file and I did not need to restart it to make this work.

    0 讨论(0)
  • 2020-12-16 10:57

    Error shows that script does not exists

    The file does not exists. check your full path

    C:\Windows\TEMP\hudson6299483223982766034.sh
    The system cannot find the file specified
    

    Moreover, to launch .sh scripts into windows, you need to have CYGWIN installed and well configured into your path

    Confirm that script exists.

    Into jenkins script, do the following to confirm that you do have the file

    cd C:\Windows\TEMP\
    ls -rtl
    sh -xe hudson6299483223982766034.sh
    
    0 讨论(0)
  • 2020-12-16 10:58

    As far as I know, Windows will not support shell scripts out of the box. You can install Cygwin or Git for Windows, go to Manage Jenkins > Configure System Shell and point it to the location of sh.exe file found in their installation. For example:

    C:\Program Files\Git\bin\sh.exe
    

    There is another option I've discovered. This one is better because it allowed me to use shell in pipeline scripts with simple sh "something".

    Add the folder to system PATH. Right click on Computer, click properties > advanced system settings > environmental variables, add C:\Program Files\Git\bin\ to your system Path property.

    IMPORTANT note: for some reason I had to add it to the system wide Path, adding to user Path didn't work, even though Jenkins was running on this user.

    An important note (thanks bugfixr!):

    This works. It should be noted that you will need to restart Jenkins in order for it to pick up the new PATH variable. I just went to my services and restated it from there.

    Disclaimer: the names may differ slightly as I'm not using English Windows.

    0 讨论(0)
  • 2020-12-16 10:59

    Go to Jenkins -> Manage Jenkins -> Configure System -> Global properties Check the box 'Environment variables' and add the JAVA_HOME path = "C:\Program Files\Java\jdk-10.0.1"

    *Don't write bin at the end

    0 讨论(0)
  • 2020-12-16 11:07

    For Windows slave, please use Execute Windows batch command.
    For Unix-like slave like linux or Mac, Execute shell is the option.

    Execute Windows Command shell

    0 讨论(0)
  • 2020-12-16 11:07

    This is happens because Jenkins is not aware about the shell path. In Manage Jenkins -> Configure System -> Shell, set the shell path as

    • C:\Windows\system32\cmd.exe
    0 讨论(0)
提交回复
热议问题