Skip to content
My GitHub Profile My Twitter Profile

[GitHub Actions] How configure SSH to deploy a branch

Here you can see how configurate your computer and server to login to your SSH server without password.

1.- Generate SSH key

First of all, you need to generate your SSH key. You can do it with the following command:

mkdir ~/.ssh
cd ~/.ssh
ssh-keygen

The program prompts you to enter a file name for the key pair. Remember that you must not enter a passphrase. In this example, the file name is id_rsa.

2.- Copy the public key to the server

ssh-copy-id -i ~/.ssh/id_rsa.pub [USERNAME]@[HOST]

3.- Copy the result of the following command to the server

ssh-keyscan [HOST]

4.- Configure GitHub Actions Secrets

Go to your repository and click on Settings > Secrets and variables > Actions >New repository secret and add the following secrets:

  • SSH_PRIVATE_KEY: The content of the private key file. Type cat ~/.ssh/id_rsa to see the content.
  • SSH_KNOWN_HOSTS: The result of the ssh-keyscan command (step 3).

4.- Create workflow file

When you want to login to your server, you need to type the following content:

- name: 🔑 Install SSH Key
        run: |
          install -m 600 -D /dev/null ~/.ssh/id_rsa
          echo "${{ secrets.KEY }}" > ~/.ssh/id_rsa
          echo "${{ secrets.KNOWN_HOSTS }}" > ~/.ssh/known_hosts

After that you are able to login to your server from the workflow.