Git Post Receive Hook: update current working branch to staging server

无人久伴 提交于 2019-12-06 09:20:50

问题


So, I have a staging server setup, and what I would like to do is when someone pushes to a non master branch update what the staging server's directory. My current post-receive hook looks like:

echo "post hook is on the run!"

while read oldrev newrev ref
do
    echo "$ref"
done

unset $(git rev-parse --local-env-vars)
cd ../staging/bikereport
git fetch
git pull origin $ref

echo "Post receive finsihed"

However I am not seeing the changes I would like on the server, and remote returns "Already up-to-date" which makes me think it's pulling from Master or something?

This isn't an elegant solution, but we are a very small team working on a single project. An ideal solution would be to save every branch to a new directory within the staging server and then update only that directory per push.


回答1:


cd ../staging/bikereport
git fetch

Beware: your git directory is not your staging/bikereport repo.
It is still the one where the hook is running.

You need to set GIT_DIR to /full/path/ofstaging/bikereport/.git (and to be sure, set GIT_WORK_TREE to /full/path/ofstaging/bikereport) in order for commands run from that repo to work.
And you need to unset GIT_INDEX_FILE!

See "Why doesn't setting GIT_WORK_TREE work in a post-commit hook?" for more details.

You hook should start with:

#!/bin/sh
 unset GIT_INDEX_FILE
 export GIT_WORK_TREE=/full/path/ofstaging/bikereport/
 export GIT_DIR=/full/path/ofstaging/bikereport/.git/


来源:https://stackoverflow.com/questions/17505211/git-post-receive-hook-update-current-working-branch-to-staging-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!