连接到gitlab服务器
- gitlab仓库一般位于为:/var/opt/gitlab/git-data/repositories;
- repositories该目录下的文件夹对应gitlab的组;
- 组文件夹下的对应的是gitlab上相应的代码库;
- 进入xxxx.git目录,目录结构大致如下: hooks.old目录下是默认的hook脚本,将它备份后,新建hooks目录,软链接到一个新的目录/opt/gitlab/embedded/service/gitlab-shell/hooks,然后在这个目录下创建新的hooks脚本。
用R写的一个简单的hook脚本(脚本名称update)
- 用途:只有提交人在用户列表中,才能往branch1和branch2分支推送代码。
#!/opt/gitlab/embedded/bin/ruby --disable-gems
#!/usr/bin/env ruby
# Fix the PATH so that gitlab-shell can find git-upload-pack and friends.
ENV['PATH'] = '/opt/gitlab/bin:/opt/gitlab/embedded/bin:' + ENV['PATH']
# This file was placed here by GitLab. It makes sure that your pushed commits will be processed properly.
ref_name = ARGV[0]
old_value = ARGV[1]
new_value = ARGV[2]
repo_path = Dir.pwd
key_id = ENV.delete('GL_ID')
user = `git log -1 --pretty=format:'%an' #{new_value}`
user_list = [ "xiaoming","xiaohong","xiaoxue"]
require_relative '/opt/gitlab/embedded/service/gitlab-shell/lib/gitlab_custom_hook'
if GitlabCustomHook.new(repo_path, key_id).update(ref_name, old_value, new_value)
puts "ref_name #{ref_name}."
if ref_name == 'refs/heads/branch1'
puts "user #{user}."
if user_list.include?user
exit 0
else
puts "branch #{ref_name} is locked . if you wanna try ? please contact cmo."
exit 1
end
elsif ref_name == 'refs/heads/branch2'
puts "user #{user}."
if user_list.include?user
exit 0
else
puts "branch #{ref_name} is locked . if you wanna try ? please contact cmo."
exit 1
end
else
exit 0
end
end
详见:https://docs.gitlab.com/ee/administration/custom_hooks.html