How to run shell script on Teamcity Windows agent? - windows

We used to use Jenkins as our CI tool, I can run shell script in Jenkins windows slave, the steps is:
1.install cygwin on Windows slave
2.add step in Jenkins as shell script
3.add "export PATH=/cygdrive/c/dev/tools/cygwin/bin:$PATH " as the first line
then I can execute any shell script in Jenkins.
Now we need to move to teamcity, and I found above steps not work on teamcity.
when I run a "Command line" and set the first line as "export PATH=/cygdrive/c/dev/tools/cygwin/bin:$PATH", it gives me an error as "'export' is not recognized as an internal or external command".
Is there any way to resolve it? I don't want to re-write our shell script into cmd.

This is quite easy to resolve. The error does not come from Cygwin of course, but from CMD. The export command only makes sense within a Bash script; cmd has no way of knowing what to do with it.
What you need to do is simply specify to CMD that the script should run using Cygwin's command interpreter (bash). So you can either just specify the long path to the bash executable and your script, as in
C:\dev\tools\cygwin\bin\bash D:\myscript.sh
or you can just add the path to the cygwin directory to your machine's PATH variable. You can do this by changing the System Settings and append the path there, or you can do it as part of your script above:
# this is not necessary if you have already changed the System Variable to include the path
set PATH=C:\dev\tools\cygwin\bin;%PATH%
bash D:\myscript.sh

There is a good chance you already have Git installed on the agent with Git Bash. Following is how I run npm commands without changing the PATH variable
"C:\Program Files\Git\bin\bash.exe" -c "cd Source && npm install && npm run build"

With this, you can run the shell/bash commands:-
"C:\Program Files\Git\bin\bash.exe" -c "cd Source && npm install && npm run build"
And from Powershell, if you want to run bash scripts :-
& 'C:\Program Files\Git\bin\bash.exe' .\build.sh

Related

How to create a .bat file which execute python file

I'm new to bat file and started to implementing it. I have a list of linux application commands which starts my application. I have a windows system to deploy, used to git bash to execute those commands, but in every system restart have to start the application manually so I started implementing bat file which mapped in system start up
#echo off
title ML_autostart_API
start "" "C:\Program Files\Git\git-bash.exe"
using the above script I've opened the git bash. In further need to perform below commands
# To activate python Environment
source E:/ML_APIs/Python_Environment/python3.8.10/Scripts/activate
# To navigate the project dir
cd E:/ML_APIs/API/Call_SessionV1
# To set the environment variables
source config/config.sh
# To run python application
python application.py
have to execute the above using git bash since it is open source commands and doesn't execute in windows. git bash is opening and further commands is not working.
You will need to make 2 files, one for the windows command line (.bat) another for the bash script (.sh). The reason being, after you start the bash console, it will work on different window and it has no idea what your .bat contains. We shall call our scripts as boot.bat and start.sh respectively.
boot.bat
#echo off
title ML_autostart_API
start "C:\Program Files\Git\git-bash.exe" start.sh
Notice the start.sh is added at the end of the start command as parameter.
start.sh
# To activate python Environment
source E:/ML_APIs/Python_Environment/python3.8.10/Scripts/activate
# To navigate the project dir
cd E:/ML_APIs/API/Call_SessionV1
# To set the environment variables
source config/config.sh
# To run python application
python application.py
Note
Both scripts are in the same directory.
This answer assumes python is actually recognized in git-bash paths.
Should this is not the case, you can just use the full path to the executable to call it.
A better alternative would be to just execute the bash script directly on start up (using that start "C:\Program Files\Git\git-bash.exe" start.sh), no mixing stuff.

WebStorm mocha test 'sh' is not recognized as an internal or external command

I'm trying to run a mocha test for my node.js code which runs the following line:
exec(`sh ${scriptFile}`);
When I run it from the command line (Git Bash) using npm test, it passes. However, when I run it from WebStorm I get the following error:
'sh' is not recognized as an internal or external command,
operable program or batch file.
I feel like I'm probably missing some WebStorm setting, but I can't figure out what it is. Any ideas?
P.S. I'm on Windows.
For those who are using phpStorm on Windows and come here by Google
Install Git on your machine. Go to Settings/Terminal and set shell path to C:\Program Files\Git\bin\sh.exe
Also add C:\Program Files\Git\bin to your PATH in environment variables.
Git Bash implements a *nix-esque shell simulating bash which can parse your command. Webstorm appears to be trying to execute your command in either powershell or cmd, which do not support sh syntax.
Open the Terminal page of the Settings/Preferences dialog, and configure the Shell path field as follows:
"[path to the git installation]\bin\sh.exe" -login -i
This will probably be "C:\Program Files\Git\bin\sh.exe" -login -i
Source: https://www.jetbrains.com/help/webstorm/2017.1/working-with-embedded-local-terminal.html

Jenkins pipeline sh fail with "cannot run program nohup" on windows

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?
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"
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)
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'
}
}
}
}
}
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 doesn't understand the "sh" command. To enable this, add
C:\Program Files\Git\bin &
C:\Program Files\Git\usr\bin
to the System Environment variable PATH, than restart your system.
Than execute your command in jenkins, it will work.
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.
In my case I replaced 'sh' by 'bat' in Pipeline script and worked.
sh is not windows command. The simple way to enable the use of 'sh' command in windows is to install GIT BASH
Once you install GIT BASH, then you need to set below environment variables path.
C:\Program Files\Git\bin : This path contains sh.exe, bash.exe and git.exe
C:\Program Files\Git\usr\bin : This path contains several Linux based exe and dll (cat.exe, find.exe etc.)
By setting above configuration you will be able to execute 'sh' command in Jenkinsfiles on Jenkins installed on windows machine.
I was getting the same error below solutions worked for me..
Install git-bash
for windows use "bat" instead of "sh"
set "C:\Program Files\Git\usr\bin" to PATH(user variable)
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.
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
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"
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.
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.
I got the above issue in windows 10 and just added the path "C:\Program Files\Git\usr\bin" to the system variables then it started working.

How can I let jenkins run `bash` command on windows?

I have setup a jenkins server on windows10 pro and installed ubuntu bash on the system. And I created a Executable windows batch command and put the command bash -c ls there. When executing this job I got below error:
c:\jenkins\workspaces>bash -c ls
'bash' is not recognized as an internal or external command,
operable program or batch file.
It says that bash is not recognized as an internal or external command. Then I tried to use the absolute path but still not work. The error is shown as below:
c:\jenkins\workspaces>C:\Windows\System32\bash.exe -c ls
'C:\Windows\System32\bash.exe' is not recognized as an internal or external command,
operable program or batch file.
I can run the command manually on windows. How can I configure this on jenkins? I need to run the build command from windows normal command window and in my build script it needs to launch a process inside bash.
You could install MsysGit, which includes bash, and in Jenkins you have to set the shell executable, which is the path to the sh.exe
You could also install cygwin, but i think this uses high amount of RAM for just running bash scripts.
similiar to what #Borislav said, a migrated shell might help, if you had mingw or msys2 and start your java -jar slave.jar from that bash console, you should be able to directly chose to execute shell, just considering it to be a linux node at your jenkins
Try to install git. Git hold a minimal bash environment which can help in your case.
https://git-scm.com/

Jenkins Shell Script Error

I have Cygwin and its bin path is in the path variable. My .sh file association is set to bash.exe. However, when I try to execute a shell script from a Jenkins build, I get the following error:
The system cannot find the file specified
FATAL: command execution failed
java.io.IOException: Cannot run program "sh"
Other posts have indicated this could be an issue with the .sh file association, but I don't see why that would be the case when I'm able to execute .sh files just fine outside of Jenkins. Can anyone tell me if I'm missing a step here? Thanks in advance.
hmm... running jenkins on windows and using cygwin doesn't sound good. are you sure that the command sh is available in your environment? maybe try sh.exe that might work. otherwise start cygwin with bash.exe and try to set an the alias sh to sh.exe... something like that

Resources