I'm trying to use multiple deploy keys for github, which means I need to match my SSH configs based on the repository name. I cannot match based on hostname, port or user because they are all the same. Only repo name and deploy keys are different.
I know this question has been asked before but I can't find a satisfactory answer because I am not interested in adding a subdomain or user to my github ssh URL. This question is specifically on how to match a URL PATH or SSH argument or other workaround that do not involve modifying the default github ssh URL.
The default ssh github url is git@github.com:githubusername/githubreponame.git
The usual SSH configs are:
# repo 1
# MATCH or HOST xxxxxxxxxxx
HostName github.com
User git
IdentityFile ~/.ssh/myrepo1name_rsa
# repo 2
# MATCH or HOST xxxxxxxxxxx
HostName github.com
User git
IdentityFile ~/.ssh/myrepo2name_rsa
As per the ssh config manual http://man7.org/linux/man-pages/man5/ssh_config.5.html you can use 2 parameters to match a config, HOST or MATCH.
We already know that HOST cannot do it. So MATCH is the only way:
The available criteria keywords for MATCH are: canonical, final, exec, host, originalhost, user, and localuser.
From those MATCH criteria keywords, it seems none of them have access to the repo name or the SSH arguments or anything useful.
The MATCH exec criteria keyword seems promising:
The exec keyword executes the specified command under the user's shell. If the command returns a zero exit status then the condition is considered true.
exec has access to the following variables http://man7.org/linux/man-pages/man5/ssh_config.5.html#TOKENS:
%h The remote hostname.
%i The local user ID.
%L The local hostname.
%l The local hostname, including the domain name.
%n The original remote hostname, as given on the command line.
%p The remote port.
%r The remote username.
%u The local username.
None of these variables appear useful to me.
I have tried the following:
Match exec "pwd | grep myreponame1"
HostName github.com
User git
IdentityFile ~/.ssh/myreponame1_rsa
It is partly successful, as usually when I am using git I am inside the repo directory so pwd | grep reponame will exit true. But it doesn't work for cloning or if I use a different directory name.
I am looking for a better alternative, which would probably also use MATCH exec in some way.
Edit: the best solution I found so far is:
Match exec "git remote get-url origin | grep reponame || echo $REPO | grep reponame"
But I'm not 100% happy with it because I need to add an env variable before git clone.