How to use Jenkins CLI to add new slaves

Overview

Jenkins provides a cli that can used for remote adminstration, which comes bundled with each installation. In this post we will learn how to remotely add new slaves with the tool.

Node Credentials

All master-node communications are handled through SSH over port 22. A username and RSA key-pair is used by the Master node to manage the remote node, which must be created in the Jenkins credential store.

  1. Log into the Jenkins master node with a user that has administrative privileges.
  2. From the navigation sidebar, navigate to Credentials > System > Global Credentials
  3. Click Add Credentials from the sidebar
  4. From the Kind drop menu, select SSH Username with private key
  5. In the ID field, enter jenkins as the ID. The ID is used to reference the credential in pipelines and operational tasks, such as adding new nodes.
  6. Provide a description of the credential in the Description field.
  7. In the Username field, enter jenkins
  8. For the private key, select Enter directly, and paste the contents of the private key we created earlier.
  9. If your key-pair is secured using a passphrase, enter it in the passphrase field.

Preparing the Node

In order to register a new Jenkins slave remotely you will need to perform a few tasks.

  1. Create a new user with name that matches the Jenkins Credential username created earlier. The user must have a home directory set and use the Bash shell as its default shell.
  2. Create the SSH directory for your Jenkins user.
  3. Create a file named authorized_keys in the SSH directory.
  4. Add the contents of the public key of the RSA key-pair created in the Getting Started section above.
  5. Set the permissions of the SSH directory and all of its files so as to only allow the Jenkins user read access.

Downloading Jenkins CLI

Registering a Jenkins Node using Jenkins CLI

De-Registering a Jenkins Node using Jenkins CLI