Ad Code

Using GitHub Actions to automatically deploy code on server

Buildings On Cloud -
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 -

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

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 -

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

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:
name: Deploy Code On Server
    branches: [ main ]
    runs-on: self-hosted
        node-version: [16.x]
        # See supported Node.js release schedule at
    - 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 -

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.<username>/<repository-name>/settings/actions/runners

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


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 

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.

./ --url --token <YOUR_TOKEN>  

Last step, run it!

3. Setup Services

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

Install as service
sudo ./ install

Start the service
sudo ./ start

Check the status
sudo ./ 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


Ad Code