Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run batch scripts on a remote server (windows) from jenkins

Tags:

I've got a continuous integration server (Jenkins ) which builds my code (checks for compilation errors) and runs tests and then deploys the files to a remote server (not a war file, but the actual file structure) I do this with a Jenkins plugin which allows me to transfer files via samba, it does this nightly.

Now, what I need to do is run an ant command on the remote server. And after that I need to start the application server on the remote server, the application server is started by running a .bat file from the command line.

I'm pretty clueless how to accomplish this, I know Jenkins is capable of running batch commands, but how do I make them run in the context of the server and not the context of the build server?

like image 823
Øyvind B Avatar asked Mar 21 '14 08:03

Øyvind B


People also ask

How do I run a batch file on a remote machine using Jenkins?

Install an SSH server on your remote windows (MobaSSH home edition worked well for me) Make sure your Jenkins user, on your Jenkins machine, has the required certification to open an SSH connection with your remote (you can simply open a terminal and ssh to your remote once, then accept the certification.


2 Answers

If Jenkins on Windows, remote on *nix, use plink.exe (which is essentially command line PuTTy)

If Jenkins on Windows, remote on Window, use psexec.exe

If Jenkins on *nix, remote on *nix, use ssh

If Jenkins on *nix, remote on Windows, (update 2015-01) Ansible http://docs.ansible.com/intro_windows.html has support for calling Windows commands, eg powershell, from a unix/linux machine, https://github.com/ansible/ansible-examples/blob/master/windows/run-powershell.yml

Tell me what OSes are involved (both on Jenkins and remote), and I will flash this out further.

Edit:
The download page for psexec.exe lists all command line options. You will want something along the lines of:

psexec \\remotecomputername -u remoteusername -p remotepassword cmd /c <your commands here>
Replace <your commands here> with actual commands as you would execute them from command prompt.

Note that psexec first needs to install a service, and required elevated command prompt/admin remote credentials to do so.
Also, you need to run psexec -accepteula once to accept the EULA prompt.

like image 139
Slav Avatar answered Sep 28 '22 07:09

Slav


Following Slav's answer above, here is a simpler solution for Jenkins (*nix) to remote (windows):

  1. Install an SSH server on your remote windows (MobaSSH home edition worked well for me)
  2. Make sure your Jenkins user, on your Jenkins machine, has the required certification to open an SSH connection with your remote (you can simply open a terminal and ssh to your remote once, then accept the certification. Make sure it is saved for the Jenkins user).
  3. You can now add an execute shell build phase in your Jenkins job which can SSH to your remote windows machine.

Notes :

  1. The established connection might require some additional work - you might have to set windows environment variables or map network drivers in order for your executed commands or batch files to work properly on your windows machines.
  2. If you wish to run GUI related operations this solution might not be relevant (Following my work on running automation tests which require GUI manipulation).
  3. Using Jenkins SSH plugin is an issue, as seen here.
like image 45
Rann Lifshitz Avatar answered Sep 28 '22 07:09

Rann Lifshitz