Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidfeldi/2078b9159a140331ffe01bdd00ae5469 to your computer and use it in GitHub Desktop.
Save davidfeldi/2078b9159a140331ffe01bdd00ae5469 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment