This blog will guide you through a detailed but yet easy steps for setup SSH between Jenkins and Github .Using this approach, you do not need to provide your credentials to configure the git repo in your Jenkins job. and you will achieve the password-less connection.However in order to launch linux instance on AWS where Jenkins is installed, please follow this blog.
If you want to see the video for this article, click here
Agenda:
- Setup a user
jenkins
on your Jenkins server - Generate the SSH keys
- Copy the the public key in Github
- Configure in Jenkins Credentials
- Configure a sample job in Jenkins using SSH connection
Prerequisite:
- Jenkins Server
- Github Account
- Setup a jenkins user for the ssh connection
Configure jenkins user
Step 1. Firstly, generate the ssh keys for the jenkins
user
Ensure the ssh-agent is running
sudo -su jenkins ssh-keygen eval $(ssh-agent -s) ssh-add ~/.ssh/id_rsa
SSH key is generated successfully for the jenkins
user and to achieve the password-less connection between Jenkins and Github
Step 2. Copy the public key using the below command
cat /var/lib/jenkins/.ssh/id_rsa.pub
Github Configuration
Step 1. Go to github repository -> setting ->add deploy keys
Configure Jenkins Credentials
Now we will configure the private key of the jenkins
user in the Jenkins configuration
Add SSH Key inside Jenkins
- Login to Jenkins
- Now go to Manage Jenkins from left panel inside Jenkins console and then click Manage Credentials:
After this, select ‘Add Credentials’:
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
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:
Finally congratulations!! 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.