Skip to content

Instantly share code, notes, and snippets.

@wehappyfew
Created September 29, 2016 13:10
Show Gist options
  • Save wehappyfew/121cdb018f2b5ca38ed66e85d4825455 to your computer and use it in GitHub Desktop.
Save wehappyfew/121cdb018f2b5ca38ed66e85d4825455 to your computer and use it in GitHub Desktop.
Using multiple github deploy keys from a Jenkins instance
# When using a CI server, like Jenkins, in conjunction with github, you may wish to use
# multiple deploy keys (github-speak for an rsa key pair that has been assigned to a single
# repo, rather than a user) to allow Jenkins to pull code from the github repositories
# In the example here, where three repos are used, the idea is to take advantage of ssh's config mechanism
# For use with Jenkins, do the following:
# login to your CI Server
sudo su jenkins
cd ~/.ssh/
# then create three key key pairs
ssh -T repo1.github.com
# on prompt, set target file to repo1-rsa
# repeat for repo2 and repo 3
# you should now have three key pairs: repo1-rsa repo1-rsa.pub repo2-rsa repo2-rsa.pub repo3-rsa repo3-rsa.pub
# browse over to github and click on "admin" for each of the repo's, click on deploy key, and add deploy key
# copy over the .pub file contents for that repo's key pair
# repeat for all three repositories
# edit the ~/.ssh/config file
vim config
# enter contents like the following
Host repo1.github.com
HostName github.com
User git
IdentityFile ~/.ssh/repo1-rsa
Host repo2.github.com
HostName github.com
User git
IdentityFile ~/.ssh/repo2-rsa
Host repo3.github.com
HostName github.com
User git
IdentityFile ~/.ssh/repo3-rsa
# now go into your Jenkins control panel
# in your jobs, alter the configs (or create) so that
# anywhere a github reference is made
# substitute akin to the following:
# repo1.github.com:MyOrganization/Repo1.git
#
# this works great because ssh will lookup repo1.github.com in the config file
# and substitute in the hostname and user, and use the correct private key file
# that corresponds to the deploy key on the repo
@Perfect-X
Copy link

This is exactly what I needed

@abrarcv170
Copy link

That was a nice idea ,i appreciate it,is there any other way to achieve this?

@DevJogger
Copy link

Thanks for sharing! Came into this, but use credentials plugin to solve the problem in the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment