23

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?

Mrchief
  • 75,126
  • 20
  • 142
  • 189
Øyvind B
  • 331
  • 1
  • 2
  • 6
  • My approach is to use ssh tasks (``, etc) to run commands remotely. Wrap it in an Ant task in your build file on the build machine, to run the server startup script via sshexec. – Dante WWWW Mar 21 '14 at 09:36
  • This depends on what OS the remote server **and** the Jenkins are running on. – Slav Mar 21 '14 at 13:06

4 Answers4

34

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.

AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48
Slav
  • 27,057
  • 11
  • 80
  • 104
  • Both the machines are windows machines, I'll look into using psexec.exe for it. – Øyvind B Mar 22 '14 at 20:15
  • Edited the answer with more info about `psexec` – Slav Mar 24 '14 at 16:46
  • Well, I found [winexe](http://sourceforge.net/projects/winexe/), but that's just someone's own program. For anything more official, I can't think of anything other than setting up `cygwin` on Windows, and from Linux `ssh`ing to `cygwin` and executing a command (which will probably be `psexec` anyways) – Slav Sep 09 '14 at 13:19
  • @Slav : If Jenkins on *nix, remote on Windows....... Any simplier way to handle this use case ? – Rann Lifshitz Feb 06 '17 at 17:01
  • @RannLifshitz sorry, not that I am aware off, but it has been a while since I touched that – Slav Feb 07 '17 at 02:33
  • @Slav I'm exploring a possible simpler solution : running a SSH sever on the remote windows, then running SSHing from the Jenkins *nix to the remote Windows. No problem running scripts via the SSH connection. PowerShell and Ansible are giving me a hard time with their setup, I'm doing a corporate PoC and don't have the luxury of ramping up new tech solutions – Rann Lifshitz Feb 07 '17 at 07:11
2

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.
Community
  • 1
  • 1
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
0

1、i install (MobaSSH home ) on my remote windows server . 2、and install jenkins ssh plugin 3、edit shell eg: go build project 4、it seems something wrong , " go: creating work dir: CreateFile C:\WINDOWS\system32\bsh\tmp: The system cannot find the path specified."

-1

I ended up going with a different approach after trying out psexec.exe for a while.

Psexec.exe and copying files over the network was a bit slow and unstable, especially since the domain I work on has a policy of changing password every months (which broke the build).

In the end I went with the master/slave approach, which is faster and more stable. Since I don't have to use psexec.exe and don't have to copy files over the network.

Øyvind B
  • 331
  • 1
  • 2
  • 6
  • 7
    Can you elaborate on what a master/slave approach is in this context? – mowwwalker Mar 23 '16 at 02:46
  • how exactly did you manage to execute the batch file? I'm also having problems with Psexec... – soninob Jul 23 '18 at 06:45
  • 1
    Master-slave configuration can be found here [Master Agent Config](https://wiki.jenkins.io/display/JENKINS/Step+by+step+guide+to+set+up+master+and+agent+machines+on+Windows) – Shivkumar Birnale Nov 14 '19 at 06:45
  • 1
    It is pretty common when starting with Jenkins to have a single server which runs the master and all builds, however Jenkins architecture is fundamentally "Master+Agent". The master is designed to do co-ordination and provide the GUI and API endpoints, and the Agents are designed to perform the work.: http://wiki.jenkins.io/display/jenkins/distributed+builds – hernant Jun 29 '21 at 23:14