Generate SSH Key Pair:
You can generate an SSH key pair using the ssh-keygen
command.
ssh-keygen -t rsa -b 4096 -C "email@example.com"
-t rsa
: Specifies the RSA algorithm.-b 4096
: Uses a 4096-bit key.-C "email@example.com"
: Adds a label (optional).When prompted, you can specify a file to save the key (e.g., ~/.ssh/id_rsa
), or press Enter to accept the default. You may also be prompted to enter a passphrase (optional, but recommended).
Copy the Private Key:
After generating the key, you will have two files: the private key (id_rsa
) and the public key (id_rsa.pub
). You need to copy the contents of the private key file.
cat ~/.ssh/id_rsa
Copy the entire content of the file including this:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA7...
...
-----END RSA PRIVATE KEY-----
Or use
cat ~/.ssh/id_rsa.pub
This should output something like:
ssh-rsa AAAAB3... user@example.com
Alternatively, on Windows, you can use:
type %userprofile%\.ssh\id_rsa.pub
Or using git CLI:
cat ~/.ssh/id_rsa.pub
Add the Public Key to the server:
On your VPS server, add the public key to the ~/.ssh/authorized_keys
file. You can do this by appending the content of the public key file to authorized_keys
:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Navigate to Your Repository: Go to your repository on GitHub.
Go to Settings:
Click on Settings
in the top menu.
Create a Secret:
Secrets and variables
, then Actions
.New repository secret
.Name
to SSH_PRIVATE_KEY
.Value
field.Add secret
.In your GitHub Actions workflow, you can use the secrets by referencing them like $
. Here’s an example snippet:
- name: Set up SSH
uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: $