Effortlessly Update Your Code to Remote Server from GitHub or GitLab | Step-by-Step Guide

How I push code to UAT server after code commit.

We visits a lot of sites in search of a small solution to get a valid and compact solution for the given problem statement. If you are aware of everything, then you may do by yourself otherwise you have to hit and trial.

Recently, one of my colleague asked me to do an automation, like when code is pushed to the repo, it should reflect to the remote server.

The problem is known and most of us, don't want to run git pull or svn update   on each code commit to the repo. So, this is a tedious process like, login to remote system from SSH, run update, and then logout from there.

And suppose, you are out from your workplace, this will be difficult to make it work, when it need to be pushed.

I enlisted the help of a coworker to create a shell script for the task at hand. However, after testing it, I discovered that the script was not functioning properly when there were numerous file changes. I also found that I needed to include an additional step to run a web script through a browser.

What's the solution.

I was aware of writing post hook in svn, to perform an action after the code commit, if there's a given certain string exists. So I explored Gitlab and Github, there I saw a concept of WebHook, but how to do or run it. I was not familiar with it.
I decided to make it work.
How I did, let's see.
First of all, I written all the steps, what I used to perform after the login by SSH to remote system to a file and named it as:
code-update-uat-sh

#!/bin/bash

# Go to the directory where need to update the code
# from the repo

cd ~/location of the repo checkout or clone

# Once you reach to the location where you code exists
# run command to update the code
# I am using here hard reset to over-ride the local changes if any made.

git fetch --all

# Write to event to the log
git reset --hard origin/master >> gitlog.log

# run git pull once you reset to head of git rev
git pull >> gitlog.log

# If you want to perform any additional task here, like sync of the folder or files to other location
# Or want to run any other task you may write here, but for me this was the only job which was manual

And I saved it at the location of scripts, where my other scripts exists.

Next task for me was to inject this shell script after the commit of code to the git repo.

I again written a PHP script file to inject it from the browser, and may return me a response code.
update.php

<?php

// Run script from location

$run = shell_exec("mylocation/scripts/code-update-uat-sh");

echo $run;

?>

All are ready now, I am able to run it from web. But how I can automate it when some commit happens or any event happens to a specific repo: as master repo

Now time to add the php script in web hook to make fire from repo.

You may explore the repo settings section it Github or GitLab or any other provider like bitbucket or other.



In Gitlab you can do like this





You may choose your own type of repo event, I have taken here for push event.
Save it, and you are ready to go with the WebHook, which will be fired on event you choose. and will do auto code update to your remote server.



This one from GitHub



Hopenyou find this helpful !!!


Effortlessly Update Your Code to Remote Server from GitHub or GitLab | Step-by-Step Guide
Ram Krishna February 23, 2022
Share this post
Our blogs
Sign in to leave a comment
Setting Up Your Own SVN Server on Ubuntu 16+ | Complete Guide
Own SVN Server