![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204210036319.png#alt=image-20220421003605276)

### 介绍

gitlab官方文档：https://docs.gitlab.com/14.10/

GitLab 是一个用于仓库管理系统的开源项目，是由GitLabInc.开发，
使用MIT许可证的基于网络的Git仓库管理工具，且具有wiki和issue跟踪功能。
使用Git作为代码管理工具，并在此基础上搭建起来的web服务。

清华大学源
[https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/](https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/)

虚拟机至少 4G 内存， CPU核心2 个以上，否则初始化时会内存 溢出而失败，或者 UNicon 服务起不来

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204210037911.png#alt=)

### 实验环境
| 主机名 | ip | 备注 |
| :---: | :---: | :---: |
| test | 192.168.188.60 | git、nginx |
| gitlab | 192.168.188.61 | gitlab |


### 安装

#### 1.下载到/opt/

https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.6.0-ce.0.el7.x86_64.rpm（这次实验用14.6.0版本）

去清华源下载gitlab-ce-13.3.8版本https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-13.3.8-ce.0.el7.x86_64.rpm

```shell
[root@gitlab ~]# cd /opt/
```

#### 2.安装依赖

```shell
[root@gitlab opt]# yum install -y curl policycoreutils-python openssh-server perl
```

#### 3.安装

```shell
[root@gitlab opt]# rpm -ivh gitlab-ce-14.6.0-ce.0.el7.x86_64.rpm
或者
[root@gitlab opt]# yum install -y gitlab-ce-14.6.0-ce.0.el7.x86_64.rpm
# 成功如下图出现一个狐狸的图案，还会告诉你要干什么
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204210051209.png#alt=)

#### 4.修改配置文件

```shell
[root@gitlab opt]# cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.bak
[root@gitlab opt]# vim /etc/gitlab/gitlab.rb
# 修改如下
改：32 external_url 'http://gitlab.example.com'
为：32 external_url 'http://192.168.188.61'   # 改为本机地址

# 刷新配置
[root@gitlab opt]# sudo gitlab-ctl reconfigure
```

#### 5.常用命令

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204210836657.png#alt=image-20220421083602734)

```shell
# 安装好后默认启动
[root@gitlab opt]# gitlab-ctl status
run: alertmanager: (pid 2955) 26748s; run: log: (pid 2700) 26807s
run: gitaly: (pid 2856) 26749s; run: log: (pid 2234) 26897s
run: gitlab-exporter: (pid 2843) 26750s; run: log: (pid 2639) 26825s
run: gitlab-workhorse: (pid 2820) 26751s; run: log: (pid 2435) 26849s
run: grafana: (pid 2972) 26747s; run: log: (pid 2795) 26763s
run: logrotate: (pid 2487) 26838s; run: log: (pid 2506) 26837s
run: nginx: (pid 2466) 26844s; run: log: (pid 2478) 26841s
run: node-exporter: (pid 2829) 26750s; run: log: (pid 2628) 26830s
run: postgres-exporter: (pid 2964) 26747s; run: log: (pid 2731) 26799s
run: postgresql: (pid 2243) 26894s; run: log: (pid 2297) 26891s
run: prometheus: (pid 2863) 26749s; run: log: (pid 2683) 26812s
run: puma: (pid 2385) 26862s; run: log: (pid 2393) 26861s
run: redis: (pid 2075) 26906s; run: log: (pid 2092) 26903s
run: redis-exporter: (pid 2851) 26749s; run: log: (pid 2664) 26817s
run: sidekiq: (pid 2402) 26856s; run: log: (pid 2415) 26855s

# 检查端口
[root@gitlab opt]# netstat -lntp   # 可以找到与上面对应的进程与端口号
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204210844692.png#alt=)

```shell
# 停止gitlab集成的服务中的某一服务
[root@gitlab opt]# gitlab-ctl stop nginx
[root@gitlab opt]# gitlab-ctl status

# 启动gitlab集成的服务中的某一服务
[root@gitlab opt]# gitlab-ctl start nginx
[root@gitlab opt]# gitlab-ctl status

# 重启服务
[root@gitlab opt]# gitlab-ctl restart
```

```shell
# 查看日志
[root@gitlab opt]# gitlab-ctl tail

# 查看某个服务的日志
[root@gitlab opt]# gitlab-ctl tail nginx
```

**注意：**

**1、端口冲突 ： unicorn 是反向代理服务， 默认端口 8080 ，不要占用，它如果无法启动，则 应用会报 502 错误**
**2、配置不足： 内存不足，导致  gitlab-ctl  reconfigure 时无法通过**
**3、配置不足： CPU核心数量不足，导致 unicorn 无法启动，报如下错误**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222012194.png#alt=image-20220422201250918)

### 使用

#### 1.打开页面

**浏览器输入IP地址即可，如下图**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222015164.png#alt=)

#### 2.设置密码

**需要设置八位以上的密码**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222017237.png#alt=)

#### 3.登陆

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222019154.png#alt=)



<font color='red'>新版本不需要创建密码，在成功启动时，会生成/etc/gitlab/initial_root_password登录密码文件</font>



#### 4.更改语言

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222023321.png#alt=)

#### 5.注册限制

**生产中，当新来一个同事或者有人忘记密码直接新创一个账户，是不允许的**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222026022.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222029914.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222031390.png#alt=)

#### 6.新建项目

**新建项目有多处可以新建，如：新建gitlab刚进去的欢迎页面、仪表板和管理中心等**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222310680.png#alt=)

**点击新建项目过后会出现跟gitee、github一样的操作，回到test机子上操作**

**操作推送现有的 Git 仓库的步骤**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222310019.png#alt=)

```shell
# 如果是新的本地仓库，需要git init初始化，不然本地找不到master分支之类的
# 因为之前在test机子操作过，所以可以不用初始化直接配置推送
[root@test ~]# cd /opt/
[root@test opt]# ls
shy  yum
[root@test opt]# cd shy/
[root@test shy]# ls
dev.php  index.html  install.sh  monitor  README.md  test.sh  test.txt

[root@test shy]# git remote rename origin old-origin   # 因为本地仓库因为有这个源，所以不用操作
error: 不能重命名配置小节 'remote.origin' 到 'remote.old-origin'

[root@test shy]# git remote add origin http://192.168.188.61/root/uplooking.git   # 配置远程git版本库
[root@test shy]# git push -u origin --all   # 推送上去
Username for 'http://192.168.188.61': root   # 因为没有做免密需要账号密码
Password for 'http://root@192.168.188.61': 
Counting objects: 29, done.
Compressing objects: 100% (20/20), done.
Writing objects: 100% (29/29), 3.37 KiB | 0 bytes/s, done.
Total 29 (delta 6), reused 12 (delta 1)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://192.168.188.61/root/uplooking/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
remote: To create a merge request for test, visit:
remote:   http://192.168.188.61/root/uplooking/-/merge_requests/new?merge_request%5Bsource_branch%5D=test
remote: 
To http://192.168.188.61/root/uplooking.git
 * [new branch]      dev -> dev
 * [new branch]      master -> master
 * [new branch]      test -> test
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
分支 master 设置为跟踪来自 origin 的远程分支 master。
分支 test 设置为跟踪来自 origin 的远程分支 test。
[root@test shy]# git push -u origin --tags   # 标签提交
Username for 'http://192.168.188.61': root
Password for 'http://root@192.168.188.61': 
fatal: Authentication failed for 'http://192.168.188.61/root/uplooking.git/'
[root@test shy]# git push -u origin --tags
Username for 'http://192.168.188.61': root
Password for 'http://root@192.168.188.61': 
Counting objects: 1, done.
Writing objects: 100% (1/1), 165 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To http://192.168.188.61/root/uplooking.git
 * [new tag]         v0.1 -> v0.1
 * [new tag]         v0.6 -> v0.6
 * [new tag]         v1.0 -> v1.0
```

**回到gitlab的web页面刷新，会发现分支和标签都会显示**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222321502.png#alt=)

#### 7.项目导入(迁移)

导入一个gitee上的web项目https://gitee.com/kangjie1209/monitor，复制https的克隆链接

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222341038.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222346700.png#alt=)

#### 8.新建用户与授权

**Gitlab权限管理**
**Gitlab用户在组中有五种权限：Guest、Reporter、Developer、Master、Owner**

**Guest：可以创建issue、发表评论，不能读写版本库**
**Reporter：可以克隆代码，不能提交，QA、PM可以赋予这个权限**
**Developer：可以克隆代码、开发、提交、push，RD可以赋予这个权限**
**Master：可以创建项目、添加tag、保护分支、添加项目成员、编辑项目，核心RD负责人可以赋予这个权限**
**Owner：可以设置项目访问权限 - Visibility Level、删除项目、迁移项目、管理组成员，开发组leader可以赋予这个权限**
**Gitlab中的组和项目有三种访问权限：Private、Internal、Public**

**Private：只有组成员才能看到**
**Internal：只要登录的用户就能看到**
**Public：所有人都能看到**
**开源项目和组设置的是Internal**

##### 新建用户

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204222359856.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230003363.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230004399.png#alt=)

##### 确认密码

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230008260.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230008639.png#alt=)

##### 登录

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230010175.png#alt=)

**此时退出登录普通账户会发现是英文需要更改，没有管理中心，只能创建项目，之前创建、导入的项目都没有，所以需要回到管理员用户给予授权**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230013556.png#alt=)

##### 授权

**回到管理账户对项目授权**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230018989.png#alt=)

**回到被授权的用户，会发现出现运行读写的项目以及该项目的身份**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230020921.png#alt=)

**对项目的修改，申请合并请求**

修改了readme.md，申请合并，因为不是项目拥有者，所以需要通过管理审核

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230025587.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230026841.png#alt=)

**回到管理员账户**

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230028077.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230029416.png#alt=)

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230031496.png#alt=)

在没有管理员同意审核之前是看不到修改，管理员审核过后同意就会看到，如下图

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230033920.png#alt=)





案例01：项目与组

案例1：验证项目是不是隶属于该组的成员才可以看见

1.首先创建dev、ops两个组

2.然后基于组创建两个项目

3.最后创建用户、为用户分配组、以及组中的权限



案例02：主程序员与普通开发

案例2：验证主程序员和开发者身份的权限

1.主程序员：能够对master、dev分支进行操作：

2.测试开发者：仅能对非master分支进行操作











#### 9.拉取仓库代码进行部署

```shell
# 复制gitlab中monitor的http克隆链接进行拉取
# 需要有nginx
# 在test机子操作
[root@test opt]# git clone http://192.168.188.61/root/monitor.git
正克隆到 'monitor'...
Username for 'http://192.168.188.61': root
Password for 'http://root@192.168.188.61': 
remote: Enumerating objects: 439, done.
remote: Counting objects: 100% (439/439), done.
remote: Compressing objects: 100% (375/375), done.
remote: Total 439 (delta 56), reused 434 (delta 54), pack-reused 0
接收对象中: 100% (439/439), 8.78 MiB | 0 bytes/s, done.
处理 delta 中: 100% (56/56), done.
[root@test opt]# ls
monitor  shy  yum
[root@test opt]# grep -Ev "#|^$" /usr/local/nginx/conf/nginx.conf
worker_processes  1;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /opt/monitor;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@test opt]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@test opt]# nginx -s reload

# 浏览器打开页面验证，输入test机子的ip
```

![](https://figure-bed-1304788733.cos.ap-guangzhou.myqcloud.com/typora/202204230102462.png#alt=)
