In some situation you might need to add nodes (aka. agents) programmatically to a Jenkins setup. This is a shell script to register the new node based on the gist of Christopher Davenport.

examples/jenkins/create-jenkins-node.sh

#!/bin/bash

JENKINS_URL=$1
NODE_NAME=$2
NODE_HOME='/home/build/jenkins-node'
EXECUTORS=1
SSH_PORT=22
CRED_ID=$3
LABELS=build
USERID=${USER}

cat <<EOF | java -jar ~/bin/jenkins-cli.jar -s $1 create-node $2
<slave>
  <name>${NODE_NAME}</name>
  <description></description>
  <remoteFS>${NODE_HOME}</remoteFS>
  <numExecutors>${EXECUTORS}</numExecutors>
  <mode>NORMAL</mode>
  <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
  <launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.5">
    <host>${NODE_NAME}</host>
    <port>${SSH_PORT}</port>
    <credentialsId>${CRED_ID}</credentialsId>
  </launcher>
  <label>${LABELS}</label>
  <nodeProperties/>
  <userId>${USERID}</userId>
</slave>
EOF

Notes:

  • "Agent" and "node" is often used interchangably and in the old days they were called "slaves" so you will still see that word used in some documentation and in the code.
  • The origrinal name of the Jenkins project was Hudson and that too still appears in a lot of the code.
  • remove the -remoting flag