gpt4 book ai didi

git - 实现自定义 gitlab 钩子(Hook)时出现问题

转载 作者:太空狗 更新时间:2023-10-29 14:18:41 30 4
gpt4 key购买 nike

我正在实现自定义 gitlab Hook 。当有人推送 master 时,我想更新一个特定的文件。然后提交此更改并将其推送到远程源。钩子(Hook)如下所示:

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

Hook 正在运行,但 cd_app && update_file 没有执行预期的操作(不更新 update_file)。我假设 cd_app 没有更改目录(通过 shell 打印)。

但是为了测试,当我在 if 检查之前设置 refname="refs/heads/master" 时,它工作正常!

找不到 gitlab 在哪里记录 custom_hooks。看来我错过了什么。你们能给我进一步的引用或找出我做错了什么吗?

最佳答案

这是我为我的系统做的。

首先,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

接下来hook脚本如下:

$ # 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

然后,从PC端git push到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

关于git - 实现自定义 gitlab 钩子(Hook)时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29868031/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com