How to Install Jenkins Slave as Windows Service

If you are here through a search engine, I wrote an updated version of this article.

If you are running Jenkins master on Azure, it automatically provisions Windows VM host but it’s not permanent. What if you want a permanent Windows slave? What if you want to configure a slave on an existing Windows machie? In this example, I am using my own Windows 10 desktop environment (not a good practice however) to make it a permanent Jenkins slave for this article.

I think the best way to run Jenkins slave on Windows is to install it as a Windows service. It can start automatically as Windows starts and we could programmatically check the status, start, stop and restart.

I found it quite hard to find step-by-step information on how to do it and this documentation is really old and it doesn’t work any more with much tighter security on Windows, so I’m going to summarize the right way here.

Creating a Node

  1. Logon to Jenkins with an administrative account.
  2. Go to Manage Jenkins.
  3. Click Manage Nodes.
    manage nodes
  4. Click New Node.
  5. Enter node name like “win-bld01” and click Permanent Agent and then click OK.
    enter node name
  6. Enter a path in Remote root directory of your choice and choose “Launch agent via Java Web Start” in Launch method.
    Launch agent via Java Web Start
  7. Click OK to save the setting.

Download and Configure Jenkins Slave Windows Service

  1. Download the latest Windows Service wrapper executable from http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/
    Download the latest Windows Service wrapper executable
    I’m downloading winsw-2.2.0-net4.exe in this example.
  2. Place it to the Custom WorkDir path and rename it to jenkins-slave.exe. I am using a directory C:\Users\[My User Account]\jenkins in this example.
  3. Create jenkins-slave.xml in the same directory with the following XML. Make sure to change the values in the XML according to your environment.
<service>
  <id>JenkinsSlave</id>
  <name>Jenkins agent</name>
  <description>This service runs an agent for Jenkins automation server.</description>
  <executable>C:\Users\amaterasu48\apps\java\bin\java.exe</executable>
  <arguments>-Xrs -jar &quot;%BASE%\slave.jar&quot; -jnlpUrl https://jks.westus.cloudapp.azure.com/computer/win-bld01/slave-agent.jnlp -secret e54b3a6b958f1e400e9809808sdfs098sdf0s9bcd4938d2fca496fbbea3e889 -workDir=C:\Users\amaterasu48\jenkins</arguments>
  <logmode>rotate</logmode>
  <onfailure action="restart">
    <download from="https://jks.westus.cloudapp.azure.com/jnlpJars/slave.jar" to="%BASE%\slave.jar">
      <extensions>
        <extension classname="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension" enabled="true" id="killOnStartup">
          <pidfile>%BASE%\jenkins_agent.pid</pidfile>
          <stoptimeout>5000</stoptimeout>
          <stopparentfirst>false</stopparentfirst>
        </extension>
      </extensions>
    </download>
  </onfailure>
</service>

For more options, please refer to the official documentation.

Also make sure to create jenkins-slave.exe.config file with the following XML cotent to prevent the executable from running on the earlier version of the .NET Framework.

<configuration>
	<startup>
		<supportedruntime version="v4.0"/>
	</startup>
</configuration>

Install Jenkins Slave

  1. Open Windows command line as Administrator.
  2. Navigate to the directory where you have jenkins-slave.exe e.g. cd C:\Users\amaterasu48\jenkins
  3. Install the service by executing the following command.
    jenkins-slave.exe install
  4. Once it’s successful, you will see a message like this.
    2019-05-22 23:17:14,486 INFO - Installing the service with id 'JenkinsSlave'
  5. Right click Windows start button and select Run. Enter services.msc to open Windows Service management console. You will see the Jenkins agent Windows service we just installed.
    Windows Service management console
  6. Right click it and Start it.

Recap

It’s not a few simple steps but as long as this can be automated, it should not be so painful. Also, if your Jenkins slave has to talk to other resources within your network, it’s wiser to run the Windows service as a domain user that has permission to other servers.