Ad Code

Using GitHub Actions to automatically deploy code on server

Buildings On Cloud - lorbic.com
Photo by Aleksandar Pasaric from Pexels


So, we’ve got a cloud computing instance (cloud VM or a virtual computer on cloud) and installed Ubuntu, and finally installed and configured and NGINX.
Then we copied our websites source code to the root directory and boom we have deployed a static website on cloud.

But, with this approach whenever we make any changes on our website, we will have to ssh into the server and manually pull the git repository to deploy our latest changes.
Isn’t it a very tedious task. Actually it is and doing so may create room for errors.

So, why don’t we do something to make all this tediousness go away and relax. For the rescue we have GitHub Actions.

In this article we will learn how to automatically deploy our code stored in GitHub repository to the cloud.


1. Create GitHub Actions workflow

Go to your GitHub repository and click on actions tab.
Actions Tab - lorbic.com

Now you will see a page with many GitHub Actions workflow templates.
GitHub Actions Workflows - lorbic.com

You can choose any of these as a starting ground but, I chose nodejs because I will be deploying express backend.
Okay now click on Set up this workflow a shown in below picture.
Click on setup up this workflow - lorbic.com

Great, now you will see a page opened with an editable file.
Edit workflow - lorbic.com

Most Important Step

Replace default runs-on: ubuntu-latest
with runs-on: self-hosted

Now go ahead and change the file and comment line 25 to 31. To comment simply add # in front of the line.

# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Deploy Code On Server
on:
  push:
    branches: [ main ]
jobs:
  build:
    runs-on: self-hosted
    strategy:
      matrix:
        node-version: [16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    steps:
    - uses: actions/[email protected]
    #- name: Use Node.js ${{ matrix.node-version }}
    #  uses: actions/[email protected]
    #  with:
    #    node-version: ${{ matrix.node-version }}
    #    cache: 'npm'
    #- run: npm i
    #- run: npm run build --if-present
    # - run: npm test
    # copy source code to local folder

Awesome, now click on Start Commit on the top-right and save your file.

Now you will be able to see the .github/workflows folder in your source code along with other files and folders. The file we have just created is stored here.
.github/workflows - lorbic.com


We have finished half of the work. So, let’s get to the server and finish the other half.

2. Install and Configure GitHub Actions Runner on Linux

First of all ssh into your server.

ssh -i <path-to-identity-file> <username>@<host-ip> 

Now go to repository setting by this path settings > actions > runner or follow below link (change and to match yours) to create GitHub Actions runner.
https://github.com/<username>/<repository-name>/settings/actions/runners

Click on New self-hosted runner and follow the guidance on that page.

Download

Create a folder
mkdir actions-runner && cd actions-runner  

Download the latest runner package
curl -o  actions-runner-linux-x64-2.284.0.tar.gz  -L  https://github.com/actions/runner/releases/download/v2.284.0/actions-runner-linux-x64-2.284.0.tar.gz 

Extract the installer
tar xzf ./actions-runner-linux-x64-2.284.0.tar.gz

Create the runner and start the configuration experience

Change the link and the token with yours in this command.

./config.sh --url https://github.com/vk4s/portfolio --token <YOUR_TOKEN>  

Last step, run it!
 ./run.sh

3. Setup Services

Configure GitHub Actions Runner to run at startup and in background.

Install as service
sudo ./svc.sh install

Start the service
sudo ./svc.sh start

Check the status
sudo ./svc.sh status

For more details about self-hosted runners check these docs by GitHub.


If you get any error or have a suggestion please leave a comment.
Share this article with your friends. 😀🤟

Post a Comment

0 Comments

Ad Code