Using Laravel's Artisan with Git for Deployment.

There are many ways to handle web application deployment. Decisions on what method to use depends a lot on what type of hosting environment you are using, as well as the size, scope, and critical nature of the application. 

I handle the deployment of several applications with my own artisan "push" command that utilizes Laravel's SSH remote facade. The command is below, but first there's a little setup necessary:

 
  • This assumes you have set up a bare git repository on the remote server that you will push to, then checkout from. I put these repos under /var/git, but if you don't have root access or just want to put them somewhere else, that's fine. Here I'm creating a "bare" git repository and giving ownership to my user account (I don't remember now why I'm giving the www-data group ownership - I think username:username should be fine).
    sudo mkdir /var/git
    sudo mkdir /var/git/my_project.git
    cd /var/git/my_project.git
    sudo git init --bare
    sudo chown username:www-data /var/git/my_project.git
  • In the remote.php config file in your Laravel project, under the connections array, add any additional connections (test, production, etc), add the appropriate credentials, and remote folder root of your project (var/www, var/www/vhosts/project, etc.).
  • In the same file, add a 'repo_path' array key to each connection that points to the remote's bare repo (/var/git/my_project.git). (Remember: you can extend any of Laravel's config files as needed)
  • Add the Push.php artisan command referenced below to the commands directory and register the command with artisan by opening app/start/artisan.php and adding:
    Artisan::add(new Push);
  • Now when you run 'php artisan' from the shell in the root of your project, you should see a new "push" command. The syntax is as follows (the remote name should match the name of the remote repo in your local git config):
    php artisan push [remote name] --branch=[branch name | defaults to 'master']
    

If everything is configured correctly, the command will push the repo to the remote server, then use SSH commands to checkout the HEAD of the repo to the project root your web server config is pointing to. It will then perform a little house cleaning, such as running any outstanding migrations, composer installs, and clearing the cache folders.

If you are using git tags, you can optionally have the command store the most recent tag label in a version.txt file that resides in the project root for output somewhere in your application.

I'm sure this is far from a perfect solution, but it works well for me.

comments powered by Disqus