I'd like for Jenkins to automagically fetch data from my private repository hosted on Github. But I have no idea how to accomplish that task.. Tried the documentation, generating ssh-key for jenkins user and all what I can see is: "unable to clone the repo". I've checked URLs - they are valid.
Any clues, maybe you know some docs/blogs/whatever which are describing this kind of stuff?
This question is related to
git
continuous-integration
hudson
github
jenkins
One thing that got this working for me is to make sure that github.com
is in ~jenkins/.ssh/known_hosts
.
Another option is to use GitHub personal access tokens:
https://github.com/my-username/my-project.git
)github-token-for-my-username
I tested this on Jenkins ver. 2.222.1 and Jenkins GitHub plugin 1.29.5 with a private GitHub repo.
If you need Jenkins to access more then 1 project you will need to:
1. add public key to one github user account
2. add this user as Owner (to access all projects) or as a Collaborator in every project.
Many public keys for one system user will not work because GitHub will find first matched deploy key and will send back error like "ERROR: Permission to user/repo2 denied to user/repo1"
An alternative to the answer from sergey_mo is to create multiple ssh keys on the jenkins server.
(Though as the first commenter to sergey_mo's answer said, this may end up being more painful than managing a single key-pair.)
I had a similar problem with gitlab. It turns out I had restricted the users that are allowed to login via ssh. This won't affect github users, but in case people end up here for gitlab (and the like) issues, ensure you add git
to the AllowUsers
setting in /etc/ssh/sshd_config
:
# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
AllowUsers batman git
Jenkins creates a user Jenkins on the system. The ssh key must be generated for the Jenkins user. Here are the steps:
sudo su jenkins -s /bin/bash
cd ~
mkdir .ssh // may already exist
cd .ssh
ssh-keygen
Now you can create a Jenkins credential using the SSH key On Jenkins dashboard Add Credentials
select this option
Private Key: From the Jenkins master ~/.ssh
Source: Stackoverflow.com