If you are here through a search engine, I wrote an updated version of this article just recently.
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
- Logon to Jenkins with an administrative account.
- Go to Manage Jenkins.
- Click Manage Nodes.
- Click New Node.
- Enter node name like “win-bld01” and click Permanent Agent and then click OK.
- Enter a path in Remote root directory of your choice and choose “Launch agent via Java Web Start” in Launch method.
- Click OK to save the setting.
Download and Configure Jenkins Slave Windows Service
- Download the latest Windows Service wrapper executable from http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/
I’m downloading winsw-2.2.0-net4.exe in this example.
- 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.
- 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 "%BASE%\slave.jar" -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
- Open Windows command line as Administrator.
- Navigate to the directory where you have jenkins-slave.exe e.g. cd C:\Users\amaterasu48\jenkins
- Install the service by executing the following command.
- 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'
- 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.
- Right click it and Start it.
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.