解决Gitlab不显示Push Event和新分支

这个问题在Github上有不少讨论,为方便国际友人,可以看这里的英文版

换了Redis配置后,发现git push 后Gitlab页面上不再显示Push Event和新的分支了,这个问题在自己部署的版本上经常出现,主要原来还是配置不对,直接用包安装会比较方便。这里针对的是v8.7,下面是解决的过程,方便用需要的朋友。

下图就是你git push后gitlab所做的工作,gitlab-shell会触发回调,并往redis里塞任务,而sidekiq会从redis里取任务出来并更新页面上的东西。如果你push完代码更新了,页面没显示出来,多半是sidekiq没有从redis取到更新的任务,当然首先要保证sidekiq正常在后台工作。

首要任务

确保Gitlab各组件工作正常,执行以下命令检查,并按它的提示做好准备工作。

sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production  

回调

确保gitlab-shell里有相应的回调脚本

[root@host git]# ls gitlab-shell/hooks/                                                                            
post-receive  pre-receive  update  

其实gitlab把每个代码库里的回调文件夹都软链接到上述地址,确保你的代码库里的软链接正常,用以下方法确认:

[root@host git]# ll repositories/yourname/example.git/
total 48  
drwxrwxr-x   2 git git 4096 Jul 16 11:44 branches  
-rw-rw-r--   1 git git   66 Jul 16 11:44 config
-rw-rw-r--   1 git git   73 Jul 16 11:44 description
-rw-rw-r--   1 git git   23 Jul 16 11:44 HEAD
lrwxrwxrwx   1 git git   31 Jul 16 11:44 hooks -> /home/git/gitlab-shell/hooks  
drwxrwxr-x   2 git git 4096 Jul 16 11:44 hooks.old.1468640682  
drwxrwxr-x   2 git git 4096 Aug 26 18:10 info  
drwxrwxr-x 110 git git 4096 Aug 27 10:30 objects  
-rw-rw-r--   1 git git  337 Aug 26 18:10 packed-refs
drwxrwxr-x   4 git git 4096 Jul 16 11:44 refs  
-rw-rw-r--   1 git git 8673 Aug 27 10:30 update.log

若没有的话要运行gitlab-shell/bin/create-hooks命令来创建.

Gitlab在每次收到git push后都会调用post-receive来做相应的更新,要确认它是否有被正常调用,可以修改里面的内容,让它输出一些log信息,如下面所示:3

#!/usr/bin/env ruby  
# post-receive
# This file was placed here by GitLab. It makes sure that your pushed commits  
# will be processed properly.  

refs = ARGF.read  
key_id  = ENV['GL_ID']  
repo_path = Dir.pwd  

# reset GL_ID env since we already got its value  
ENV['GL_ID'] = nil  

require_relative '../lib/gitlab_custom_hook'  
require_relative '../lib/gitlab_post_receive'  

if GitlabPostReceive.new(repo_path, key_id, refs).exec &&  
    GitlabCustomHook.new.post_receive(refs, repo_path)
# Print some log here  
  open('/tmp/post.log','w') do |f|  
    f << "#{refs},#{key_id},#{repo_path}\n"  
  end  
  exit 0  
else  
  exit 1  
end  

现在git push后,你应该可以在/tmp/post.log里看到相应的log信息,确认无误后把这个文件还原成原来的样子。

Redis

gitlab-shell和sidekiq的设置文件分别在gitlab-shell/config.ymlgitlab/config/resque.yml。一定要确保这两个地方调用的redis实例是同一个,以下是通常情况用本地redis服务的配置:

[root@host git]# cat gitlab-shell/config.yml
---
user: git  
gitlab_url: http://gitlab.yourdomain.com/  
http_settings:  
  self_signed_cert: false
repos_path: "/home/git/repositories/"  
auth_file: "/home/git/.ssh/authorized_keys"  
redis:  
  bin: "/usr/local/bin/redis-cli"
  db: 0
  namespace: resque:gitlab
  socket: "/var/run/redis/redis.sock"
log_level: INFO  
audit_usernames: false  
[root@host git]# cat gitlab/config/resque.yml
development: redis://localhost:6379  
test: redis://localhost:6379  
production: unix:/var/run/redis/redis.sock  

你可以手动按以上配置给redis放一条任务进去:

/usr/bin/redis-cli -h localhost -p 6379 rpush 'resque:gitlab:queue:post_receive' '{"class":"PostReceive","args":["/repositoriespath/projectname.git/hooks","","","branch-or-tag-name","user-myusername"]}'

Sidekiq

确保sidekiq后台任务正常运行:

sudo -u git -H RAILS_ENV=production bin/background_jobs status  

让log输出,再push一下看sidekiq有无执行相应的任务:

tail -f gitlab/log/sidekiq.log  

正常情况下,git push后sidekiq应该用下面的输出:

2016-08-27T02:30:11.909Z 24223 TID-ouv325z6o PostReceive JID- INFO: start  
2016-08-27T02:30:12.081Z 24223 TID-ouv325z6o PostReceive JID- INFO: done: 0.171 sec  

只要确保以上步骤都正常,git push完再打开Gitlab页面就会有相应的信息更新。在Github上最常见的是两个地方的redis配置不对或sidekiq运行出错。

参考

Guimin Lin

Read more posts by this author.