I’ve recently covered how to configure git with Hudson on Ubuntu’s Tomcat and it has worked quite well. After taking the plunge and subscribing to have private repositories on github I ran into an issue of not being able to clone the private repository since it was… well, private.
Here is the message I was seeing:
ERROR: Error cloning remote repo 'origin' : Could not clone https://username@github.com/username/repository.git
ERROR: Cause: Error performing git clone -o origin https://username@github.com/username/repository.git /srv/hudson/jobs/repository/workspace
Trying next repository
ERROR: Could not clone from a repository
FATAL: Could not clone
hudson.plugins.git.GitException: Could not clone
at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:587)
at hudson.plugins.git.GitSCM$2.invoke(GitSCM.java:535)
at hudson.FilePath.act(FilePath.java:753)
at hudson.FilePath.act(FilePath.java:735)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:535)
at hudson.model.AbstractProject.checkout(AbstractProject.java:1038)
at hudson.model.AbstractBuild$AbstractRunner.checkout(AbstractBuild.java:479)
at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:411)
at hudson.model.Run.run(Run.java:1257)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:129)
To solve the problem we need to create an ssh key for the tomcat user so that it can access the repository.
cd /usr/share/tomcat6
sudo mkdir .ssh
cd .ssh/
sudo touch known_hosts
sudo ssh-keygen -t rsa -C "tomcat@forthedeveloper.com"
When creating the key pair save the key to /usr/share/tomcat6/.ssh/id_rsa.
I don’t know how to tell tomcat the passphrase for a ssh key, so I just left the passphrase blank.
Once the key has been created we need to add the github site to the known_hosts file. If you have already setup your normal account for working with github and have xclip installed you can run the following to copy the github ssh host information:
ssh-keygen -F github.com | xclip -sel clip
Edit the /usr/share/tomcat6/.ssh/known_hosts file and paste in the copied host information.
To finish things up:
sudo chown -R tomcat6:tomcat6 /usr/share/tomcat6/.ssh/
sudo /etc/init.d/tomcat6 restart
You can copy the new ssh key for tomcat to your clipboard if you have xclip installed with:
cat /usr/share/tomcat6/.ssh/id_rsa.pub | xclip -sel clip
If you are only using hudson for one repository you can just add the public key to the one repository in the repository admin (ex: https://github.com/username/repository/edit) under the ‘Deploy Keys’ area. Otherwise you can add it to your account’s ‘SSH Public Keys’ to allow the tomcat server access to all of your repositories.
Now when you run hudson against your private github repository it should have the proper access.