Jenkins and GIT Integration using SSH Key
Jenkins is an open-source automation tool with built-in plugins for continuous integration purposes. It is used to build compile and test your project which makes developers work easy to make changes in the project.
Integrating Jenkins and git is very important if your project is on Git and you want to build it on Jenkins.
After installing these two in your system. You have to set the Environment Variable for these two also. For that go to control panel->System and Security->System->Advance System Setting
After setting the environment variables in your system now go to Jenkins. Open Jenkins in your web browser and follow the steps given below:
Step 1: Click on Manage Jenkins
Step 2: Click on Global Tool Configuration
Step 3: Set your git home location i.e. give the path of git in your system
Step 4: Now that you have linked your local git with your local Jenkins. It is time to generate the SSH keys for integrating your Jenkins project with your git repository. Open your git bash and type the command ssh-keygen. Just like below, you will generate two files in .ssh folder. One is id_rsa which is the private key and the other file is id_rsa.pub.
Step 5: Now go to Github and login with your account. Then go to settings and select the SSH and GPG keys and then click on the button New SSH Key.
Step 6: Now you have to paste the public key here which you generated and is saved in .ssh folder under the file name id_rsa.pub. Copy the whole key and paste it in git and save it there.
Adding CredentialsAdd Credentials option.
Step 1: Click on the System
Step 2: Select the Global Credentials
Step 3: Then click on Add Credentials
Step 4: Select the Kind dropdown as SSH Username with Private Key and configure it.
Configure the private key here which is stored in .ssh folder under the file name id_rsa.
Configuring Git with Jenkins
Now you have configured both the private and public key in Jenkins and Github respectively. Now open your project and go to configure.
Step 1: In General check the Github project and provide your Github home URL there
Step 2: Give the repository Url in Source Code Management, repository Url can be fetched by clicking on clone and download option of Github and you have to select the SSH Url. Also, add credentials there of Jenkins. Just like this image.
Step 3: Lastly set your pom.xml path if it is a maven project and click on apply and save
The pom.xml will be the path of your project repository. Like if you have multiple repositories in you Github then your formal to write your pom.xml should be like this “Repo_name\pom.xml”.
Now You are all set to clone the git repositories in your Jenkins. Just go to your project and select Build Now and it will clone all the repo to your local directory as well as in the project workspace.