我正在实现一个自定义gitlab挂钩。当有人推入master时,我想更新一个特定的文件。然后提交此更改并将其推送到远程源。钩子如下所示:
clone_repo(){
//cloning a specifc branch on my_app dir
export GIT_WORK_TREE="path/my_app"
}
cd_app(){
cd my_app
}
update_file(){
// updating random.java
}
commit_file(){
git commit -m "commit from hook" random.java
}
while read oldrev newrev refname
do
if [ ${refname} = "refs/heads/master" ]
then
clone_repo
cd_app && update_file
commit_file && push_it
exit 0
fi
done
挂钩正在运行,但cd_app && update_file
没有做假设(不更新update_file)。我假设cd_app
没有改变目录(通过shell打印)。
但是,当我在refname="refs/heads/master"
检查之前设置if
进行测试时,它运行正常!
无法找到gitlab为custom_hooks记录的位置。似乎我错过了一些东西。你们能给我进一步的参考或确定我做错了吗?
答案 0 :(得分:1)
以下是我为我的系统做的事情。
首先,GIT_WORK_TREE看起来像这样:
$ # GIT_WORK_TREE
$ pwd
/home/git/loging_at_post_receive
$
$ git remote -v
origin /home/git/repositories/gitlab-user/loging_at_post_receive.git/ (fetch)
origin /home/git/repositories/gitlab-user/loging_at_post_receive.git/ (push)
$
$ ls
log.txt
$
$ git log --oneline
5aecefe first commit
接下来,钩子脚本如下:
$ # BARE repository
$ pwd
/home/git/repositories/gitlab-user/test_hook.git
$
$ cat hooks/post-receive
#!/bin/bash
clone_repo(){
#cloning a specifc branch on my_app dir
export GIT_WORK_TREE="/home/git/loging_at_post_receive"
}
cd_app(){
cd /home/git/loging_at_post_receive
}
update_file(){
date >> log.txt
echo "update_file"
}
commit_file(){
git --git-dir=.git add log.txt
git --git-dir=.git commit -m "commit from hook"
echo "commit_file"
}
push_it(){
git --git-dir=.git push origin master
echo "push_it"
}
while read oldrev newrev refname
do
if [ ${refname} = "refs/heads/master" ]
then
clone_repo
cd_app && update_file
commit_file && push_it
exit 0
fi
done
然后,git从PC推送到GitLab。 (请注意"远程:"区域)
$ # on PC
$ echo 1 >> README
$ git commit -am 'test'
$ git push origin master
[master 9aed67e] test
1 files changed, 1 insertions(+), 0 deletions(-)
Counting objects: 5, done.
Writing objects: 100% (3/3), 239 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: update_file
remote: [master 0ce599e] commit from hook
remote: 1 file changed, 1 insertion(+)
remote: commit_file
remote: To /home/git/repositories/gitlab-user/loging_at_post_receive.git/
remote: 5aecefe..0ce599e master -> master
remote: push_it
To https://gitlab-server/gitlab-user/test_hook.git
712a3d1..9aed67e master -> master
结果
$ # GIT_WORK_TREE
$ pwd
/home/git/loging_at_post_receive
$
$ git log --oneline
0ce599e commit from hook
5aecefe first commit
$
$ git log origin/master --oneline
0ce599e commit from hook
5aecefe first commit