Reputation: 1604
As a sole developer on a project, I'm trying to thrash out a simple development and deployment workflow with git, constrained by using a single server for staging and production.
The production path is /var/www/vhosts/site.com
The staging path is /var/www/vhosts/staging.site.com
I understand how to create a branch for staging and that it's best to have prime (live site code) and hub (clone codebase) repos on the server, and then create a local working clone from the hub. But how do I create a git repo in /var/www/vhosts/
that serves both production and staging? Do I need separate repos?
Upvotes: 0
Views: 126
Reputation: 2828
You can use external work trees and change them "on the fly" for bare repos.
Setup the bare repo on your server and then create a post-receive
hook for it to do the deployments.
#!/bin/bash
# post-receive
# deploy production and staging to vhost dirs
# Directory we are deploying to. Should be the directory where the repo's root .gitignore would exist in.
PRODDEST="/path/to/destination/of/production"
STAGDEST="/path/to/destination/of/staging"
while read oldrev newrev refname; do
# Grab the name of the branch that was pushed.
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" = "$branch" ]; then
echo "Master push, deploy production..."
GIT_WORK_TREE=$PRODDEST git checkout -f master
GIT_WORK_TREE=$PRODDEST git clean -fd
elif [ "develop" = "$branch" ]; then
echo "Develop push, deploy staging..."
GIT_WORK_TREE=$STAGDEST git checkout -f develop
GIT_WORK_TREE=$STAGDEST git clean -fd
fi
done
This has been adapted from a single-branch deployment script I use. I did not test it so it may need to be tweaked.
Essentially the bare repo runs the hook and checks if the push was a master push (and deploys production) or a develop push (and deploys staging).
You could also expand this hook to call build tools after the changes have been checked out to compile assets and what not. I usually remove all development packages, sources and build tools after everything is done.
EDIT: this does not work if you need to push a single branch to multiple deploy locations. Not sure what parameters can be sent with pushes, or if remote names could be used somehow.
Upvotes: 1
Reputation: 4100
From the article, you have one main, bare repository called 'hub'. The 'prime' repository and the repository on your computer are clones of 'hub'.
In your situation, you have two 'prime' repositories: one is your staging area ('prime-staging') and one is your production area ('prime-production').
Using a combination of the hooks described in the article and the one described here (that takes a certain action depending on which branch is pushed), your 'prime-staging' or 'prime-production' repositories will be updated.
The 'hub repository should have two branches: master
(or staging
) associated with your staging site and production
associated with your production site. You would do all your work on master
, and push these changes to 'hub', allowing the git hook to update the staging repository. You would look at these changes in the live environment, making any changes needed on master
and pushing again to 'hub'. Once the staging site looks good, you would do:
git checkout production
git reset --hard master
git push origin production
Now, the git hook will see that the production branch has been updated and update your production site accordingly. (NB: assuming hub is just the nomenclature, it's standard to call your primary repository origin
)
So the setup I imagine on the server is:
mkdir -p /path/to/site.git
cd /path/to/site.git #// hub
git init --bare
cd /var/www/vhosts
git clone /path/to/site.git site.com #// prime-production
git clone /path/to/site.git staging.site.com #// prime-staging
You place the hooks in site.git
. When the staging
branch is updated, change into /var/www/vhosts/staging.site.com
and execute a git pull
. And do the same when the production
branch is updated for /var/www/vhosts/site.com
.
Upvotes: 1