Custom gitlab hook implementation issue
I am implementing a custom gitlab-hook. While someone clicks on the master, I want to update a specific file. Then commit this change and push it to the remote origin. The hook looks like this:
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
The hook works, but cd_app && update_file
does not do what it is supposed to do (without updating update_file). I am assuming cd_app
not changing directory (via shell).
But for testing purposes, when I installed refname="refs/heads/master"
just before checking out if
, it works great!
Couldn't find where gitlab is registering for custom_hooks. And it seems that I am missing something. Can you guys give me an additional link or determine what I am doing wrong?
source to share
This is how I did it for my system.
First, the GIT_WORK_TREE looks like this:
$ # 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
Next, the hook script looks like this:
$ # 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
Then git push from PC to GitLab. (note the "remote:" area)
$ # 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
As a result
$ # 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
source to share