This article shows the configuration to deploy 2 GitHub repositories on a single server.


On the Server

  1. GitHub does not allow reusing the same deploy key in more than 1 repository. To cater for this restriction, we create a key pair for each repository. 

    $ ssh-keygen -t ed25519 -C "For repo1"
    $ ssh-keygen -t ed25519 -C "For repo2"
    
  2. Push the private keys into the SSH Agent.

    $ eval "$(ssh-agent -s)"
    $ ssh-add key1
    $ ssh-add key2
    
  3. Create a new config file for SSH.

    $ sudo pico /etc/ssh/ssh_config.d/my_repos.conf
                                                                                    
    Host repo1
    HostName github.com
    User git
    IdentityFile ~/.ssh/key1
    
    Host repo2
    HostName github.com
    User git
    IdentityFile ~/.ssh/key2
    
  4. Reload the SSH process.

    $ sudo systemctl restart ssh
    

On Github

  1. Add key1.pub to repo1.

  2. Add key2.pub to repo2.


Deployment Script

  1. Update the repository URL in your deployment script. For example, in Capistrano:

    (New)
    set :repo_url, 'git@repo1:username/myrepo1.git'
    set :repo_url, 'git@repo2:username/myrepo2.git'
    
    (Old)
    set :repo_url, 'git@github.com:username/myrepo1.git'
    set :repo_url, 'git@github.com:username/myrepo2.git'
    

Known Hosts

  1. If you haven't been able to connect to the remote before, please remember to add it as your known host. E.g.

    ssh -T git@github.com