In this tutorial, we will see, How can you Setup GIT with Jenkins.
This tutorial demonstrates how to set up Jenkins and Git to work together and provide a smooth continuous integration system.
This tutorial we will explain the Benefit and the real meaning of CI. To connect the Git with Jenkin, We will install the Git plugin inside the Jenkins. Now, understand, How it will help us to make product life cycle more reliable and easier.
Suppose we have configured the Git with Jenkins. Now we will configure our Project and in Build Trigger, we provide the configuration in this way like, When anyone check-in the code, Jenkin will run and create the build and Test the source code and report immediately if any error occurs.
Let’s see how can we connect the Jenkins and Git –
1. Open the Jenkins in your browser. (http://localhost:8080/)
2. Click on the ‘Manage Jenkins‘ link. (Present in the left menu list)
3. In the Manage Jenkins, Find and click on the link ‘Manage Plugins‘. This will display all the plugin that support the Jenkins.
4. After this switch to ‘Available‘ tab by click on the Available link.
5. Here find the plugin named ‘GIT plugin‘ and check the checkbox for this. Please note there are many available plugins with the name after Git but be specific and chose the plugin named ‘GIT plugin’ only.
6. Now click the button ‘Download now and install after restart‘. This will start the installation, This will take some time so keep patience.
7. Now click on check box ‘Restart Jenkins…’. This will restart the Jenkins.
8. Click the link “ENABLE AUTO REFRESH” in the top right of the page.
You have installed the Git Plugin with Jenkins successfully. But still one task is pending, We need to know Jenkins where the Git is installed.
- Go to ‘Manage Jenkins’.
- Navigate to ‘Configure System‘ and click on it.
- Scroll down the page, You will find the section ‘Git’ and option ‘Path to Git executable’ within this section.
- You need to provide your Git cmd path here. So go to your C drive and program files. Open the directory named Git and inside Git, directory cmd and inside cmd you git.exe file is present. We need to provide this path –
- Provide this path in ‘Path to Git executable’ and click on ‘Save’ button.
By default, the Jenkins installer sets up Jenkins to run as a service, which runs as the “Local System account”, NOT your user account. Since the “Local System account” does not have SSH keys orknown_hosts set up, “git clone” will fail.
So first we need to add Jenkins build SSH keys to the “Local System account”.
Generate and Configure SSH key for GitHub
Let’s see how can we create and link the SSH key to the local system account.
1. Open ‘Git Bash’. Type Git Bash in window search and it will show you git bash icon. Click on this.
Products from Amazon.in
Price: Check on Amazon
Price: INR 29,950.00
Price: INR 15,490.00
2. Go to your desired directory. (Like I want to use C drive so I will use cd c://)
3. Use the command ‘pwd’, To check your current working directory.
4. Now we will create a .ssh directory inside the C drive use command —
5. Go to your .ssh directory. Using command cd .ssh
6. To generate the RSA or SSH key enter gave command and press enter until you get the Key in console–
ssh-keygen -t rsa -C “Your GitHub email address”
7. This will generate the RSA key for you.
You have generated the key successfully. You can go to your ‘.ssh’ directory and verify the keys. In .ssh directory, we have two keys. One is private key and another is the public key. We will connect our Git with the help of public key but before that we have to register our directory with git using the command –
ssh -v email@example.com
Now go to your .ssh directory, you will get the another file called known_hosts . So using this we have registered our server (Local) with Github.
Now you need to go to your GitHub account and register this SSH key with your account.
- Login your GitHub account.
- Click on your profile menu, Select the option setting and go to setting.
- In Setting select the option, SSH key.
- Enter the SSH key title.
- Go to your .ssh directory, Open the public ssh key using notepad++ and copy it.
- Paste the same key inside the key box on Github and click on Save button.
- Github will ask you to confirm GitHub account password. Enter your password.
Have we configured the Git with Jenkins ?
The answer is Yes. But one more step, We need to Test our Git Connectivity with Jenkins. For this, We need to perform one more activity.
Verify Git and GitHub Connectivity
1. Copy your .ssh directory.
2. Open your Git directory(Where Git is installed). For me, it’s inside the Program files on path —
3. Now paste this .ssh directory inside the Git directory (C:\Program Files\Git).
4. Now open Bin folder inside theGit. (C:\Program Files\Git\bin)
5. Here you will find the sh.exe file. Click on it. It will open as prompt.
6. Enter the command ‘ssh.exe -T firstname.lastname@example.org‘ . This will print you the confirmation message like this –
So finally, We have done. I know this was the lengthy process but all steps was necessary. This is the process, what we need to follow.
If you have any query feel free to contact us.