Creating Dedicated SSH Keys for GitHub Actions Deployment
Creating Dedicated SSH Keys for GitHub Actions Deployment
Section titled âCreating Dedicated SSH Keys for GitHub Actions DeploymentâThis guide will walk you through creating and setting up a dedicated SSH key for your GitHub Actions deployment to your Digital Ocean droplet. Using a dedicated deployment key rather than your personal SSH key is a security best practice.
Why Use a Dedicated Deployment Key?
Section titled âWhy Use a Dedicated Deployment Key?â- Limited scope: Deployment keys can be restricted to specific operations
- Better security: If compromised, only the deployment is affected, not your personal access
- Easier revocation: You can revoke deployment keys without affecting your personal access
- Audit trail: Easier to track which key was used for which operations
Step 1: Generate a New SSH Key Pair
Section titled âStep 1: Generate a New SSH Key PairâOn your local machine, open a terminal and run:
ssh-keygen -t rsa -b 4096 -C "bartendie-github-actions-deploy" -f ~/.ssh/bartendie_do_deploy_keyThis command:
- Creates a 4096-bit RSA key
- Adds a comment âbartendie-github-actions-deployâ to identify the keyâs purpose
- Saves the key to
~/.ssh/bartendie_do_deploy_key(private key) and~/.ssh/bartendie_do_deploy_key.pub(public key)
When prompted for a passphrase, you can leave it empty since this key will be used in automated processes.
Step 2: Add the Public Key to Your Digital Ocean Droplet
Section titled âStep 2: Add the Public Key to Your Digital Ocean DropletâTransfer the public key to your droplet using the ssh-copy-id command:
ssh-copy-id -i ~/.ssh/bartendie_do_deploy_key.pub root@64.23.245.181Replace your-droplet-ip with your actual droplet IP address.
Alternatively, you can manually add the key by:
-
Viewing your public key:
Terminal window cat ~/.ssh/bartendie_do_deploy_key.pub -
SSH into your droplet:
Terminal window ssh root@64.23.245.181 -
Add the key to authorized_keys:
Terminal window echo "YOUR_PUBLIC_KEY" >> ~/.ssh/authorized_keys
Step 3: Add the Private Key to GitHub Secrets
Section titled âStep 3: Add the Private Key to GitHub Secretsâ-
View and copy your private key:
Terminal window cat ~/.ssh/bartendie_do_deploy_key -
In your GitHub repository:
- Go to Settings > Secrets and variables > Actions
- Click New repository secret
- Name:
SSH_PRIVATE_KEY - Value: Paste the entire private key, including the
-----BEGIN RSA PRIVATE KEY-----and-----END RSA PRIVATE KEY-----lines
Step 4: Add Known Hosts to GitHub Secrets
Section titled âStep 4: Add Known Hosts to GitHub SecretsâGitHub Actions needs to know the SSH fingerprint of your server to establish a secure connection.
-
Get the SSH fingerprint:
Terminal window ssh-keyscan your-droplet-ip -
Add this output as another GitHub secret:
- Name:
SSH_KNOWN_HOSTS - Value: Paste the entire output from the ssh-keyscan command
- Name:
Step A: Add User and Host to GitHub Secrets
Section titled âStep A: Add User and Host to GitHub Secretsâ-
Create a secret for the SSH user:
- Name:
SSH_USER - Value:
root(or your preferred user)
- Name:
-
Create a secret for the host:
- Name:
SSH_HOST - Value: Your dropletâs IP address
- Name:
Step 6: Test the Deployment
Section titled âStep 6: Test the Deploymentâ- Push a commit to your repository
- Check the GitHub Actions workflow runs successfully
- Verify your Docusaurus site is deployed to your Digital Ocean droplet
Security Considerations
Section titled âSecurity Considerationsâ- Store your private key securely; donât commit it to any repository
- If you suspect the key is compromised, immediately remove it from your droplet and generate a new one
- Consider setting up user-level access instead of root for improved security
- Regularly rotate your deployment keys as part of your security practice
Troubleshooting
Section titled âTroubleshootingâIf you encounter permission issues:
- Check the permissions on your droplet:
Terminal window ls -la /var/www/html - Ensure the deploy user has write permissions:
Terminal window chown -R root:www-data /var/www/htmlchmod -R 775 /var/www/html