Install Jenkins slave as a Windows service in command line

Maxime Viargues picture Maxime Viargues · Jun 26, 2017 · Viewed 36.5k times · Source

I have been looking a lot on Google on how to install the service in command line (so without manual interaction) but I am stuck on how to get the jenkins-slave.exe

I found those instruction https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins+as+a+Windows+service but I can't figure how to get the executable. I have downloaded and run the slave.jar with the right key, which connects the slave, but the exe is not generated.

I found this page https://github.com/kohsuke/winsw/blob/master/doc/installation.md#winsw-installation-guide to install it manually but that sounds like re-invented the wheel when the jar can do it. Plus there is a risk the WinSW.exe is different and doesn't get updated by the plugin (I saw some automatic update code in it).

So is there a way I can download the jenkins-slave.exe or generate it? or is there a way to run the "Install as a service" in command line from the slave.jar?

Answer

Maxime Viargues picture Maxime Viargues · Aug 10, 2017

To answer my own question, after having contacted the plugin developers:

There is no actual way to download the exe from Jenkins directly, the slave.jar gets it from the master via the remoting protocol. I have created a request to be able to download it via an URL (as suggested by the developer), so it might be available in the future.

Right now the executable is a renamed Windows Service Wrapper binary: https://github.com/kohsuke/winsw so I used this binary the same way.

Regarding the configuration used by WinSW and XML files, I used the one from the GitHub repository https://github.com/jenkinsci/windows-slave-installer-module. The versions are compatible in terms of the configuration.

So basically I download the exe, get the private key from Jenkins and create the service using the configuration from the original plugin. Then I install the service using jenkins-slave.exe install.

Step by step:

  • Get the JNLP command from Jenkins (from the Node page) to get the private key, e.g. java -jar slave.jar -jnlpUrl http://jenkins...
  • Download the slave.jar file from Jenkins (gotten from the JNLP command)
  • Download the service wrapper executable, e.g. http://repo.jenkins-ci.org/public/com/sun/winsw/winsw/2.1.0/winsw-2.1.0-bin.exe
  • Setup the XML used to run the service (available in the module or directly on winsw website)
  • Setup the slave configuration XML file (available on the module source code)
  • Then install the service using jenkins-slave.exe install