Setup SSH between Jenkins and Github

Please visit my website and subscribe to my youtube channel for more articles

https://devops4solutions.com/

In this blog, we will explore how to Setup SSH between Jenkins and Github for achieving the passwordless connection . Using this approach, you do not need to provide your credentials to configure the git repo in your Jenkins job

Agenda:

  1. Setup a user jenkins on your Jenkins server
  2. Generate the SSH keys
  3. Copy the the public key in Github
  4. Configure in Jenkins Credentials
  5. Configure a sample job in Jenkins using SSH connection

Prerequisite:

  1. Jenkins should be installed
  2. Github account

Setup jenkins user on jenkins server

  • Generate the ssh keys
  • Ensure the ssh-agent is running
sudo -su jenkins
Image for post
Image for post
Image for post
Image for post

SSH key is generated successfully for the jenkins user

Now copy the .pub file using below command

cat /var/lib/jenkins/.ssh/id_rsa.pub

Github Configuration

  • Go to Github repository -> Setting ->Deploy Keys
Image for post
Image for post

Configure Jenkins Credentials

Add SSH Key inside Jenkins

  • Login to jenkins
  • Navigate to Credentials from left panel in Jenkins console and then click on global or Go to Manage jenkins -> Manage Credentials
Image for post
Image for post
  • After this, select ‘Add Credentials’:
Image for post
Image for post
  • This will open a new form for us.
  • In the Kind dropdown, select ‘SSH username with private key’ and then give a name for it.
  • Copy the private key from the jenkins server for jenkins user
Image for post
Image for post

Now you can clone any git repo in this jenkins instance. You do not need to provide the credentials while configuring the job in Jenkins

Configure Jenkins Job

  • Create a freestyle job in Jenkins
  • Configure SSH url of your git repo

Summary:

Congratulation, we have successfully setup SSH between Jenkins and Github for achieving the password less connection . Using this approach, you do not need to provide your credentials to configure the git repo in your Jenkins job.

Written by

Devops Automation Enginneer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store