Github Actions: Deploy using SSH

Github Actions: Deploy using SSH
Photo by T K / Unsplash

GitHub Actions is a tool for automating software workflows. With it, you can build, test, and deploy your code right from GitHub. You can set up workflows that run when specific events occur in your repository, or you can run them on a schedule. For example, you can use GitHub Actions to build and test your code, or to publish a package to a package registry. The tool is integrated with the rest of the GitHub platform, so you can use it in conjunction with other features like pull requests and issues.

Why use SSH to deploy your code?

As developers, almost every one of us is familiar with `ssh`. So I chose this example as an introduction to GitHub Actions. There are better ways to deploy your code to your server.

However, this blog caters to absolute beginners who want to get started With CI/CD using GitHub Actions. So let's get started.

What you will learn in this blog?

In this article, we will learn how to deploy a ReactJs app with Python Server on an Ubuntu Machine.

Configuring Your Github Action

Go to your repository in GitHub.

Go to Actions

Click on Setup your own workflow

You will be greeted by a blank editor Like this. GitHub actions are configured in YAML. Consider this as a mix between python and HTML.

What are we trying to achieve?

Deploy our full stack ReactJS and Python backend on every push to the Main Branch

Let's Get Started

name: remote ssh command
      - main
    name: Deploy
    runs-on: ubuntu-latest
    - name: executing remote ssh
      uses: appleboy/[email protected]
        host: ${{ secrets.PROJECT_HOST }}
        username: ubuntu
        key: ${{ secrets.PROJECT_SSH_KEY }}
        port: 22
        script: sudo /home/ubuntu/

So, What's going on here ?

The first 4 lines are self-explanatory Name: the workflow and tell Github when to execute it. We want To execute in whenever a push is made to the main branch.

So each GitHub Action deploys a temporary instance. In our case, we asked GitHub to deploy a Ubuntu instance using `appleboy` template which takes care of all the configuration for us.

Life Tip: If someone else has already done it. Use it, give credit to them and focus on the main task.

In the last line script: is executing our `` which deploys are code.

Simple isn't it 😊

Now this 👉 ${{ secrets.PROJECT_HOST }} is a variable or rather a secret. You don't want everyone in your repository to have these keys or Ips

Here is how you configure it

You Go to settings -> Secrets -> Actions and add your project IP(AWS instance IP or something similar) and the ssh Private Key(just copy and paste it here) and we are done

That's how you deploy your code using SSH, GitHub Actions to a Ubuntu Server. 😊

I forgot. In case you are wondering this is how the looks. Going in-depth is probably another blog

eval $(ssh-agent -s)
ssh-add /home/ubuntu/.ssh/github
# Adding Github Key
cd /home/ubuntu/stellar-ledger
git pull
cd /home/ubuntu/project/app
npm run build
sudo service project_backend restart
sudo service nginx restart

Don't Complicate things or build scalable solutions on your first try. Just keep it simple. Happy Coding 😊