Jan05
Setup Environment UAT + LIVE – GITHUB (Auto Deploy)
We will cover UAT and Live environments and exclude developer environment.
DEV environment for developer to code and test, before he push his commits to remote repository (github.com), which will be auto deploy to UAT environment for everyone to test with other developers changes. Note – we wont be covering DEV environment.
UAT environment for testing the site and getting ready to push to live environment after we think all the bugs are fixed.
Live environment to have the latest version of the website.
Let’s start with project for Estate Agency New York and we will call this NearYou.com.
Create directory in user home for our project to host uat and live websites.
$ mkdir -p NearYou.com/uat/ $ mkdir -p NearYou.com/live/
Setting up UAT Environment
Change directory to live
$ cd NearYou.com/live/
First clone repository for the project from github.com, dot in the end will put all the content in the current working directory.
$ git clone git@github.com:Username/NearYou.com.git .
Your script / application is now uploaded to the server, configure your UAT environment, so its accessible via http and run the installation for your script, then we will continue back to the git.
Assuming your script webroot is located in app/webroot, we will create ‘www’ directory symbolic link to webroot.
$ ln -s /home/username/NearYou.com/uat/www
Now we can setup httpd.conf from manually or through cpanel to put webroot to www, which will go directly to app/webroot. incase we wanted to change folder, it will be very easy with changing path to symbolic link to another directory.
we will change webroot from cpanel > Add subdomain: UAT.NearYou.com and set document root to be /home/username/NearYou.com/uat/www
http://NearYou.com is setup, now you can install the application or change the configuration ‘config.php’ files and have the site up and running, then we will keep configuration settings separate from our normal repository on another branch, this will keep your credential for server from settings protected, but remember we will never push our new branch to remote repository.
We are ready to processed on creating our new local branch to keep settings separate from updates.
Before you create new branch, you might be curious what files changed, which you can find out by running following command
$ git diff --name-only
Create new local branch uat and switch in this branch
$ git checkout -b uat
Let’s commit all our changes now, which we created manually or by installation script.
$ git commit -am 'configuration changes'
Now we want to edit .git/config with nano and add following line, so when we do ‘git pull’, it will update from master branch.
[branch "uat"]
remote = origin
merge = refs/heads/master
if you run the ‘git pull’ command now, it will say ‘Already up-to-date.’
Create our git pull php script for auto deploy in the www folder.
$ cd ~/NearYou.com/uat/www $ nano gh.php
Add following php code to gh.php and make sure you change the MySecretKey to your own hash key.
<?php define('PRIVATE_KEY', 'MySecretKey'); if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_REQUEST['thing'] === PRIVATE_KEY) echo shell_exec("git pull 2>&1");
Next we put this script in action with webhooks on github.com > project > settings > webhooks & service > add webhooks
Add payload url: http://uat.NearYou.com/gh.php
Secret: MySecretKey
That’s it, we are done for UAT environment. Now any push to github, will automatically deploy to UAT environment.
Setting up Live Environment
Everything is same as above for live environment, except the webhooks. we won’t setup webhook for live environment, instead we will deploy on demand, when everything is running properly on UAT environment, then we will push to live environment with following link.
Note: change all the ‘uat’ names to ‘live’ when redoing everything for live environment.
Deploy On Demand
Remember we created gh.php file in www folder and which we setup for webhook. well for live, we will just call this directly on browser, when we need to deploy to live environment.
https://NearYou.com/gh.com?thing=MySecretKey
The above url will deploy all the latest changes to the server and live site now will have upto-date source code from github with new updates.
Last Note: You can create live branch on remote repository on github and setup to pull that branch only, so if there was any changes in that branch, you can pull them only and not get the head branch, which might not be stable if lots of developers were pushing everyday there changes before testing them.
Feel free to leave a comment and let us me know your thoughts.