2013年6月17日 星期一

[jenkins] How to setup a Jenkins slave with ssh private key (Key file version, directly assign)

How to setup a Jenkins slave with ssh private key
(Key file version, directly assign)



Quick Guide (4 steps)
ETA: 10 mins
[Slave Machine]
# init: [Slave] Create Account for Autobuild
slave:~$ sudo adduser jing_autobuild


# Step 1: Create Private Key and Public Key
slave:~$ ssh-keygen -t rsa -C "The access key for the slave Jing"
(note: Enter passphrase)
Output:
private key: id_rsa         # copy to master
public key: id_rsa.pub    # copy to slave (.ssh/authorized_keys)


# Step 2: Setup the Key-pair
           (a) Create the authorized key
slave:~$ cat id_rsa.pub >> ~/.ssh/authorized_keys


[Master Machine]

# Step 2 (b) Copy the private key to the master
master:~/.ssh$ scp jing_autobuild@127.0.0.1:.ssh/id_rsa jing_slave_privatekey


# Step 3: Setup the Credential
[Manage Jenkins] -> [Manage Credential] -> [Add Credentials]:
Select: SSH Username with private key
Select: From the file on Jenkins master


Info
File: /var/lib/jenkins/.ssh/jing_slave_privatekey


# Step 4: Create Node
[Manage Jenkins] -> [Manage Node] -> [New Node]


Info
Remote FS root: /home/jing_autobuild
Host:
Credentials:




Detail
Step 1: Create private key and public key (detail)

You are on slave machine
slave:~$ ssh-keygen -t rsa -C "The access key for the slave Jing"
Output:
private key: id_rsa          # for jenkins master to connect this slave
public key: id_rsa.pub     # for this slave to verify the connection from jenkins master
Note: You should enter a strong passphrase for your key. If you do not use passphrase, anyone can commit your codes when the keys were stolen.
example:

Step 2: Setup the public key as the authorized key for verifying the credential key from the master

You are on slave machine
slave:~$ cat id_rsa.pub > ~/.ssh/authorized_keys
example:


Step 3: Setup the the credential key

You are on the Jenkins master
[Manage Jenkins] -> [Manage Credential] -> [Add Credentials]:
Select: SSH Username with private key


Setup the username and key
Username: the username for access the slave machine
Key:  From a file on Jenkins master
Step 1: copy the private key from slave to jenkins master
# You are on the master machine
master:~/.ssh$ scp jing_autobuild@127.0.0.1:.ssh/id_rsa jing_slave_privatekey

ex:

Step 2: set up the private key file


Setup the credential for the node
  [Manage Jenkins] -> [Manage Node] -> select node -> [Configure]




Result