To use SSH for CI/CD in GitHub Actions, you need to generate an SSH key pair and add:
- The private key to your GitHub repo secrets.
- The public key to your server’s authorized keys.
Here’s how to do it:
✅ 1. Generate SSH Key Pair on Your Local Machine
Run this in your terminal (Linux/macOS/git bash/WSL):
ssh-keygen -t ed25519 -C "github-deploy"
If your system doesn’t support ed25519
, you can use:
ssh-keygen -t rsa -b 4096 -C "github-deploy"
When prompted:
-
Enter file name: You can name it something like
~/.ssh/github_cicd
- Passphrase: Leave empty (press Enter) for automation
✅ 2. Copy the Public Key to Your Server
ssh-copy-id -i ~/.ssh/github_cicd.pub deployuser@your.server.ip
Replace
deployuser
with the actual username on your server andyour.server.ip
with your server IP or domain.
If ssh-copy-id
isn't available, do this manually:
cat ~/.ssh/github_cicd.pub
Copy the output and add it to:
~/.ssh/authorized_keys
on your server.
✅ 3. Add the Private Key to GitHub
- Open your private key:
cat ~/.ssh/github_cicd
Copy the entire content (including
-----BEGIN OPENSSH PRIVATE KEY-----
and-----END OPENSSH PRIVATE KEY-----
).Go to your GitHub repository → Settings → Secrets and variables → Actions → New repository secret.
Name:
SSH_PRIVATE_KEY
Value: (paste the private key)
✅ 4. Use the Key in GitHub Actions
Your GitHub Actions file is already using this with:
- name: Set up SSH
uses: webfactory/ssh-agent@v0.7.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
That’s it — GitHub will now be able to SSH into your server during deployment.