Jenkins Integration – Pulling From A Git Private Repository

Integrating Jenkins with Git is not a complicated task by itself and can be achieved by using all kinds of Jenkins plugins that will be discussed later on, As long as you are pulling your code from a Git public repository it is indeed a simple task, The problems begin when your repository is private. This article will help you get through this “Nightmare” .

I will demonstrate the process by pulling from a Git private repository hosted in GitHub, However it will be a similar process for any Git private repository hosted by any Git server for example, BitBucket.

 

1. First we need to generate a new SSH Key.

a. Open Git bash and follow the instructions bellow :

 

git1

 Comments on that section :

– When asked to enter a file which will save the key just press Enter because we want the default settings.

– Don’t type the command ssh-add id_rsa.

2. Now we need to add our GitHub repository url to our known hosts list. There are several ways for doing that, I found that the easiest way is just to perform a “Dummy” pull request for the repository. Just create a folder in any location on the file system, Open the Git bash and go to that location.

Type the following command : git clone [email protected]:example/example.git

You will be asked if you want to add the host to the known hosts list enter “Yes”.

 

3. Next step is to add the newly generated SSH Key to GitHub for authentication.

a. In git bash enter the following command :

clip < ~/.ssh/id_rsa.pub

This command will copy the content of the SSH Key to your clip board. Another alternative is to open id_rsa.pub in any text editor and copy the content.

b. Go To https://github.com/example/example and got to settings. Click on Deply Keys -> Add Deploy Key and enter the content.

 

4. Jenkins runs on a different user, It runs on the local admin thus we need to copy the .ssh directory to the following paths so Jenkins could use it.

a. Copy .ssh folder to Git folder.

b. Copy .ssh folder to C:\Windows\SysWOW64\config\systemprofile in 64 bit machine and to C:\Windows\system\config\systemprofile in 32 bit machine.

 

5. Jenkins configuration :

a. You need to install git plugin, There is a vast variety of git plugins ,You can choose by vendor for example if you use GitHub than install the GitHub plugin, However for most cases the Git plugin will be sufficient.

b. Go to manage jenkins -> configure system and under git installations you will need to enter the path for the git.exe . Another option is to add git to your path and then you can just enter git.exe .

 

 

Enjoy,

Nimrod Ticotzner.

Fork me on GitHub