Step-by-Step: Generate SSH Key Pair for CI/CD
Sospeter Mong'are

Sospeter Mong'are @msnmongare

About: Software Engineer passionate about developing for the web

Location:
Kenya
Joined:
Nov 22, 2018

Step-by-Step: Generate SSH Key Pair for CI/CD

Publish Date: May 27
1 0

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"
Enter fullscreen mode Exit fullscreen mode

If your system doesn’t support ed25519, you can use:

ssh-keygen -t rsa -b 4096 -C "github-deploy"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Replace deployuser with the actual username on your server and your.server.ip with your server IP or domain.

If ssh-copy-id isn't available, do this manually:

cat ~/.ssh/github_cicd.pub
Enter fullscreen mode Exit fullscreen mode

Copy the output and add it to:

~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

on your server.


✅ 3. Add the Private Key to GitHub

  1. Open your private key:
cat ~/.ssh/github_cicd
Enter fullscreen mode Exit fullscreen mode
  1. Copy the entire content (including -----BEGIN OPENSSH PRIVATE KEY----- and -----END OPENSSH PRIVATE KEY-----).

  2. Go to your GitHub repository → SettingsSecrets and variablesActionsNew repository secret.

  3. 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 }}
Enter fullscreen mode Exit fullscreen mode

That’s it — GitHub will now be able to SSH into your server during deployment.

Comments 0 total

    Add comment