Simple Laravel deployment script

I would like to share with you a small deployment script that we are using in Lighting Beetle, to deploy one of our internal apps.

The application is written with Laravel and is being deployed to one of our VPS.

Deployment script

should

  • upload changed source files from repository to a server
  • run database migration scripts
  • run front-end build

I've created the deploy.sh and put it on the production server:

cd /var/www/our-project  
php artisan down #laravel stuff  
git fetch origin master  
git reset --hard FETCH_HEAD  
git clean -df  
composer update  
npm install  
bower install  
php artisan migrate --force  
grunt build  
php artisan up #laravel stuff  

This script is assuming that you already cloned the repository into /var/www/our-project, and granted access to it for current user. When you run the script simply with ./deploy.sh command, the application will be put in the maintenance mode, it will fetch master branch and run additional install scripts (if you are using tools like Grunt or Bower).

Okey, so that's the script, but logging and running the script each time when we want to deploy is a bit annoying. How to run the script automatically when we push to master (or another) branch?

Automatic deployment

Git has a feature called hooks, that allow us to make a POST request to a server, when certain events happen within the repository. That means, we could create a script (e.g. with PHP) on our server that will proccess the POST request and then run our deployment script (via shell_exec() funcion call).

However, I decided to simplify the process (or even more complicate it?) and use a continuous integration (CI) tool that will run the script for us. In this case, we are using Codeship.io. There are many others, like Travis, or more simple ones like Dploy.io

Besides deployment, Codeship.io offers other great funcionality in automatization of testing our application, but it's beyond the scope of this article.

First, grant access for codeship to your bitbucket/github repository, so it can set up hooks. Then, to configure automatic deployment, open the project and navigate to project settings, select deployment, then check if master branch is set and as a method of deployment, select custom script:

ssh -p 1234 patresk@123.12.12.12 'integration/deploy.sh'  

As you may notice, the command expects integration/deploy.sh to be placed in the home directory of user patresk, and the ssh service listening on 1234 port.

To make the ssh login process work, don't forget to copy codeship public ssh key to authorized ssh keys on the production server.

Also, don't forget that the user patresk requires permission to run the script and also to read/write project files in /var/www/our-project.

That's it! Now, when someone push to the master branch, e.g. when merging a devel branch into it, the script will run and your app will be automatically deployed.

Hope you'll find it useful, and if you have any ideas to make the process better, don't hesistate to submit a comment.