Fri. Mar 29th, 2024

Sometimes you need to be able to add a slave to Jenkins Master via the command line.  I have taken great use of that recently by having it automatically add Docker Slaves.  Here is the code:

cat <<EOF | java -jar jenkins-cli.jar -s http://localhost:8080/ create-node Slave1 --username $username --password $password
<slave>
  <name>Slave 1</name>
  <description></description>
  <remoteFS>/home/jenkins</remoteFS>
  <numExecutors>5</numExecutors>
  <mode>NORMAL</mode>
  <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
  <launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.10">
    <host>192.168.0.124</host>
    <port>22</port>
    <credentialsId>93aa6353-0b12-48d9-8533-6a3dc1b02b83</credentialsId>
    <maxNumRetries>0</maxNumRetries>
    <retryWaitTime>0</retryWaitTime>
  </launcher>
  <label>slave</label>
  <nodeProperties/>
  <userId>jeff@mymiller.name</userId>
</slave>
EOF

Things to change:

Setting

Description

nameThis is the slave name, each slave must have a unique name.
hostThe hostname or IP address to the slave.
credentialsIdThe credentials ID to login on this Slave.  This can be found in the URL by opening up the Credentials that correspond to this slave.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

2 thoughts on “Jenkins Add Slave via Command LIne”
  1. Hi Jeff, thanks for this post. I have a question, is there a command to fetch the configuration details of an existing slave , like the config mentioned above? I want to add an aws instance,and i need to send various parameters like keys, access key etc

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d