Tuesday, September 13, 2016

Set up Jenkins Slave (on Linux)

1        Create a jenkins user on the Slave

Create a jenkins user and a password using the following command on the slave server [your_slave_server]


sudo adduser jenkins --shell /bin/bash
sudo passwd jenkins

By default this creates user jenkins at /home/jenkins
Now login as user jenkins and create a slave directory under jenkins.

mkdir ~/slave

2       Setting up Slaves jenkins on the Master Using Username and Password



Click on the New Node, then give it a name, select the “Permanent Agent” option and click ok.


In the New Node Page, Click Add button besides Credentials field to create the credential, which is the jenkins user and password to the slave server. The password is encrypted in the jenkins system.

Enter the values as indicated below. Then click save


Once saved, Jenkins will automatically connect to the slave machine and configure an agent. Noticeably, slave.jar will be install in /home/jenkins/slave on the slave server and eventually workspace will be created there too.

3        Setting up a Slave jenkins using ssh Key

This is the preferred method as the access is not tied with the user’s password and there won’t be impact if the user’s password is changed.

1.       Login to the slave server as a jenkins user
2.       Create an .ssh directory and cd into the directory

mkdir ~/.ssh
chmod 700 ~/.ssh
cd ~/.ssh

The .ssh directory must have proper access permissions that group and others cannot have write permissions, otherwise, the following error would show when starting the slave.
ERROR: Server rejected the 1 private key(s) for jenkins (credentialId:***/method:publickey)

3.       Create an ssh key pair using the following command. Press enter for all the defaults when prompted

ssh-keygen -t rsa -C "The access key for You Jenkins slave"

4.       Add the public to authorized_keys file using the following command.

cat id_rsa.pub > ~/.ssh/authorized_keys

5.       Copy the contents of the private key in id_rsa to the clipboard.

cat id_rsa


6.       Login in to Jenkins Web Console as an administrator
Go to dashboard –> credentials –> Global credentials –> add credentials 

Select SSH Username with private key 
Enter jenkins as the username
Paste the key in the clipboard to Private Key (Enter directly)
click ok.

7.       Go to Dashboard –> Manage Jenkins –> Manage Nodes
Select the key credential for the Slave configuration then click Save.

No comments:

Post a Comment