Deploying Web Apps With Git
I had previously posted about using Git to publish changes to live websites on a VPS, but have recently started using a much simpler approach.
The key is you can only Git “push” to a bare repo, but you can also checkout a branch from the bare repo to a working directory where your website code lives.
To start, create a directory for the bare Git repo somewhere convenient, like in your home directory. It's conventional to name this directory with a “.git” extension to signify it's a bare repo. Then inside that new directory, initialize your bare repo. Replace “project” with your project name.
mkdir project.git cd project.git/ git init --bare
Now create a
post-receive hook script, that is run when new code is pushed to your server, which checks out your branch into the working directory. (I'm using Nano editor to edit or create this file.)
cd hooks/ nano post-receive
The script looks like this, but of course your update with your own paths, and change
master to your preferred branch.
#!/bin/sh git --work-tree=/path/to/your/web/app --git-dir=/path/to/your/project.git checkout -f master # Optionally, do some other post deployment clean up echo "composer install" cd /path/to/your/web/app && composer install echo "Clearing cache" rm -rf cache/* exit 0
Save your file, and then make it executable.
Finally, to be able to Git push, add your server as a remote on your local machine, where
production is the name of the remote.
git remote add production <serveruser>@<ip>:project.git
Now, when it's time to deploy, it's as simple as,
git push production master