Ozzie Neher

Developing for the Web

Git Push to Deploy + Build

October 10th, 2016

This is basically the gist of the Digital Ocean tutorial but with a few extra hints along the way. If you get stuck, there are lots of comments/discussion on that post.

The outcome of this post will get you setup to do something like

git push live master

And have it send your files to your server, install your Composer dependencies, and migrate your database.

1. Setup a git repo on the server

We need to setup an empty git repository on the server. This is not your actual project that you're pushing up, think of it like a local GitHub on your server. It just stores the files you track in git.

SSH into your server and make a directory where you'll store all of the repos for your projects that you want to this up for. Something like /home/YOURUSER/repos/. Inside this folder, create a new folder for your project initialize an empty git repository. In my examples, I'm going to assume my project is called portfolio.

cd /home/ozzie
mkdir portfolio.git
cd portfolio.git
git init --bare

the git init --bare line initializes a blank git repository. If you run ls, you'll see the different files and folders git makes for you. We're going to want to change into the hooks folder.

cd hooks

2. The post-receive hook

Now that we're in the hooks folder of our repo, we're going to want to setup a post-receive hook. If you're not familiar with git hooks, they're files that get called based on different actions in the git life cycle. The post-receive hook gets called whenever you push to this remote destination. Let's make this file.

# This may need to be run with sudo prepended
nano post-receive

Inside nano, enter these lines

git --work-tree=/home/ozzie/www/portfolio --git-dir=/home/ozzie/repos/portfolio.git checkout -f
composer install -d /home/ozzie/www/portfolio
php /home/ozzie/www/portfolio/artisan migrate

Make sure you've swapped out the paths above to the ones related to your project. Once you've entered the above in, press ctrl+x to exit. When prompted, press Y to save and enter to use the original filename.

After you've saved it, we need to make the file executable. Run the command

chmod +x post-receive

Here's how this file works if you've never wrote a bash script before. It goes down the file line by line, executing each command in the order they are written. Here's what ours does:

  1. The first line tells the script what shell to use, we're just using the Ubuntu default.
  2. We first set the working directory to the folder where our project lives (not the repo folder we just made), and then tell it where the repo we want to grab from lives (this is the .git folder we just made). Then we simply use checkout -f to change our projects files to the ones we just pushed up to the .git folder.
  3. Lines 3 & 4 are optional, but I like to have them in there to save me an ssh into the server. These simply run a composer install inside the project directory, just in case I pulled any new ones in, and the last line runs Laravels migrate command to update the database with any changes I might have made locally.

Feel free to add any other commands underneath the 2nd line that are a part of your build process. Maybe you need to run an npm script; all you would need to do is add whatever command you would type into your terminal, underneath that second line.

3. Adding the new repo as a remote

Now, navigate in your terminal to where you are working on this project on your local computer. Mine is ~/Code/portfolio. Inside here we simply want to add our server as a remote option.

git remote add live ssh://youruser@yourdomain.com/home/ozzie/repos/portfolio.git

Now whenever you want to make your changes live on the server, simply run

git push live branchname

And it'll upload it automatically! If you added extra commands after the git checkout in the post-receive file like I did, you'll also see any output that the command generates. This is helpful to know that your build process went smoothly.