开发者

Git hook to update various web folders based on branch pushed to remote server

开发者 https://www.devze.com 2023-02-18 17:18 出处:网络
I am developing a web application using Gitosis (Debian Lenny) that I want to be able to push to different remote repos/locations thus allowing a \"bleeding-edge\", \"release-candidate\", & \"prod

I am developing a web application using Gitosis (Debian Lenny) that I want to be able to push to different remote repos/locations thus allowing a "bleeding-edge", "release-candidate", & "production" version of the application with mirrored physical web directories and codebase. I would prefer each dir开发者_运维问答ectory to have a branch of the application. So one repository, three "live" versions.

I am new to Git so there's probably a better solution but what i've come up w/ so far is either finding a way to attach hooks to branches (which I haven't found) or to write a hook that will filter for which branch is being committed.

My question is on how to set up a post-update hook that will check the committed branch, set a variable for the directory based on that branch, and literally copy the codebase into that directory where it can be instantly accessible over HTTP?

git rev-list --branches --pretty=oneline --max-count=1

will return something like:

cc5112ba59bc82f2e847ac264317c062ce80f69f test commit

but i need the name of the branch itself like "experimental" or "master".

So basically I'm looking for something like:

1) Get name of branch that was just committed (e.g. "master", "experimental") into a string

2) Use Bash case to declare what directory to use

3) Run something like "git archive --format=tar HEAD | (cd $LOCATION && tar xf -)" where location is what was returned from case.

I thought I was getting close with the following but realized it didn't return the branch name but the commit message instead:

#!/bin/sh

# Get substr from "sha1[space]commit-msg"
BRANCH=$(git rev-list --pretty=oneline --branches --max-count=1 | awk '{split($0,array," ")} END{print array[2]}')
case $BRANCH in
    "experimental")
        dir="/home/APP_NAME/experimental"
    ;;
    "master")
        dir="/home/APP_NAME/production"
    ;;
esac
# move to location and copy files
git archive --format=tar HEAD | (cd $loc && tar xf -)

I did realize that if I always put the branch as the first part of the commit I could accomplish something like this but I really don't want to worry about it.

Any help would be appreciated. Thanks in advance!


Should you push to a non-bare repo (which isn't a recommended technique, but can work with this post-update hook), you can complete that hook, knowing that:

git symbolic-ref HEAD
# or
git rev-parse HEAD

Give you the name of the current branch, while $ref represents the branch being pushed.
You can then update three different working tree, each one already set on the right branch.


Perhaps I'm missing something, but if this is in a post-update hook then you get the names of the refs that are being updated as parameters to the hook. e.g. if my push updates master and experimental in a repository, then post-update is invoked with:

 post-update refs/heads/master refs/heads/experimental

... and you can access them with $1 as $2 usual in your hook script.

Incidentally, the githooks documentation points out that you might want to consider using the post-receive hook instead, which instead gets information on standard input - however, it also gives you the old and new values of the ref as well as the ref name, which might be useful.

0

精彩评论

暂无评论...
验证码 换一张
取 消