I have a PHP project in a BitBucket git repo.
I work in a branch called "develop" for small fixes, or I work in temporary feature branches. When I'm ready to deploy, I merge those branches into "master".
I want to make deploying to my live site as easy as that (merging to master and pushing to BitBucket).
But I really don't want my server to have any access to my repo because that adds security concerns. If you care about security, you want your repo to be in as few places as possible. If your server gets compromised, that's a bad enough situation, but it would be even worse if the attacker then would have access to my full repo. This person agrees.
So I assume that I'll want to use something like git archive master
, like https://stackoverflow.com/a/163769/470749 explains.
How can I set up a hook that detects a push of "master" and then runs git archive master
to export the latest code (not as a repo, though) to a compressed zip file which it then sends (via SCP and/or Rsync?) to the remote server, unzips it to a new directory, and then (maybe via changing a symlink) points the server to that new directory?
Bonus question: how could I enable easy emergency rollbacks? (I imagine there might be situations where I want to revert to the previous commit quickly.)
I'm happy with the scripts I ended up with:
deploy.sh:
archive_and_upload.sh:
revert_to_previous_package.sh:
As you can see, I set my Dreamhost server to serve from a folder called "live", which is really just a symlink to a subfolder that is named as the timestamp for when that package of code was uploaded. There is also another symlink called "previous" which makes rolling back easy (in case I notice problems after deploying and want to revert).