Managing Deploy Keys

There are four ways to manage SSH keys on your servers when automating deployment scripts:

This guide will help you decide what strategy is best for you.

SSH agent forwarding

In many cases, especially in the beginning of a project, SSH agent forwarding is the quickest and simplest method to use. Agent forwarding uses the same SSH keys that your local development computer uses.

Pros

Cons

Setup

  1. Turn on agent forwarding locally. See our guide on SSH agent forwarding for more information.
  2. Set your deploy scripts to use agent forwarding. For example, on a bash script, enabling agent forwarding would look something like this: ssh -A serverA 'bash -s' < deploy.sh

HTTPS cloning with OAuth tokens

If you don’t want to use SSH keys, you can use HTTPS with OAuth tokens.

Pros

Cons

Setup

See our guide on Git automation with tokens.

Deploy keys

A deploy key is an SSH key that is stored on your server and grants access to a single GitHub repository. This key is attached directly to the repository instead of to a personal user account.

Pros

Cons

Setup

  1. Run the ssh-keygen procedure on your server.
  2. In the top right corner of any GitHub page, click your profile photo. Sample of an avatar
  3. On your profile page, click the Repositories tab, then click the name of your repository. Repository tab
  4. In your repository’s right sidebar, click Settings. Settings tab
  5. In the sidebar, click Deploy Keys. Deploy Keys section
  6. Click Add deploy key. Paste your public key in and submit. Add Deploy Key button

Machine users

If your server needs to access multiple repositories, you can choose to create a new GitHub account and attach an SSH key that will be used exclusively for automation. Since this GitHub account won’t be used by a human, it’s called a machine user. You can then add the machine user as collaborator or add the machine user to a team with access to the repositories it needs to manipulate. NOTE: Adding a machine user as a collaborator always grants read/write access. Adding a machine user to a team grants the permissions of the team.

Tip: Our terms of service do mention that 'Accounts registered by "bots" or other automated methods are not permitted.' and that 'One person or legal entity may not maintain more than one free account.' But don't fear, we won't send rabid lawyers out to hunt you down if you create machine users for your server deploy scripts. Machine users are completely kosher.

Pros

Cons

Setup

  1. Run the ssh-keygen procedure on your server and attach the public key to the machine user account.
  2. Give that account access to the repositories it will need to access. You can do this by adding the account as collaborator or adding it to a team in an organization.