Connecting Jenkins to a private Github repository can be a pain if not done properly. Also, I would recommend you to check out how things work for a public Github repo. If you have already tried it, good. Otherwise, follow my another blog on how to integrate Jenkins with Github for public repository.
- Jenkins should be installed and the initial Jenkins setup needs to be done.
- A Private Github repository.
Generate ssh keys and add to Github:
For this task, we will be using ssh keys to connect to a private Github repository. You can also use username and password, later in this tutorial, I will explain where you need to change instead of ssh keys for login credentials.
- Generate your ssh keys using this blog by Github.
- Adding generated keys to the private Github repository created before.
Go to your private Github repo -> Settings -> Deploy keys.
Add your ssh key inside the key text input folder, if you want write access to the repo click on the Allow write access.
Jenkins configuration to access private repo:
Go to Jenkins dashboard -> Credentials -> System -> Global credentials -> Add credentials.
Give username as Jenkins or whatever you can remember. Add the Private key -> Enter directly and copy paste the same ssh keys here, click on okay. Here you can select Username with password option if you want to use a password to access the private repo from Jenkins. As I mentioned at the beginning of tutorial that which step needs to be changed if you want to enable login with a password.
Till here we are done with Jenkins and Github configuration setup. Now set up the Jenkins job to take this private repo and trigger job on a new commit to the repository.
Jenkins job setup:
Create a new Jenkins job from the dashboard. New item -> Freestyle Project. After this go to source code management -> git. Copy the ssh URL of your Github repo. As you have used ssh key you need to use ssh URL instead of https URL.
Go to your newly created job in Jenkins and select Git inside Source Code Mangement
Add the credentials you created before to the credentials section in source code management under the above Repository URL. I have used the name for those credentials as jenkins-dummy.
Hola!!!. That’s it. Save and try running your job.
Check out this blog of mine which contains how to trigger Jenkins job on Github commit. It contains information about adding public Github repo configuration on Jenkins as well.
Ask your questions below and I will try to answer as many of them.