DEV Community

Cover image for How to automate a deploy in a VPS with GitHub actions via SSH
Miguel A. Gavilán Merino
Miguel A. Gavilán Merino

Posted on • Updated on

How to automate a deploy in a VPS with GitHub actions via SSH

Hi everyone.
Some time ago I hired a VPS server to host my portfolio and do tests, but I had a problem. Every time I wanted to push a change to the server, I had to do it manually. The steps I followed were the following:

  • Connect to VPS via SSH
  • Move to project directory
  • Make a git pull to have the new changes on my web
  • Typical things of the framework used (Clear cache, etc...)

But with the appearance of Github actions, these steps changed.
Now we can write a script to tell GhActions 'do this deploy for me'.

This is very easy. Just we should create a yml file on project/.github/workflows/. For example /my_project/.github/workflows/ci.yml
And in this file, we can write something like this:

name: CI

on: [push]

jobs:
  deploy:
    if: github.ref == 'refs/heads/master'
    runs-on: [ubuntu-latest]
    steps:
      - uses: actions/checkout@v1
      - name: Push to server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_IP }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          script: cd ${{ secrets.PROJECT_PATH }} && git pull
Enter fullscreen mode Exit fullscreen mode
  • The on line says that this job is launched on push event.
  • The jobs block defines what jobs will be executed.
    • The if line says that this job will only run in a push to the master branch.
    • The runs-on line says that this job will run on latest ubuntu distro.
    • And the steps block says what are the steps that we are going to execute.
      • uses: actions/checkout@v1. This is a GitHub Action for executing remote ssh commands.
      • Finally, in the with block we configure the parameters for the SSH connection.
        • In the Script label, we can write the commands that we want to run on our server, for example cd our-project-path/ && git pull

Note that I use the GitHub secrets to keep important information hidden.

Once we have this file in our repository, the next time we push the master branch, it will automatically be deployed to our server.
Alt Text

*It is always important to maintain good test coverage in our application to avoid unwanted errors*

Discussion (6)

Collapse
patricnox profile image
PatricNox

If I don't want to use someones image/script, but to write my own, how would one achieve that?

Collapse
unactive profile image
UnActive

Have you found anything since then :|

Collapse
patricnox profile image
PatricNox

Not really, had to clone the image and host it under my account

Collapse
rein96 profile image
Reinhart Andreas • Edited

Thanks for the tutorial

But I got error:

_======CMD======
cd public_html/ && npm run build
======END======
2020/09/23 18:49:42 dial tcp ***:22: i/o timeout
_

Here's the code:
dev-to-uploads.s3.amazonaws.com/i/...

Collapse
shnigi profile image
Niki Ahlskog

I just can't get this to work. I have passphrase for my ssh key. I have secrets added in the repository. I can manually login to my server and do git pull. Then I enter password and it works like it should. However github actions say:

err: git@github.com: Permission denied (publickey).
2020/06/24 13:21:57 Process exited with status 1
err: fatal: Could not read from remote repository.

So what am I doing wrong here? I expected that passphrase would input the password. Or is there something else wrong? I also did this: cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys I suppose this works only if you didn't secure your ssh key. The passphrase option is not working for me.

Collapse
semyondev profile image
Semyon

Thank you very much, only this way works for me