Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ssh keys with git submodules

I am using an enterprise version of GITHUB. I am creating an automatic build server for to build our projects. These projects have submodules.

As this build server is not "me" (it cannot possess my user credentials) , it needs to be able to download the projects from GIT via the github "deploy keys" feature. (per-project read only ssh-keys)

The limitations are: 1. github expects me to log on as ssh://[email protected] 2. github enforces a policy where no two projects can share a deploy key (unlike regular user keys which are registered for the entire github server). [side question, what is the reasoning behind this?!] 3. My corporate IT does not allow shared arbitrary accounts: A user is always associated with a person, it cannot belong to a server.

As such, I think my only option is to use the deploy keys feature with different keys for different projects. (deploy keys were basically developed with this scenario in mind... )

The only way I found which I can setup multiple keys is: https://gist.github.com/jexchan/2351996

Now regarding submodules: This trick can only work for top-level projects, because that's the only place I can specify a custom hostname, as in: "git clone git@custom-git-host-name" command.

When it goes to init and update the submodules, it uses whatever hostname is in the parent repo (which is the original mygithubserver.com ) . When our devs use their personal keys, this works flawlessly. However, for the build-server which needs different deploy keys for different projects, this fails.

Is there a way to get around this, and have git use different ssh keys for different projects on the same server?

Is there some silly way to have 2 projects share deploy keys? (I am not allowed to modify github source code, as this is a really huge IT managed enterprise githib server)

like image 750
eshalev Avatar asked Jan 31 '17 10:01

eshalev


People also ask

Can I have multiple SSH keys for git?

For most developers, there may be a need to run multiple GitHub accounts on one computer. For instance, you can run an Organization's GitHub account and another one for your personal projects all on the same computer. In this article, you will learn how to use multiple SSH keys for different GitHub accounts.

Can one account have multiple SSH keys?

While using the same password on multiple sites makes your accounts less secure, most of the time you can use the same SSH key for multiple accounts. However, there are specific situations when you'll need to set up more than one SSH key: You have two different Bitbucket Cloud accounts.


1 Answers

I solved it by creating a small script file in the parent project containing the following:

git submodule init
ssh-agent sh -c "ssh-add -D; ssh-add some_private_key.pem; git submodule update some_dubmodule"
ssh-agent sh -c "ssh-add -D; ssh-add another_private_key.pem; git submodule update another_submodule"
like image 140
eshalev Avatar answered Oct 06 '22 02:10

eshalev