Deploying Your Projects with Git

There are so many different ways to deploy a web project, many IDEs will have deployment tools to assist with this using ftp/sftp.

For web projects I generally use JetBrain’s PHPStorm as my IDE. You can configure a deployment server and sync the files on save or manually sync them. However, as projects grow to significant sizes, the file comparison tool and sync tool slow down considerably, even becoming unusable when you are trying to compare changes in the complete project.

One of my preferred ways to deploy a project, is by using a git hook on the server and git pushing my branch up to it.

I haven’t worked on any project that has not use git for source control in a very long time. Since I am already working in git, having a git command to deploy becomes very convenient. Especially when you have many environments to deploy to.

Another benefit is, that it allows you to set some rules for deploying into specific environments, such as only the master branch can be pushed to the production server. You may also add in any other operations you like as well during the deployment, such as applying migrations, updating composer or npm and anything else. You can even do things like change the contents of a config file for that specific environment or start a Continuous Integration run. Or it also makes it simple to allow your CI tool to automatically push a specific branch to your server on a successful run.

To begin with you will need to make sure you have git installed on your server. You can verify that it is installed by executing the following command on the server:

$ git --version

If it is not installed, install it using the following command.

For Debian and Ubuntu.

$ sudo apt install git

For RedHat, CentOS and Fedora.

$ sudo yum install git

We will need to create a directory on the server to store the repository. This can be anywhere you like. Generally I use “/usr/local/git”. You can name the project anything you like as well, in this case I will use “my-project.git”.

$ sudo mkdir -p /usr/local/git/my-project.git

Next we need to initialize the repository as a bare repo.

$ cd /usr/local/git/my-project.git $ sudo git init —bare my-project.git

Now we will to add in our git hook that will do all the work. This hook can take on many different instructions depending on what you want to enforce. But we will keep it simple and only allow the master branch to be pushed to this server. I am assuming your web project root is located at “/var/www/html”. You can rename the path as appropriate in the “post-receive” file.

$ cd /usr/local/git/my-project.git/hooks

Using your favorite editor, paste in the following into a file called “post-receive”.

#!/bin/bash # # The "post-receive" script is run after receive-pack has accepted a pack # and the repository has been updated. It is passed arguments in through # stdin in the form # <oldrev> <newref> <refname> # # For example: # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master # # see contrib/hooks/ for a sample. while read oldrev newrev ref do if [[ $ref =~ .*/master$ ]]; then echo "Master ref received. Deploying master branch" git —work-tree=/var/www/html —git-dir=/usr/local/git/myproject.git checkout -f # Any additional commands you would like to execute # can be placed here. else echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server." fi done

Next we need to fix the repo permissions.

Set the user and group permissions appropriately for all the files and directories so that all users you want to allow to push to that server can. In this case I am assuming you have a user: deploy and a group: webdev.

$ sudo cd /usr/local/git $ sudo chown -R deploy my-project.git $ sudo chgrp -R webdev my-project.git $ sudo find . -type d -exec chmod 2775 {} \; $ sudo find . -type f -exec chmod 664 {} \;

Lastly our hook file needs to be executable as well.

$ sudo chmod 775 /usr/local/git/my-project.git/hooks/post-receive

Now we need to let our git client know about the new git remote. At the root of your local project directory execute the following commands. You may name the remote anything you like, here I am assuming we are pushing to a production server.

$ git remote add production git@myserver.com:/usr/local/git/my-project.git $ git push production +master:refs/heads/master

And that is it, we are now ready to “git push” our project on the server.

All you will need to do from here on is use the following command to deploy to your server.

$ git push production

Happy Deploying!