Currently I deploy a project to production with git, using git push production master
to a repository with the following post-receive hook:
#!/bin/bash
GIT_WORK_TREE=/home/username/www.example.com/myproject/ git checkout -f
production
is a remote, added via git remote add production ssh://server/home/username/projects/myproject.git
.
But now I need to deploy a separate branch to a separate path on the server. I did come up with a solution, but I suppose there's a better way to do it.
What I did was create a new repository on the server, myproject-alternate.git
, with a similar post-receive hook (replacing /myproject/
with /myproject-alternate/
), added this new repository with git remote add alternate ssh://server/home/username/projects/myproject-alternate.git
. Now I can deploy to the alternate path with git push alternate branchname:master
.
This works, but I have some issues:
- The command to deploy to the alternate server is not what I was expecting—more than once I forgot the
:master
at the end and the server's repository received a new branch and the post-receive hook wasn't triggered. - I'm not sure if creating a new repository on the server was the best solution, and I wonder what would happen with a larger project.
Are there other ways to accomplish this deploy flow without the mentioned issues? Maybe a better post-receive hook that uses the received branchname to deploy to the right path? (is this even p开发者_运维百科ossible?)
I've written a blog post about a setup I use to deploy my website to a staging server and the live server. You could do something similar. The key is to configure which branches you're going to be pushing in the .git/config
file of your local repository, something like this:
[remote "alternate"]
url = ssh://server/home/username/projects/myproject-alternate.git
fetch = +refs/heads/master:refs/remotes/alternate/master
pushurl = ssh://server/home/username/projects/myproject-alternate.git
push = refs/heads/branchname:refs/heads/master
[remote "production"]
url = ssh://server/home/username/projects/myproject.git
fetch = +refs/heads/master:refs/remotes/production/master
pushurl = ssh://server/home/username/projects/myproject.git
push = refs/heads/master:refs/heads/master
This will set it up so that whenever you type
git push alternate
it will automatically push the local branchname
branch to the remote master
branch in the alternate repository.
However, since your alternate and production work trees are on the same computer, you can probably get away with only creating one repository and just checking it out to two different places. To do that, ignore the previous paragraph, and instead put something like this in your post-receive hook:
#!/bin/bash
checkout_alt=
checkout_prod=
while read oldrev newrev refname; do
case "$refname" in
( "refs/heads/branchname" )
export checkout_alt=1 ;;
( "refs/heads/master" )
export checkout_prod=1 ;;
esac
done
test -n "$checkout_alt" && GIT_WORK_TREE=/home/diazona/tmp/gittest/alt/ git checkout -f branchname
test -n "$checkout_prod" && GIT_WORK_TREE=/home/diazona/tmp/gittest/prod/ git checkout -f master
(obviously you don't have to use an if
statement). If you do this, I suggest making the repository bare so that it doesn't store its own working copy, just for simplicity. This way, you only need to have one remote, and whenever you push the master
branch, it will update the production work tree, and whenever you push the branchname
branch, it will update the alternate work tree.
Disclaimer: I haven't actually tested this, so try it out in a test folder first. If I find any errors I'll come back and edit.
I've also written a blog post on the set up I use, which allows me to git push
each branch in my project up to my remote, deploying each into their own respective directories.
For example:
Branch | Directory on remote
--------------------------------------
master | /www/foo.com/master
staging | /www/foo.com/staging
production | /www/foo.com/produciton
So, to do this, add the following function to your server dotfiles:
function gitorigin() {
mkdir -p "$1"
cd "$1"
git init --bare
git config core.bare false
git config receive.denycurrentbranch ignore
cat <<EOF >hooks/post-receive
#!/bin/bash
while read oldrev newrev ref
do
branch=\`echo \$ref | cut -d/ -f3\`
mkdir -p ../\$branch
git --work-tree=../\$branch checkout -f \$branch
echo Changes pushed to \$branch
done
EOF
chmod +x hooks/post-receive
cd ..
}
On Remote, create a repository:
$ cd /www/foo.com
$ gitrepo foo.git
On Local, push a branch
$ cd ~/Sites/foo.com
$ git remote add origin ssh:///<USER>@<HOST>/www/foo.com/foo.git
$ git push origin <BRANCH>
Will push your master branch up to remote, into /www/foo.com/<BRANCH>/
.
精彩评论