How to execute sh file from jenkins running on windows - shell

Cygwin installed in my windows and able to execute the sh file using cmd prompt.
same Cygwin plugin has been installed in jenkins which also running in windows.
I created a job in jenkins build step->execute shell command I am giving the command as sh /cygdrive/d/539707/data/getchanges/gymBuild.sh while executing the job I am facing below exception.
NOTE 1: In Jenkins/configuration/ under shell I didn't mention any path
workspace] $ sh -xe
D:\539707\tomcat-7.0.12\temp\hudson4624102689815543789.sh FATAL:
command execution failed java.io.IOException: Cannot run program "sh"
(in directory "C:\Users\539707.jenkins\jobs\Test_Gym\workspace"):
CreateProcess error=2, The system cannot find the file specified at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) at
hudson.Proc$LocalProc.(Proc.java:244) at
hudson.Proc$LocalProc.(Proc.java:216) at
hudson.Launcher$LocalLauncher.launch(Launcher.java:815) at
hudson.plugins.cygpath.CygpathLauncherDecorator$1.launch(CygpathLauncherDecorator.java:66)
at hudson.Launcher$ProcStarter.start(Launcher.java:381) at
hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:95)
at
hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:64)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at
hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
at hudson.model.Build$BuildExecution.build(Build.java:205) at
hudson.model.Build$BuildExecution.doRun(Build.java:162) at
hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
at hudson.model.Run.execute(Run.java:1738) at
hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at
hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410) Caused by:
java.io.IOException: CreateProcess error=2, The system cannot find the
file specified at java.lang.ProcessImpl.create(Native Method) at
java.lang.ProcessImpl.(ProcessImpl.java:385) at
java.lang.ProcessImpl.start(ProcessImpl.java:136) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ... 16 more
Build step 'Execute shell' marked build as failure
Finished: FAILURE
NOTE 2: In Jenkins/configuration/ under shell I mention C:\cygwin\bin\mintty.exe
After that below is the output
$ C:\cygwin\bin\cygpath -w C:\cygwin\bin\mintty.exe [workspace] $
C:\cygwin\bin\mintty.exe -xe
D:\539707\tomcat-7.0.12\temp\hudson4745164988293910592.sh
/usr/bin/mintty: unknown option '-x' Try '--help' for more
information. Build step 'Execute shell' marked build as failure
Finished: FAILURE
Kindly suggest How to execute sh file from jenkins running on windows

For each windows slave you can do the following to add cygwin to path assuming that your slave has cygwin installed already
Jenkins - Manage Jenkins - Manage Nodes
node - Configure
Environment variables : list of key-value pairs
add: name: PATH value: ${PATH};path-to-cygwin\bin
E.g., name: PATH value: ${PATH};d:\tools\cygwin\bin

here is the solution: Shell executable path to cygwin_home\bin\sh and in jenkins build step->execute shell command give the file name ex *.sh or clear Shell executable path and in jenkins build step->execute windows batch command sh path*.sh

Related

Jenkins bash scripts in temp folder not executing

I have a simple code as script file
#!/bin/bash
name1=$1
name2=$2
echo " $name1 $name2"
when i run in putty connected to the centos system inside the jenkins contained, it is giving output.
But when i try it through jenkins build, It is saying failure as the file is not found.
Started by user Jenkins Admin
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/my_first_job
[my_first_job] $ /bin/sh -xe /tmp/jenkins5284289512384758361.sh
+ Name=Madan
+ /tmp/1.sh Madan
/tmp/jenkins5284289512384758361.sh: 3: /tmp/jenkins5284289512384758361.sh: /tmp/1.sh: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
This is the error.

How to setup a Jenkins slave agent on a Mac to retrieve files using Plastic?

I have a Jenkins server running on Ubuntu; sources are on Plastic and I already have a Windows slave making Android builds, so the Plastic server is well responding with Jenkins.
I try to build the project for IOS on Mac but the job fails when getting the sources with a java exception
java.io.IOException: error=2, No such file or directory
I tried to remove the source control for the job and just running some commands like :
cm status
It failed, so I updated the PATH environment variable to make it work. Now it's ok for manual commands but still not for the automatic plastic pull.
I configured the job to pull from it using the PlasticSCM Plugin using the selector :
repository "MyRepo"
path "/"
smartbranch "/My/Branch"
Here is the complete error :
[test-mac] $ cm find changeset where date between '2019-05-27T11:14:36' and '2019-05-27T11:15:43' and branch='/My/Branch' on repositories 'MyRepo' --xml --dateformat=yyyy'-'MM'-'dd'T'HH':'mm':'ss
java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
Also: hudson.remoting.Channel$CallSiteStackTrace: Remote call to JNLP4-connect connection from 192.168.1.46/192.168.1.46:49336
at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1743)
at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:357)
at hudson.remoting.Channel.call(Channel.java:957)
at hudson.Launcher$RemoteLauncher.launch(Launcher.java:1059)
at hudson.Launcher$ProcStarter.start(Launcher.java:455)
at com.codicesoftware.plugins.hudson.PlasticTool.tryExecute(PlasticTool.java:97)
at com.codicesoftware.plugins.hudson.PlasticTool.execute(PlasticTool.java:57)
at com.codicesoftware.plugins.hudson.commands.CommandRunner.execute(CommandRunner.java:20)
at com.codicesoftware.plugins.hudson.commands.CommandRunner.executeAndRead(CommandRunner.java:36)
at com.codicesoftware.plugins.hudson.commands.CommandRunner.executeAndRead(CommandRunner.java:25)
at com.codicesoftware.plugins.hudson.commands.ChangesetsRetriever.getChangesets(ChangesetsRetriever.java:27)
at com.codicesoftware.plugins.hudson.commands.ChangesetsRetriever.getDetailedHistory(ChangesetsRetriever.java:36)
at com.codicesoftware.plugins.hudson.PlasticSCM.FindCsets(PlasticSCM.java:363)
at com.codicesoftware.plugins.hudson.PlasticSCM.SetUpWorkspace(PlasticSCM.java:175)
at com.codicesoftware.plugins.hudson.PlasticSCM.checkout(PlasticSCM.java:141)
at hudson.scm.SCM.checkout(SCM.java:504)
at hudson.model.AbstractProject.checkout(AbstractProject.java:1208)
at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:574)
at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:499)
at hudson.model.Run.execute(Run.java:1816)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Caused: java.io.IOException: Cannot run program "cm" (in directory "/Users/MyUser/Jenkins/workspace/test-mac"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at hudson.Proc$LocalProc.<init>(Proc.java:249)
at hudson.Proc$LocalProc.<init>(Proc.java:218)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:936)
at hudson.Launcher$ProcStarter.start(Launcher.java:455)
at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1313)
at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1266)
at hudson.remoting.UserRequest.perform(UserRequest.java:212)
at hudson.remoting.UserRequest.perform(UserRequest.java:54)
at hudson.remoting.Request$2.run(Request.java:369)
at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at hudson.remoting.Engine$1.lambda$newThread$0(Engine.java:93)
at java.lang.Thread.run(Thread.java:748)
I think the problem could be related to the following Jenkins bug:
https://issues.jenkins-ci.org/browse/JENKINS-17157 (Enviroment variables are not loaded in ssh using cygwin)
If your slave is connecting via SSH to your Jenkins server, the environment variables are lost so you can't run "cm" commands.
The following link may be useful: (https://support.cloudbees.com/hc/en-us/articles/229724088-How-to-set-up-a-Jenkins-agent-to-have-the-same-path-as-the-user-when-connected-via-Remote-Desktop-)
In the past, we tried to connect the slave via "Java Web Start" (not via SSH) and we were not able to reproduce this issue with the lost PATH variable (it's only happening when connecting via SSH).
On the macOS:
- Running:
eg:
sudo java -jar agent.jar -jnlpUrl http://MySERVER84/computer/macOsJava/slave-agent.jnlp -secret f5dcf86e0ceed3f22c08bc27fa07d680aff3e11c25452677af4fee218bcfb1aa -workDir "/tmp"

scmStats plugin 0.3.1 works from windows cmd prompt but not from Jenkins job

I am running sonarqube 5.1.1 and sonar-runner-2.4 from windows 2012 r2 server
Java version--java version "1.8.0_20"
After running sonar-runner.bat from the command prompt the scm stats plugin is giving the correct output on the sonar dashboard. But when sonar-runner.bat is being run as jenkins (1.617) job it gives following error :
Executing: cmd.exe /X /C "git whatchanged "--until=2015-06-28 00:01:21 +0000" --date=iso -- C:\sonar_review."
17:01:21.375 INFO - Working directory: C:\sonar_review.
17:01:21.396 WARN - Fail to retrieve SCM info. Reason: The git-log command failed.
'git' is not recognized as an internal or external command,
operable program or batch file.
I am getting the same error in sonarqube 4.5.4 .
 'git' is not recognized as an internal or external command, operable program or batch file.
This is because Jenkins doesn't know where git is installed within your system.
You have to tell Jenkins the installation path of git.
For specifying the git path to Jenkins , go to manage Jenkins > configuring system > search for GIT and specify the git executable command.
Suppose you have git installed in c drive, you have to provide path till git.exe
This will definitely solve your issue.

Run a command shell in jenkins

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)\Jenkins\workspace\test
[test] $ sh -xe C:\Windows\TEMP\hudson6299483223982766034.sh
The system cannot find the file specified
FATAL: L'exécution de la commande a échoué.
java.io.IOException: Cannot run program "sh" (in directory "C:\Program Files (x86)\Jenkins\workspace\test"): CreateProcess error=2, Le fichier spécifié est introuvable
at java.lang.ProcessBuilder.start(Unknown Source)
at hudson.Proc$LocalProc.<init>(Proc.java:244)
at hudson.Proc$LocalProc.<init>(Proc.java:216)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:816)
at hudson.Launcher$ProcStarter.start(Launcher.java:382)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:97)
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:779)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
at hudson.model.Run.execute(Run.java:1744)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:374)
Caused by: java.io.IOException: CreateProcess error=2, Le fichier spécifié est introuvable
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 16 more
Build step 'Exécuter un script shell' marked build as failure
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
Finished: FAILURE
Any idea?
For Windows slave, please use Execute Windows batch command.
For Unix-like slave like linux or Mac, Execute shell is the option.
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.
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
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
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
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.

Hudson on Windows - Error: java.io.IOException: Cannot run program "sh"

I am trying to do continuous integration with Hudson and MSTest.
When I try to run this job I get the following error:
1 Warnung(en)
0 Fehler
Verstrichene Zeit 00:00:00.13
[workspace] $ sh -xe C:\Windows\TEMP\hudson4419897732634199534.sh
The system cannot find the file specified
FATAL: Befehlsausführung fehlgeschlagen
java.io.IOException: Cannot run program "sh" (in directory "C:\Users\Markus\.hudson\jobs\Test1 Unit TEst\workspace"): CreateProcess error=2, Das System kann die angegebene Datei nicht finden
at java.lang.ProcessBuilder.start(Unknown Source)
at hudson.Proc$LocalProc.<init>(Proc.java:187)
at hudson.Proc$LocalProc.<init>(Proc.java:157)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:649)
at hudson.Launcher$ProcStarter.start(Launcher.java:266)
at hudson.Launcher$ProcStarter.join(Launcher.java:273)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:79)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:54)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:34)
at hudson.model.AbstractBuild$AbstractRunner.perform(AbstractBuild.java:646)
at hudson.model.Build$RunnerImpl.build(Build.java:181)
at hudson.model.Build$RunnerImpl.doRun(Build.java:136)
at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:434)
at hudson.model.Run.run(Run.java:1390)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:40)
at hudson.model.ResourceController.execute(ResourceController.java:81)
at hudson.model.Executor.run(Executor.java:137)
Caused by: java.io.IOException: CreateProcess error=2, Das System kann die angegebene Datei nicht finden
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 17 more
Processing tests results in file results.trx
FATAL: No MSTest TRX test report files were found. Configuration error?
[DEBUG] Skipping watched dependency update for build: Test1 Unit TEst #5 due to result: FAILURE
Finished: FAILURE
My Configuration looks like this:
Buildverfahren
Build a Visual Studio project or solution using MSBuild
MSBuild Version MS Build .NET 4
MSBuild Build File trunk\UnitTestWithNHibernate\UnitTestWithNHibernate.sln
Command Line Arguments /p:Configuration=Release
My Command Line looks like this:
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"
/runconfig: trunk\UnitTestWithNHibernate\UnitTest\LocalTestRun.testrunconfig /testcontainer: trunk\UnitTestWithNHibernate\UnitTest\bin\Debug\UnitTest.dll /resultsfile:results.trx
This happens if you have specified your Windows command as "Execute shell" rather than "Execute Windows batch command".
This 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
In my case the builds worked using the "Execute shell" before doing a system reboot and migration and gave the Cannot run program "sh" after. The issue was that the system path got shorter unexpectedly. It's not clear why, but the path lost C:\Program Files (x86)\Git\bin and the version of Git I was using comes with sh.exe.
Of course, you could just change all your "Execute shell" build steps to "Execute Windows batch command" in all your jobs as suggested by #funkybro. This doesn't solve your problem if you have a job that could run on either a Windows system or Linux and you have both systems for other reasons.
Or, you could change this setting to use cmd.exe as suggested by #ajith: Manage Jenkins -> Configure System -> Shell -> Shell executable = C:\Windows\system32\cmd.exe
But, I like having linux commands and using something that more closely resembles Bash. It's my own preference but I thought this might also be helpful. To start from the beginning, I used Chocolatey NuGet to install Git. I also recommend GnuWin to give you some of the very helpful Unix commands.
Install Chocolatey NuGet
In CMD or PowerShell: cinst gnuwin git
Then you can fix this using the Windows UI by adding C:\Program Files (x86)\Git\bin\ to the system path.
Or you can fix it in Jenkins global config: Manage Jenkins -> Configure System -> Shell -> Shell executable = C:\Program Files (x86)\Git\bin\sh.exe
This issue will be resolved by trying this command to make the jenkins job run
For me it work, just try to update this in manage jenkins -->configure system -->search for shell -->and in shell executable give the following command. It helps!!!
C:\Program Files\Git\bin\sh.exe
The way to fix the problem was to put the value of C:\Windows\system32\cmd.exe into the "Shell executable" configuration in Hudson System configuration.
In build section of your job, you can put windows command to execute after compiling the project
EX:
copy target\pmd-rules-extensions-0.0.1-SNAPSHOT.jar D:\projects\sonar\sonar-3.4.1\extensions\rules\pmd\
Just use the "windows batch command" and enter the windows commands as you would want to type them on the command line.

Resources