In ane of the previous blog post, we have discovered how to build GitHub projection using Jenkins. In that post, since information technology was a public repository, we did not tried to authenticate to GitHub. Here, we'll cover how to utilize SSH keys to authenticate to GitHub repos. This applies to both public and private source code repositories.

For this post's purposes, we'll use a sample source code repository at https://github.com/goyalmohit/DemoRepo01. You tin can use any repository on which you have required access.

Generate SSH Fundamental on Jenkins Server

First, nosotros create a directory named githubrepos on the Jenkins server and a hidden directory ssh nether that:

-bash-4.ii$ mkdir githubrepos -bash-4.2$ cd githubrepos/ -bash-4.2$ mkdir .ssh -bash-4.2$ ls -al total 4 drwxrwxr-x. 3 jenkins jenkins 18 Feb 22 23:33 . drwxr-xr-x. 30 jenkins jenkins 4096 Feb 22 23:33 .. drwxrwxr-x. two jenkins jenkins 6 Feb 22 23:33 .ssh              

Later on this, nosotros'll generate ssh key pair and give the new location to save keys:

ssh-keygen -t rsa

We'll exit the passphrase empty. Below is a sample run:

-bash-4.2$ ssh-keygen -t rsa Generating public/private rsa fundamental pair. Enter file in which to save the primal (/var/lib/jenkins/.ssh/id_rsa): /var/lib/jenkins/githubrepos/.ssh/id_rsa Enter passphrase (empty for no passphrase): Enter same passphrase once more: Your identification has been saved in /var/lib/jenkins/githubrepos/.ssh/id_rsa. Your public key has been saved in /var/lib/jenkins/githubrepos/.ssh/id_rsa.pub. The primal fingerprint is: 63:05:7f:nineteen:ec:d0:26:cd:33:cf:a6:cf:b0:c1:f1:67 jenkins@localhost.localdomain The fundamental's randomart image is: +--[ RSA 2048]----+ | . =. | | oo Oo | | o=o= | | . .o + | | S . = | | . . = . E| | * o | | . o | | | +-----------------+ -bash-4.2$ ^C

Subsequently that, we can get public cardinal from id_rsa.pub:

-bash-4.2$ cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCtyO9WjepA/vbU/0m+e2QCibi6NbyugsfARKo3EWWXHU2pitK8hzl034X0BhqHpXOLkEhyM5Thjd5XoNY8YJW+l45SZ5KrchH6W9eM1TpbvxohCxxR3ziWJctvJrvmowteW9ss/Xy+71xxx/J8k1A24z2Rs0CFMopK1PQV8SYWpegfDGyBmHPQNfLvyGV7fe44BTD5KT6mvfF+ImJDHx/jhMI4w+FvPskqJLy25B5/IGVgseB8U6rLptLKqz7zC7Eh2yD6ou33ezEpRHVA2od4+NCbxSeKyEmMB5OtHiNoPH9CaHGWrgdKQp5iOBDGC1LtkCIaGr9AuoBNee3GALJ5 jenkins@localhost.localdomain

Nosotros need to copy this value.

Configure SSH Key for GitHub Project

Once logged in to GitHub, y'all need to become to sample repository on which you lot take access. Go to repository settings -> Deploy keys -> Add deploy key:

Key Based integration with GitHub.JPG

Give a name such equally 'Jenkins Build Server' and add the central. You may select 'let write access' likewise. Since nosotros are using Jenkins only to pull repository, we'll leave this checkbox unchecked.

Install Credentials Plugin for Jenkins

We at present need to add together a plugin called 'Credentials Plugin' inside Jenkins, if its not already added. For this, login to Jenkins console -> Manage Jenkins -> Manage Plugins -> Bachelor and search for credentials plugin:

Select and add Credentials Plugin.JPG

In one case yous select, install it.

Add SSH Key inside Jenkins

At present become to Credentials from left pane within Jenkins console and then click global:

add-new-credentials-inside-jenkins

Afterwards this, select 'Add Credentials':

add-new-credentials-inside-jenkins-2

This will open a new form for us. In the Kind dropdown, select 'SSH username with private primal' and and then requite a name for it. Nosotros'll give 'GitHub-DemoRepo' so that we can easily identify that it is associated with demorepo repository in GitHub. We'll and then give the value of the key directly (below moving picture shows a public fundamental pasted into private key area, which is a fault. You lot demand to put only private key into this area):

Add New Credentials inside Jenkins-3.JPG
Add together New Credentials inside Jenkins-3

After this, click ok to save the credentials.

Install Git on Jenkins Server

Do make sure that git is already installed on the build server for this purposes.

Configure Jenkins Job to apply SSH keys

Become to Jenkins task in reference, under source code management section, select the saved credentials title proper name (As mentioned in one of the comments beneath, the repository url needs to be ssh based url for the github repository):

Configure Jenkins job to use SSH keys.JPG

Once you have added information technology, click apply and save. It's time now to get ahead and build our job.

Verify that SSH key is working

If you lot become to build output, information technology should clearly listing that SSH key is being used for authentication. Beneath is a sample output highlighting the same:

Verify that ssh keys are being used in build logs.JPG
Verify that ssh keys are existence used in build logs.