Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Jenkins Slave to run as a windows service using command line?

I want to create a jenkins slave using command line. The is a document about setting up jenkins slave as a windows service and I followed the instruction there. I can run javaws http://10.121.33.4:8080/computer/Test-Jenkins-Slave-2/slave-agent.jnlp to connect slave to master but the problem of this is it will get disconnect after the machine is restarted.

I think the best way to do this is to install slave as a service. So, I tried to follow the instruction again on that page. I have jenkins Slave installed as a service and the registry key added like below.

Service's property:
enter image description here

Registry values:
enter image description hereenter image description here

I wonder if I did anything incorrectly? Is the document missing something?

like image 377
Anonymous Avatar asked Jun 17 '14 03:06

Anonymous


People also ask

How can we install Jenkins as a Windows service?

Install Jenkins as a Windows service This can be done from JNLP, or by running " java -jar jenkins. war ". Once Jenkins is started this way, look for the "Install as Windows Service" link in the "Manage Jenkins" page (requires Microsoft .

How do I create a Windows slave?

In order to setup a windows-slave agent you need to first Enable the JNLP Agents : Go to Manage Jenkins -> Configure Global Security -> under Agents section -> TCP port for inbound agents -> select Random ->Save.

How run Jenkins agent from command line?

Go to Manage Jenkins > Manage Nodes, click on the newly created agent machine. Run from agent command line. Login to agent node -> download the agent. jar file from Jenkins controller UI to agent machine then while executing the command, please specify download path of agent.


2 Answers

(Last 2019-09-20) The quickest and most reliable way that I've found to install a Jenkins Worker/Slave as a service is to:

  1. Download the jnlp file from Jenkins' "/slave-agent.jnlp" endpoint
  2. Bring up the "slave-agent" GUI
  3. Select the "Install as a service" option from the GUI

Detailed steps and pictures below.

Note: Don't bother with modifying the registry, the embedded install makes the process quick, easy, and repeatable.

So lets say you've configured a node named "amberboch". On the worker/slave (i.e. "amberboch") machine:

  1. Bring up a browser and enter the Jenkins URL for new node (or click on the node within the "Nodes" page of Jenkins), and create the node within Jenkins as you normally would.
  2. Once the node is created, navigate to the node (Jenkins > Nodes > amberboch) and include "/slave-agent.jnlp" on the end of the browser's URL (e.g. "http://jenkins:8080/computer/computer/amberboch**/slave-agent.jnlp**") to download the jnlp file. Notice "slave-agent.jnlp" is added manually Save the file
  3. Execute the slave-agent.jnlp file with Administrator privileges. (You may have specify Java/javaws to start it with "Java(TM) Web Start Launcher") (Alternatively, you may use a format found in JENKINS-29616 proved by Pau Sabats to create a new agent jar, which should retain connection information: java -jar newAgent.jar -jnlpUrl http://jenkins:8080/computer/amberboch/slave-agent.jnlp -secret xxxxxxxx -workDir "E:\JenkinsClient")
  4. Select the "File" => "Install as a service" option from the slave-agent.jnlp GUI

Once the service has been installed, change the service's "log-on" credentials as needed for your particular installation. You may have to reset permissions or delete directories within the work-space if, whilst running in Administrator mode, a job happened to run and thereby created a work-space sub-folder (as any jobs that had run would have done so as the former administrator-privilaged user, and the regular Jenkins-user profile may not have proper permissions to the old workspace folders and files.

I hope this helps save you time and headaches in managing (imho) the best CI option I've come across.

Best regards, Rob

PS - I found another discussion that may also help: Install Jenkins slave as a Windows service in command line

like image 196
Robert Avatar answered Sep 21 '22 04:09

Robert


I have taken the

  • jenkins.exe
  • jenkins.exe.config
  • jenkins.xml

from a Jenkins 'master' installation. Next I have adapted the XML to contains the startup parameters for my slave. This gives something like:

<executable>%JAVA_HOME%\bin\java.exe</executable>
  <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "C:\Tools\jenkins_slave\slave_95\agent.jar" -jnlpUrl http://jenkins_master:9090/computer/slave_95/slave-agent.jnlp -secret 999999999999999999999999999999999999999999 -workDir "C:\jenkins_slaves_workdir"</arguments>

In the XML I have also: - edited the fields for id,name,description to my preferences - removed the content of the 'extensions' block

After that I can just start the windows service running an admin shell on the slave using the command:

sc create <service_name> binpath= "C:\Tools\jenkins_slave\slave_95\Jenkins.exe" start= auto

Which is in principle the same as starting the Jenkins master service.

like image 35
Monger39 Avatar answered Sep 21 '22 04:09

Monger39