GitLab学习笔记

安装gitlab的docker

https://www.jianshu.com/p/080a962c35b6

常用操作命令

启动

1
2
3
4
5
6
7
8
9
sudo docker run --detach \
--hostname gitlab.example.com \
--publish 443:443 --publish 80:80 --publish 222:222 \
--name gitlab \
--restart always \
--volume $GITLAB_HOME/config:/etc/gitlab \
--volume $GITLAB_HOME/logs:/var/log/gitlab \
--volume $GITLAB_HOME/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest

账号密码

root
Zcx988…

zhouchangju
12345678

注意:需要设置这个才能生效,我之前就是这些配置被还原了,导致账号密码正确也登不进去:

1
2
3
4
5
6
7
# 配置http协议所使用的访问地址,不加端口号默认为80
external_url 'http://192.168.199.231'

# 配置ssh协议所使用的访问地址和端口
gitlab_rails['gitlab_ssh_host'] = '192.168.199.231'
gitlab_rails['gitlab_shell_ssh_port'] = 222 # 此端口是run时22端口映射的222端口
:wq #保存配置文件并退出

常用API测试命令

创建group

1
2
curl --header "PRIVATE-TOKEN:oqCje7fw7P7U_m3whM9j" "http://192.168.56.101/api/v4/groups" -d "name=hexin&path=hexinpath"
curl "http://192.168.56.101/api/v4/groups" -d "private_token=oqCje7fw7P7U_m3whM9j&name=hexin&path=hexinpath"

创建用户

1
curl "http://192.168.56.101/api/v4/users" -d "private_token=this_is_private_token&email=test1@xxx.com&username=test1&name=test1&password=12345678&skip_confirmation=1"

查询用户

1
curl "http://192.168.56.101/api/v4/users?private_token=this_is_private_token&username=test1"

删除用户

1
curl -X DELETE "http://192.168.56.101/api/v4/users/39?private_token=this_is_private_token"

Block用户

之所以不用deactivate用户,是因为如果用户在过去180天之内有操作过,就不能执行deactivate这个动作。

1
curl "http://192.168.56.101/api/v4/users/39/block" -d "private_token=this_is_private_token"

解除block:

1
curl "http://192.168.56.101/api/v4/users/39/unblock" -d "private_token=this_is_private_token"

查询单个project

1
curl "http://192.168.56.101/api/v4/projects/3?private_token=this_is_private_token"

常见报错

Whoops, GitLab is taking too much time to respond.

可能是内存不足或者尚未启动完成:

https://blog.csdn.net/vtopqx/article/details/80883127

修改GitLab的页面内容

注意:修改内容后,必须重启gitlab容器才行

源码

https://github.com/gitlabhq/gitlabhq

删除用户

/opt/gitlab/embedded/service/gitlab-rails/app/services/users/destroy_service.rb

HAML模板

GitLab的前端用的是这个模板语言。

github地址:https://github.com/haml/haml/

说明文档:http://haml.info/docs/yardoc/

使用案例:https://blog.csdn.net/lzqustc/article/details/83335447

修改group的setting的展示内容

以docker安装的GitLab为例,其前端程序,位于/opt/gitlab/embedded/service/gitlab-rails/app/views这个目录下。

比如我要修改group页面的内容,给其添加一个“设置部门信息”的超链接,就可以修改这个文件:

/opt/gitlab/embedded/service/gitlab-rails/app/views/layouts/nav/sidebar/_group.html.haml

1
2
3
4
5
6
7
= nav_link(controller: :ci_cd) do
= link_to group_settings_ci_cd_path(@group), title: _('CI / CD') do
%span
= _('CI / CD')
%a{:href => 'http://datav.iwencai.com/platform/config.html'} 设置部门信息
- if Feature.enabled?(:group_level_integrations, @group)

注意缩进,如果缩进不对,会导致页面无法正常解析,进而报500错误。

问题

删除用户时,自动删除代码库是物理删除还是逻辑删除?

删除前:

/var/opt/gitlab/git-data/repositories/@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a.git

删除后(只有个目录名称):

/var/opt/gitlab/git-data/repositories/@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a+4+deleted.git

我测试了下,删除gitlab用户时,会先将用户的代码库移动到临时目录,如果5分钟内没有执行其他操作,则会自动删掉临时目录中的代码库。因此lijianqing这个代码库在服务器上是找不回来了,只能看看其他同事电脑上有没有clone过这个代码库,或者从线上/测试环境把代码拿下来重新创建代码库。

后续可以修改下人员离职时对gitlab账号的处理方案,将删除改为block,这样就不会导致用户的代码库被删除了。

文件名命名规则是怎么样的?

如何找到某个被删除的用户的代码库?

根据删除时间查找带有delete.git字样的文件夹

find / -name “delete.git“ -type d|xargs ls –full-time|grep “2020-08-13”

如何通过该文件恢复代码库?

这个文件其实就是.git文件

https://zhuanlan.zhihu.com/p/45289391

后续该怎么解决人员离职代码库被删除的问题?

查看下API,有没有不删除的方法。

或者不删除用户,只是冻结用户。

Git和GitLab是怎么整合的

可以有两种方式:独立安装和Omnibus安装

如果是打包安装,那么git数据一般位于类似这样的目录下:

/var/opt/gitlab/git-data/repositories//.git

什么是Gitaly

我看gitlab的hooks示例文件里面有提到这个,说hook已经迁移到了Gitaly:

GitLab的配置文件在哪里?

gitlab.rb

【重要】GitLab的hooks可以拦截哪些操作?

可以查看这个file类型的服务端系统hooks的说明文档:

https://github.com/gitlabhq/gitlabhq/blob/master/doc/administration/file_hooks.md

hooks文件一般位于这个目录下:

/opt/gitlab/embedded/service/gitlab-rails/file_hooks

【重要】Git的hooks可以拦截哪些操作?

Git的钩子可以获取哪些参数?

根据Server-side Hooks这个链接的内容,可以知道post-receive和pre-receive一样在执行前会得到3个参数:

The script takes no parameters, but each ref that is being pushed is passed to the script on a separate line on standard input in the following format:


b6b36c697eb2d24302f89aa22d9170dfe609855b 85baa88c22b52ddd24d71f05db31f4e46d579095 refs/heads/master

所以既然知道了ref-name和相应的hash值,就可以根据git相应的命令得到对应的信息.如git log refs/heads/master.

Git的服务端hooks可以全局生效么?

暂未找到,但是可以尝试通过gitlab创建代码库的时候,通过GitLab的钩子自动添加git hooks的超链接。这个得尝试下先。

如何实现SVN目录级别的精细化权限管理?

Group->部门

代码库->项目

分支->目录

我们目前SVN的权限想要一键迁移过去,需要做一些规整才行。

如何获取用户的IP

直接hooks脚本里面获取

可以通过请求头获取到,以PHP为例,$_SERVER['SSH_CLIENT']就包含了用户的IP信息。

通过Nginx转发所有请求

如何自动给新人创建分组?

沿用之前SVN创建分组的设计,在CRM中设置每个部门对应的GitLab的用户分组(注意是用户分组,不是项目分组)信息即可。

实习生、试用期:developer,能看能写不能合并到主干,也就是不能发

主管:master,都可以

核心代码单独创建分组和代码库,单独赋予权限。

如何将权限一键SVN迁移到GitLab

根据现在的authz文件,一键同步。

根据现在的SVN的用户分组信息,给个配置文件吧

为什么推荐拆分为小项目?

1、响应速度快,拉取代码、合并代码、发布、回滚都方便

2、权限控制更加精细

3、更符合模块化、组件化的思路,降低项目耦合度

GitLab的API

怎么鉴权

通过Access Token进行鉴权。

用户

https://docs.gitlab.com/ee/api/users.html

如何在创建用户的时候指定其所属的组?

调用group member相关的API:

https://docs.gitlab.com/ee/api/members.html

Group的权限可以有多细

可以限制只有该组的维护人员可以创建项目

subgroup是什么概念

对应的API:https://docs.gitlab.com/ee/api/groups.html

关于代码库的访问权限

http://192.168.56.101/help/public_access/public_access

类似编程里面的作用域一样,父级的访问范围必须大于子级的访问范围:

Admin users cannot create subgroups or projects with higher visibility level than that of the parent group.

所以你是不能在一个private的group里面创建public的project的。

资料

gitlab的docker

https://hub.docker.com/r/gitlab/gitlab-ce

https://docs.gitlab.com/omnibus/docker/

webhooks

https://docs.gitlab.com/ee/user/project/integrations/webhooks.html

git的hooks

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#Server-Side-Hooks

如何给gitlab创建服务端的git的hook:

https://docs.gitlab.com/ee/administration/server_hooks.html#server-hooks-core-only

GitLab是支持设置服务端全局hook的,详见这个文档

比如我用Omnibus方式安装,那么hooks就在docker内部的这个目录下:

/opt/gitlab/embedded/service/gitlab-shell/hooks

GitLab的用户权限机制

https://www.cnblogs.com/zangxueyuan/p/9222014.html

GitLab的API

https://www.cnblogs.com/Javame/p/9363931.html