gitlab 企业级私有仓库搭建

一、Gitlab基本概述

1.1 什么是gitlab

Gitlab 是一个开源分布式的版本控制系统。 Ruby语言开发完成。
Gitlab 主要实现的功能:
1.管理项目源代码。
2.对源代码进行版本控制。
Gitlab 的优势:
1.开源免费,搭建简单、维护成本较低、适合中小型公司。
2.权限管理,能实现代码对部分人可见,确保项目的安全性
3.离线同步,保证我们不在实时依赖网络环境进行代码提交。

1.2 Gitlab与Github区别

1.相同点: 两者都是提供代码托管服务,在很大程度上GitLab是仿照 GitHub来做的。
2.不同点: github创建私有仓库收费、gitlab创建私有仓库免费。
从安全方面来看,公司不希望员工获取到全部的代码,这个时候 GitLab 是最佳的选择。
但对于开源项目而言,GitHub 依然是代码托管的首选平台。

1.3 Gitlab服务构成

gitlab 作为一个分布式代码托管软件,它必然有相关的服务组件去支持这个庞大系统
在这里插入图片描述
在这里插入图片描述

  • nginx,作为gitlab的proxy代理,处理http/https以及静态资源访问请求。
  • gitlab-workhorse,用于处理文件上传和下载。
  • gitlab-shell,用于处理git clone、git pull、gitpush。
  • Logrotate,用于处理日志的切割和打包等操作。
  • Postgresql,用于保存所有的gitlab数据相关信息。
  • Redis,用于缓存数据库的信息,加快前台访问速度,以及交互读写

相关命令

gitlab-ctl start #启动全部服务
gitlab-ctl restart#重启全部服务
gitlab-ctl stop #停止全部服务
gitlab-ctl restart nginx #重启单个服务,如重启nginx
gitlab-ctl status #查看服务状态
gitlab-ctl reconfigure #使配置文件生效
gitlab-ctl show-config #验证配置文件
gitlab-ctl uninstall #删除gitlab(保留数据)
gitlab-ctl cleanse #删除所有数据,从新开始
gitlab-ctl tail <service name>查看服务的日志
gitlab-ctl tail nginx  #如查看gitlab下nginx日志
gitlab-rails console  #进入控制台

二、gitlab安装及配置

2.1 安装依赖软件

1.关闭防火墙

[root@gitlab ~]# setenforce 0
setenforce: SELinux is disabled
[root@gitlab ~]# systemctl stop firewalld
[root@gitlab ~]# systemctl disable firewalld

2.安装 gitlab 所需依赖软件

[root@gitlab ~]# yum install -y curl openssh-server postfix wget

2.2 安装 Gitlab 组件

1.下载 Gitlab

[root@gitlab ~]# wget https://mirror.tuna.tsinghua.edu.cn/gitlabce/yum/el7/gitlab-ce-12.3.9-
ce.0.el7.x86_64.rpm

2.使用 yum 安装 Gitlab

[root@gitlab ~]# uim localinstall gitlab-ce-12.3.9-ce.0.el7.x86_64.rpm 

2.3 配置 Gitlab 域名

后期通过域名访问 gitlab 服务

[root@gitlab ~]# vim /etc/gitlab/gitlab.rb 

external_url 'http://gitlab.bertwu.com'

2.4 默认安装目录分析

gitlab组件日志路径:/var/log/gitlab
gitlab配置路径:/etc/gitlab/  路径下有gitlab.rb配置文件
应用代码和组件依赖程序:/opt/gitlab
各个组件存储路径: /var/opt/gitlab/
仓库默认存储路径   /var/opt/gitlab/git-data/repositories
版本文件备份路径:/var/opt/gitlab/backups/
nginx安装路径:/var/opt/gitlab/nginx/
redis安装路径:/var/opt/gitlab/redis

2.5 配置 Gitlab 邮箱

1.在账号注册时,需要使用邮件验证。
2.后期修改密码时,需要通过邮件修改。

### Email Settings
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = '[email protected]' # 发件邮箱
gitlab_rails['gitlab_email_display_name'] = 'Bertwu-Gitlab' # 发件人显示名称

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "[email protected]" #发件人邮箱
gitlab_rails['smtp_password'] = "mqfiderkgrowhhhf" #发件人邮箱客户端授权码
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false                                                                                 

2.5 关闭 Gitlab 组件

由于 Gitlab 核心功能是代码托管,所以有些额外的组件比较浪费资源,所以可以考虑关闭。

prometheus['enable'] = false
prometheus['monitor_kubernetes'] = false
alertmanager['enable'] = false  
node_exporter['enable'] = false 
redis_exporter['enable'] = false 
postgres_exporter['enable'] = false
gitlab_exporter['probe_sidekiq'] = false
prometheus_monitoring['enable'] = false
grafana['enable'] = false  

2.6 初始化 Gitlab 组件

第一次需要初始化 gitlab 服务,后续如果对 gitlab
配置文件进行修改,也需要执行初始化进行重新配置。

[root@gitlab ~]# gitlab-ctl reconfigure
[root@gitlab ~]# gitlab-ctl status
[root@gitlab ~]# gitlab-ctl stop
[root@gitlab ~]# gitlab-ctl start

2.7 验证 Gitlab 组件

1.重新初始化后通过 gitlab-rails 检查 gitlab 邮箱是否可正常使用。

[root@gitlab ~]# gitlab-rails console # 打开控制台
# 发送邮件
irb(main):003:0> Notify.test_email('[email protected]','test','gitlab').deliver_now

2.登录gitlab,需要配置root管理员密码,再重新登录。

2.8 汉化 Gitlab 组件

1.使用 git 命令下载汉化补丁包 gitlab中文汉化包

# wget https://gitlab.com/xhang/gitlab/-/archive/v12.3.0-zh/gitlab-v12.3.0-zh.zip
[root@gitlab ~]# unzip gitlab-v12.3.0-zh.zip # 解压

2.停止 gitlab,进行中文汉化。

[root@gitlab ~]# gitlab-ctl stop
[root@gitlab ~]# \cp -r gitlab-v12.3.0-zh/* /opt/gitlab/embedded/service/gitlab-rails/

3.重启gitlab

[root@gitlab ~]# gitlab-ctl reconfigur
[root@gitlab ~]# gitlab-ctl restart

4.点击settings–> Preferences–>Localization–>选择简体中文,完成整体汉化
在这里插入图片描述

三、Gitlab用户与组

3.1 用户与组及仓库的关系

Gitlab 中的用户、用户组、项目仓库之间的关系是什么样的?

  • 如果使用user创建一个仓库,那么这个用户就是这个仓库的owner
  • 如果使用group创建一个仓库,那这个组下面添加的所有用户就是这个仓库的owner

3.2 用户与组及仓库案例1

案例1:验证项目是不是隶属于该组的成员才可以看见
1.首先创建dev、ops两个组
2.然后基于组创建两个项目
3.最后创建用户、为用户分配组、以及组中的身份

创建组示例:
在这里插入图片描述
创建具体项目示例:
将项目关联到具体的组
在这里插入图片描述
创建具体用户,将用户加入具体的组
在这里插入图片描述

3.3 验证主程序员和开发者身份的权限

在这里插入图片描述
可以看到dev组两个用户bertwu是主程序猿,jack是开发人猿,这两个人对alipy项目的权限有何不同?
1.主程序员:能够对 master、dev 分支进行操作;[通过命令测试]
2.开发者:仅能对非 master 分支进行操作;[通过命令演示效果]
2.1 关闭分支保护机制;
2.2 提交到非Master分支上;

1.bertwu用户登录到gitlab,按照文档步骤克隆项目,创建README.md文件后推送到远程仓库

$ git config --global user.name "bertwu"
$ git config --global user.email "[email protected]"

$ git clone http://gitlab.bertwu.com/dev/alipay.git # 克隆项目

$ cd alipay/
$ git remote -v # 查看仓库
origin  http://gitlab.bertwu.com/dev/alipay.git (fetch)
origin  http://gitlab.bertwu.com/dev/alipay.git (push)

$ vim README.md # 编写程序
我是bertwu用户,主程序猿

$ git add .
$ git commit README.md -m 'bertwu add README file' # 提交到本地仓库
$ git push -u origin master # 推送到远程仓库

在这里插入图片描述
2.jack用户clone最新的项目代码到本地,修改文件后再次提交

[root@gitlab ~]# git clone http://gitlab.bertwu.com/dev/alipay.git
[root@gitlab alipay]# git remote -v
origin	http://gitlab.bertwu.com/dev/alipay.git (fetch)
origin	http://gitlab.bertwu.com/dev/alipay.git (push)

[root@gitlab alipay]# vim README.md 
我是jack用户,开发人猿

[root@gitlab alipay]# git config --global user.email '[email protected]'
[root@gitlab alipay]# git config --global user.name 'jack'


# 推送到远程仓库master时报错,没有权限
[root@gitlab alipay]# git push -u origin master
Username for 'http://gitlab.bertwu.com': jack
Password for 'http://[email protected]': 
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To http://gitlab.bertwu.com/dev/alipay.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'http://gitlab.bertwu.com/dev/alipay.git'

GitLab: You are not allowed to push code to protected branches on this
这是因为,开发人员是没有权限向master上提交任何数据。

可以向其他分支上提交。

[root@gitlab alipay]# git branch dev
[root@gitlab alipay]# git checkout dev
[root@gitlab alipay]# git push -u origin dev

3.4 模拟日常开发者如何更新代码至 master 分支

1.首先创建一个 dev 分支,然后编写代码提交至远程 gitlab
2.登陆 gitlab 服务端,像主程序员申请合并请求,请求 dev 合并master
3.登陆主程序员账户查看合并信息,并确认合并操作
4.登陆 gitlab 查看该项目是否已经是 master 与dev 分支合并后的效果

1.jack用户切换到dev分支,继续开发代码,然后提交到dev分支

[root@gitlab alipay]# git branch
* dev
  master
[root@gitlab alipay]# echo "denglu" > login.py 
[root@gitlab alipay]# git add .
[root@gitlab alipay]# git commit -m "jack用户开发了登录模块"
[root@gitlab alipay]# git push -u origin dev

2.用户jack登录到gitlab后台
在这里插入图片描述
可以看到,dev分支有刚提交的login.py ,如果想要合并到master,点击右上角合并请求
在这里插入图片描述
3.当bertwu主程序员登录后台时,就能看到你提交的合并请求
在这里插入图片描述
4.主程序员审核之后感觉不错,就会同意合并请求。

四、gitlab操作

4.1 已有的仓库如何关联远程仓库

1.以超级管理员身份在gitlab上创建shopping项目远程仓库,用于本地项目的关联。
2.在本地创建shopping文件加,初始化为仓库

[root@gitlab ~]# mkdir shopping
[root@gitlab ~]# cd shopping/
[root@gitlab shopping]# git init
[root@gitlab shopping]# git config --global user.name "bertwu"
[root@gitlab shopping]# git config --global user.email "[email protected]"
[root@gitlab shopping]# vim README.md
1.支付模块
2.登录模块
3.用户模块
[root@gitlab shopping]# git add .
[root@gitlab shopping]# git commit README.md -m '开发文档'

[root@gitlab shopping]# git remote add origin http://gitlab.bertwu.com/dev/shopping.g # 关联远程仓库

[root@gitlab shopping]# git push -u origin master # 推送

3.登录bertwu用户,后台查看
在这里插入图片描述
如果出现输入密码提示,则说明该主机与gitlab服务器没有配置免秘钥。那么需要在当前主机上执行ssh-keygen生成秘钥,然后将其公钥放置gitlab服务器对应的用户下。

4.2 新成员加入操作流程

如果有新成员需要加入该项目该怎么办?
1.用gitlab超级管理员root先给新成员创建用户
2.将成员加入组,此时该用户就能看到对应的项目
3.使用http方式,输入用户名与密码,测试能否获取项目代码
3.使用ssh方式,添加对应公钥信息,测试能否获取项目代码

若原有的仓库是http 现在想换为ssh方式,如下


[root@manager shopping]# git remote -v # 查看仓库,为http
origin	http://gitlab.bertwu.com/dev/shopping.git (fetch)
origin	http://gitlab.bertwu.com/dev/shopping.git (push)

[root@manager shopping]# git remote remove origin # 删除
[root@manager shopping]# git remote -v


[root@manager shopping]# git remote add origin git@gitlab.bertwu.com:dev/shopping.git # 重新添加ssh的仓库
[
root@manager shopping]# git remote -v # 再次查看
origin	git@gitlab.bertwu.com:dev/shopping.git (fetch)
origin	git@gitlab.bertwu.com:dev/shopping.git (push)

如果在gitlab上对应用户下已经添加了对应公钥,这时候就可以免密推送了

[root@manager shopping]# git push -u origin dev

五、gitlab日常维护

5.1 备份

1.修改默认存放备份站点目录,然后进行重新加载配置文件

[root@gitlab ~]# vim /etc/gitlab/gitlab.rb 
gitlab_rails['backup_path'] = "/data/gitlab/backups" # 自定义备份路径

gitlab_rails['backup_keep_time'] = 604800 # 保留7[root@gitlab ~]# mkdir /data/gitlab/backups -p
   

2.手动执行备份命令,会将备份的结果存储至/data/gitlab/backups 目录中

[root@gitlab backups]# gitlab-rake gitlab:backup:create

3.也可以将备份命令写入定时任务每天进行自动备份

[root@gitlab backups]# crontab -l
00 03 * * * /usr/bin/gitlab-rake gitlab:backup:create

5.2 Gitlab数据恢复

当误删除项目,或者误删除用户等数据时候,如果之前有备份,那么可以做恢复操作。
1.停止数据写入服务

[root@gitlab ~]#  gitlab-ctl stop unicorn
[root@gitlab ~]#  gitlab-ctl stop sidekiq

2.通过 gitlab-rake 命令进行恢复,恢复时需要指定此前备份的名称。(但不需要写名称的.tar后缀)

[root@gitlab ~]#  gitlab-rake gitlab:backup:restore BACKUP=1634715076_2021_10_20_12.3.0

3.重启gitlab,检测是否 gitlab 是否恢复。

[root@gitlab ~]#  gitlab-ctl restart

5.3 Gitlab迁移升级

一般情况,gitlab一经使用,则不会升级;除非做迁移,例如从本地环境迁移到阿里云时,可以顺便升级;

迁移与升级:

  1. 一般情况下,迁移会迁移两个文件,/etc/gitlab/gitlab.rb 和backups下的备份文件。
  2. 在新节点需要安装相同的版本,先恢复数据
  3. 数据恢复后先升级到本版本的最后一个版本,然后再升级到下个版本,例如(12.3 —> 12.9.9 --> 13),不能跨版本升级。

1.如果从12 最后一个版本升级到13 ,会报错

[root@gitlab ~]# yum localinstall gitlabce-13.0.10-ce.0.el7.x86_64.rpm -y
#会提示报错:
Running transaction
* gitlab_monitor['enable'] has been
deprecated since 12.3 and was removed in
13.0. Use gitlab_exporter['enable']
instead.

2.只需要根据提示编辑 /etc/gitlab.rb 配置文件

#提示什么错变更什么即可
gitlab_monitor['enable'] =false

gitlab_exporter['enable'] = false

3.修改配置后重新初始化,然后在进行升级

[root@gitlab ~]# gitlab-ctl reconfigure
[root@gitlab ~]# yum localinstall gitlabce-13.0.10-ce.0.el7.x86_64.rpm -y

5.5 Gitlab安全

将访问网页的 HTTP 协议升级到 HTTPS协议,保证数据安全;
1.阿里云上申请下载nginx证书到/ssh_key文件夹

[root@gitlab ~]# mkdir -p /ssl_key

2.修改 Gitlab 配置文件

[root@gitlab ~]# vim /etc/gitlab/gitlab.rb
external_url "https://gitlab.bertwu.online"
# 必须修改
nginx['enable'] = true
nginx['client_max_body_size'] = '1000m'
nginx['redirect_http_to_https'] = true
nginx['redirect_http_to_https_port'] = 80
# 所有请求80的都跳转到443
nginx['ssl_certificate'] =
"/ssl_key/server.crt"
nginx['ssl_certificate_key'] =
"/ssl_key/server.key"
nginx['ssl_ciphers'] = "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256"
nginx['ssl_prefer_server_ciphers'] = "on"
nginx['ssl_protocols'] = "TLSv1.2"
nginx['ssl_session_cache'] = "builtin:1000 nnshared:SSL:10m"
nginx['ssl_session_timeout'] = "1440m"

3.重新初始化 Gitlab

[root@gitlab ~]#  gitlab-ctl reconfigure

5.6 Gitlab忘记密码

如何重置 GitLab的 root 密码 官方修改密码方式
1.在 root 用户下,执行

[root@gitlab ~]# gitlab-rails console -e production
--------------------------------------------------------------------------------
 GitLab:       12.3.0 (4fcd588b89f)
 GitLab Shell: 10.0.0
 PostgreSQL:   10.9
--------------------------------------------------------------------------------

2.获得用户数据,修改用户密码

irb(main):001:0> user=User.where(id:1).first
=> #<User id:1 @root>

#更改密码并确认密码
irb(main):002:0> user.password="12345678"
=> "12345678"
irb(main):003:0> user.password_confirmation="12345678"
=> "12345678"

#保存退出
irb(main):004:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 07420155-1ccb-44a5-9b1c-c74e0253f253) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f6ee7794b38 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):005:0> quit
[root@gitlab ~]# 

おすすめ

転載: blog.csdn.net/m0_46090675/article/details/120853935
おすすめ