As much as I love using Hosting4Real for all my serious projects, I will always have projects that are not serious enough to have the golden hosting plan. For example I am currently working on a school project, where we will have to develop a website for the local amber festival. This is not visited by anyone but the school and will need be developed a lot underway. I therefore got a DigitalOcean server setup for hosting such projects (and my Piwik installation). It is installed with the free Serverpilot plan. Just for the making it easy for myself to deploy a website.
The server has off course Git installed and as I actively uses Github for developing my projects (all school projects are forced to), I decided for myself to setup a automatic deployment of specific Github repos. This made it easy to deploy, both for me and others. Furthermore, as Serverpilot does not support custom users for FTP at the free plan, it is easy and safe for both me, and the rest of my group to upload files to the server. I am not forced to give out passwords to the whole server and can still let the edit the website.
I found a nice little guide telling me how the easiest way of setting up auto deployment under Serverpilot. It was made for Bitbucket, but with a few small changes it was setup for Github. Below I will explain the process. If you want to check it out for Bitbucket oyu can find the guide at Serverpilots FAQ here. The same setup will apply to Gitlab with a very few changes.
I suspect that you already have created and setup a Serverpilot app. When setup, you will need to SSH into the server with the user “serverpilot”.
When logged in, run the function ssh-keygen
You will now have to copy the public key of your server to the Github SSH key page. You will find the public key that you have just generated at the location /srv/users/serverpilot/.ssh/id_rsa.pub
Copy all the content from this file to this page at Github.
Now you will have to move into the folder of your app by cd ~/apps/amberfestival
(Remember to replace my amberfestival app name with your app name)
Next, clone the repo into a folder called repo in the app folder git clone --mirror git@github.com:bjornskov/ravfestival.git repo
You will find the URL for the repo at the Github page of your repo, copy this and paste it instead of mine.
Now that we have setup the repo in the app, we will make a initial checkout. This will copy the files of your public folder into the repo.
cd repo
GIT_WORK_TREE=/srv/users/serverpilot/apps/amberfestival/public git checkout -f master
Make a PHP file in the public folder. Now when you are logged into SSH it is easiest to do this with nano.
nano ~/apps/amberfestival/public/github-deploy.php
(You can call the file whatever you like. It is important to rename it for better security.)
Pase the following code into the file.
<?php $repo_dir = '/srv/users/serverpilot/apps/amberfestival/repo'; $web_root_dir = '/srv/users/serverpilot/apps/amberfestival/public'; // Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'. $git_bin_path = 'git'; $update = false; // Parse data from Bitbucket hook payload $payload = json_decode($_POST['payload']); if (empty($payload->commits)){ // When merging and pushing to bitbucket, the commits array will be empty. // In this case there is no way to know what branch was pushed to, so we will do an update. $update = true; } else { foreach ($payload->commits as $commit) { $branch = $commit->branch; if ($branch === 'master' || isset($commit->branches) && in_array('master', $commit->branches)) { $update = true; break; } } } if ($update) { // Do a git checkout to the web root exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' fetch'); exec('cd ' . $repo_dir . ' && GIT_WORK_TREE=' . $web_root_dir . ' ' . $git_bin_path . ' checkout -f'); // Log the deployment $commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD'); file_put_contents('deploy.log', date('m/d/Y h:i:s a') . " Deployed branch: " . $branch . " Commit: " . $commit_hash . "\n", FILE_APPEND); } ?>
Remember to change the two directories to fit the name of your app in line 2 and 3.
Now setup the URL for the PHP script at Github. You will find the hooks page at the settings page of your project page. For my project, this URL is: https://github.com/bjornskov/ravfestival/settings/hooks
Now the server will automatically deploy the changes you make to your Github repo, as they are pushed into the master.