2

We are using windows R2 2012 64bit servers. In future we will be having many machines where we would be running our jenkins slaves. We want to automate the jenkins slave launching process.

I have gone through link by jenkins but did not got much help from it.

I also wonder that how to install same set of tools from master to all slave machines.

I did googling but did not found any article on this topic.

Community
  • 1
  • 1

1 Answers1

0

What I do to add a Windows slave is to use a script which:

  • set the right environment variables like JAVA_HOME,
  • launch the right java -jar slave.jar with the secret key you can see in the Jenkins master node page for that new slave.
    To get slave.jar from the master onto the slave, execute from the slave Windows server:

    curl -o slave.jar https://your.server/jenkins/jnlpJars/slave.jar
    
  • use nssm to declare that script as a Windows service

The script is similar to agent.bat:

set PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0
set PATH=D:\Tools\SonarRunner\bin;%PATH%
set M2_HOME=D:\Tools\apache-maven-3.5.0
set PATH=%M2_HOME%\bin;%PATH%
set PATH=D:\Tools\apache-ant-1.9.3\bin;%PATH%
set GH=D:\Tools\Git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%
set PATH=%JAVA_HOME%\bin;%PATH%

set WORKSPACE_FOLDER=D:\Jenkins\workspace
set GIT_WORKSPACE_FOLDER=D:\Jenkins\workspace

java -Xmx768m -jar slave.jar -jnlpUrl https://your.server/jenkins/computer/<SlaveName>/slave-agent.jnlp -secret 87ef3d...

That script is then called as a Windows service, ran by a dedicated user account:

runas /user:<domain>\<jenkinsUser> cmd ( enter `jenkinsUser` Windows password )

D:\Tools\nssm-2.24\win64\nssm.exe install <SlaveName> D:\Jenkins\agent.bat

Its Windows service is then configured:

sc config <SlaveName> obj= <domain>\<jenkinsUsers> password= <jenkinsUser password>
sc config <SlaveName> start= auto

For automating the installation of other software: see Chocolatey - Software Management Automation, The package manager for Windows.


To fully automate the declaration-side of slaves, use the web API to create the slave, and a groovy script to retrieve the Jenkins node/slave secret JnlpMac key.
See this script for the creation.
And the groovy script (with Jenkins 2.46 or newer) to get the secret key:

echo 'println jenkins.model.Jenkins.instance.nodesObject.getNode("my-agent")?.computer?.jnlpMac' \
  | java -jar ~/Downloads/jenkins-cli.jar -s https://jenkins/ groovy =
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1) If possible could you please provide a sample script of your script. 2) For having secret key of slave, first we need to create slave manually in Jenkins master, right. Can't we automate this from scratch. 3) In script first we need to copy slave.jar file to slave machine ? and then execute java -jar path/to/slave.jar secret_key (what is the exact command), right? –  Jun 19 '17 at 10:15
  • Yes. I am on my phone at the moment, I will add more when I get back. – VonC Jun 19 '17 at 10:17
  • @Johnabraham I have added scripts and API needed to fully automate the declaration of a new node/computer/slave, and its installation on the slave server itself. – VonC Jun 19 '17 at 14:06
  • This is great help, I will definitely try this. thanks :) –  Jun 19 '17 at 14:48
  • @Johnabraham Don't forget to customize agent.bat: you need at least JAVA_HOME. The rest depends on the tools you want your Jenkins slave process to access (I have illustrated maven and ant, your needs might differ) – VonC Jun 19 '17 at 14:50
  • As this script needs to be executed on remote windows servers, what you suggest, which tool or way we can use to execute script remotely(windows to windows). Do you think "PsExec" is good option? –  Jun 20 '17 at 12:02
  • @Johnabraham Sorry, I missed your last command. Did you made it work? Did you used PsExec? – VonC Jun 27 '17 at 09:24
  • No, not yet. I am finding way to execute agent.ps1 to remote machine. –  Jun 27 '17 at 13:40