When you want to authenticate with GitHub API using SSH, the common approach is to use deploy keys or manage SSH keys through GitHub Apps. Below, I will detail how to use deploy keys for SSH authentication and how to set up and use GitHub Apps for more advanced management.
Using Deploy Keys for SSH Authentication
Deploy keys are SSH keys specifically provided for a single repository, allowing servers to access specific GitHub projects. Here are the steps to set up and use deploy keys:
-
Generate SSH Keys: Generate SSH keys on your server using the
ssh-keygencommand. For example:bashssh-keygen -t rsa -b 4096 -C "your_email@example.com"This generates a key pair (a private key and a public key).
-
Add Public Key to GitHub Repository: Log in to GitHub, navigate to your repository, click "Settings", and select "Deploy keys" from the sidebar. Click "Add deploy key", fill in the Title and Key fields, and paste the public key (typically the content of the
.pubfile) into the Key field. You can also choose whether to grant this key write permissions. -
Use Private Key on Server: Ensure your server uses the generated private key for SSH operations. This typically involves configuring the SSH client (usually in
~/.ssh/config) correctly to point to the appropriate private key.
Using deploy keys is straightforward, but they are limited to a single repository. If you need to push data across multiple repositories, you may need to consider other methods, such as GitHub Apps.
Using GitHub Apps to Manage SSH Keys
GitHub Apps provide more flexible permission control and the ability to access multiple repositories. Here are the basic steps to use GitHub Apps for managing SSH keys:
-
Create a GitHub App: Create a new GitHub App on GitHub. You can find the creation option under GitHub Settings -> Developer settings -> GitHub Apps.
-
Set Permissions and Events: During creation, configure the permissions required for the App and the Webhook events it should respond to.
-
Install the App and Obtain the Private Key: After creation, install this App at the repository or organization level and download the generated private key.
-
Use the App's Private Key for Operations: On your server or development environment, use the App's private key to perform necessary Git operations. Ensure you use the appropriate API to authenticate via the App.
Through GitHub Apps, you can access multiple repositories while having finer-grained permission control, which is particularly valuable for large projects or teams.
In summary, using deploy keys is a quicker way to set up SSH access for a single repository, while GitHub Apps provide more advanced features and finer-grained permission control. Choose the appropriate method based on your specific needs.