gitlab

一、gitlab介绍

参考链接:https://www.cnblogs.com/hgzero/p/14088215.html
GitLab是利用Ruby on Rails一个开源的版本管理系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。

GitLab能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。

它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找

二、gitlab服务

Nginx:静态web服务器。
gitlab-shell:用于处理Git命令和修改authorized keys列表。
gitlab-workhorse: 轻量级的反向代理服务器。
logrotate:日志文件管理工具。
postgresql:数据库。
redis:缓存数据库。
sidekiq:用于在后台执行队列任务(异步执行)。
unicorn:An HTTP server for Rack applications,GitLab Rails应用是托管在这个服务器上面的。

三、工作流程

3.1 GitLab Shell

GitLab Shell有两个作用:为GitLab处理Git命令、修改authorized keys列表

当通过SSH访问GitLab Server时,GitLab Shell会:

限制执行预定义好的Git命令(git push,git pull,git annex)
调用GitLab Rails API检查权限
执行pre-receive钩子(在企业版中叫做Git钩子)
执行用户请求的动作,处理GitLab的post-receive动作
处理自定义的post-receive动作
当通过http(s)访问GitLab Server时,工作流程取决于你是从Git仓库拉取(pull)代码还是向git仓库推送(push)代码:

如果是从Git仓库拉取(pull)代码,GitLab Rails应用会全权负责处理用户鉴权和执行Git命令的工作

如果是向Git仓库推送(push)代码,GitLab Rails应用既不会进行用户鉴权也不会执行Git命令,它会把以下工作交由GitLab Shell进行处理:

调用GitLab Rails API 检查权限
执行pre-receive钩子(在GitLab企业版中叫做Git钩子)
执行你请求的动作
处理GitLab的post-receive动作
处理自定义的post-receive动作

3.2 GitLab Workhorse

GitLab Workhorse是一个敏捷的反向代理。它会处理一些大的HTTP请求,比如文件上传、文件下载、Git push/pull和Git包下载。其它请求会反向代理到GitLab Rails应用,即反向代理给后端的unicorn。

五、gitlab安装

Gitlab要求服务器内存2G以上

5.1 方式一:下载gitlab-ce的rpm包

官方源:https://packages.gitlab.com/gitlab/gitlab-ce
清华源:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

5.1.1 git安装

链接:https://blog.csdn.net/martinlinux/article/details/120538913

5.1.2 gitlab下载安装

gitlab安装好之后自动配置开机自启,需要内存2g以上(不包括2g)

下载rpm安装
#wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-13.6.1-ce.0.el7.x86_64.rpm
wget http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-13.6.1-ce.0.el7.x86_64.rpm
yum install -y gitlab-ce-13.6.1-ce.0.el7.x86_64.rpm

yum源安装
vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
Repo_gpgcheck=0
Enabled=1
Gpgkey=https://packages.gitlab.com/gpg.key
yum makecache  # 这一步会创建大量的数据
# 直接安装最新版
yum install -y gitlab-ce 
# 如果要安装指定的版本,在后面填上版本号即可
yum install -y  gitlab-ce-13.6.1
# 如果安装时出现gpgkey验证错误,只需在安装时明确指明不进行gpgkey验证
yum install gitlab-ce -y --nogpgcheck

六、gitlab配置

6.1 配置url

[root@localhost ~]# cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb-bak #先做好备份
[root@centos7 test]# vim /etc/gitlab/gitlab.rb

[root@centos7 test]# grep "^[a-Z]" /etc/gitlab/gitlab.rb
external_url 'http://192.168.8.20'  # 这里一定要加上http://

时区配置
gitlab_rails['time_zone'] = 'Asia/Shanghai'

重载配置

安装完成后,需要对GitLab的配置进行初始化,或者在修改了GitLab的配置也需要进行初始化操作
gitlab-ctl reconfigure # 重载配置文件并启动 gitlab

6.2 配置邮箱

参考链接:https://www.cnblogs.com/justuntil/p/10352750.html

6.2.1 邮箱服务的用途

有合并请求时,邮件通知
账号注册时,邮件验证
修改密码时,通过邮件修改

6.2.2 配置QQ邮箱

设置–>账户–>smtp–>密保验证–>验证成功后返回授权码–>保存授权码
在这里插入图片描述

6.2.3 配置gitlab

cp /etc/gitlab/gitlab.rb{,$(date +%F)-bak}  #备份配置文件
vim /etc/gitlab/gitlab.rb
# 配置邮件服务
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465  #如果这里写25 下边就不要开启tls
gitlab_rails['smtp_user_name'] = "xxx@qq.com"  # 自己的qq邮箱账号
gitlab_rails['smtp_password'] = "xxx"  # 开通smtp时返回的授权码
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"   
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "xxx@qq.com"  # 指定发送邮件的邮箱地址
user["git_user_email"] = "xxx@qq.com"   # 指定接收邮件的邮箱地址

更新设置
gitlab-ctl reconfigure

6.2.4 测试邮件服务是否正常

检查ActionMailer delivery_method 来确认是你想要的配置。如果你配置了SMTP,应该显示 :smtp;如果配置了使用 Sendmail,应该显示 :sendmail:

[root@localhost ~]# gitlab-rails console
--------------------------------------------------------------------------------
 Ruby:         ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
 GitLab:       13.6.1 (1b6a590b197) FOSS
 GitLab Shell: 13.13.0
 PostgreSQL:   11.9
--------------------------------------------------------------------------------
Loading production environment (Rails 6.0.3.3)
irb(main):001:0> ActionMailer::Base.delivery_method
=> :smtp

如果配置的是 SMTP,检查下 mail 设置:

irb(main):002:0> ActionMailer::Base.smtp_settings
=> {:authentication=>:login, :address=>"smtp.qq.com", :port=>465, :user_name=>"xxxxxxxx@qq.com", :password=>"授权码", :domain=>"qq.com", :enable_starttls_auto=>true, :tls=>true, :ca_file=>"/opt/gitlab/embedded/ssl/certs/cacert.pem"}

发送邮件测试

打开gitlab控制台
gitlab-rails console

发送邮件
Notify.test_email('接收方邮件地址','邮件标题','邮件内容').deliver_now

发送成功

irb(main):001:0> Notify.test_email('timerovers@163.com','gitlab','gitlab邮件发送成功').deliver_now
Notify#test_email: processed outbound mail in 1.3ms
Delivered mail 618bccd89ed88_1340f537075689@localhost.localdomain.mail (1175.5ms)
Date: Wed, 10 Nov 2021 21:44:56 +0800
From: GitLab <1197079639@qq.com>
Reply-To: GitLab <noreply@192.168.8.20>
To: timerovers@163.com
Message-ID: <618bccd89ed88_1340f537075689@localhost.localdomain.mail>
Subject: gitlab
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Auto-Submitted: auto-generated
X-Auto-Response-Suppress: All

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>gitlab&#37038;&#20214;&#21457;&#36865;&#25104;&#21151;</p></body></html>

=> #<Mail::Message:199620, Multipart: false, Headers: <Date: Wed, 10 Nov 2021 21:44:56 +0800>, <From: GitLab <1197079639@qq.com>>, <Reply-To: GitLab <noreply@192.168.8.20>>, <To: timerovers@163.com>, <Message-ID: <618bccd89ed88_1340f537075689@localhost.localdomain.mail>>, <Subject: gitlab>, <Mime-Version: 1.0>, <Content-Type: text/html; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>, <Auto-Submitted: auto-generated>, <X-Auto-Response-Suppress: All>>
irb(main):002:0> 

6.3 配置ssl

6.3.1 修改配置

external_url 'https://git.time.com:8112'  #修改为https
unicorn['worker_processes'] = 2  #设置进程
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/hogoliving.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/hogoliving.key"

6.4 配置数据目录

 git_data_dirs({
   "default" => {
     "path" => "/data/git-data"
    }
 })

七、Gitlab命令

gitlab-ctl start         # 启动所有 gitlab 组件
gitlab-ctl stop          # 停止所有 gitlab 组件
gitlab-ctl restart       # 重启所有 gitlab 组件
gitlab-ctl status        # 查看服务状态

gitlab-ctl reconfigure   # 重载配置文件并启动 gitlab
gitlab-ctl show-config   # 验证配置文件

gitlab-ctl tail          # 查看日志

gitlab-rake gitlab:check SANITIZE=true --trace    # 检查gitlab

八、web页面访问

8.1 第一登录需要修改密码

账号root 密码自己设置
在这里插入图片描述

8.2 设置中文

GitLab12以上的版本自带中文语言,不再需要各种翻译或者破解插件了
8.2.1 右上角->设置
在这里插入图片描述
8.2.2 选择Preferences->选择中文->保存设置就行
在这里插入图片描述

九、Gitlab备份与恢复

参考链接:https://www.jianshu.com/p/bb4c5c4b53ee

9.1 查看系统版本和软件版本

[root@localhost ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
13.6.1

9.2 备份

1)修改配置文件

# 备份保存的位置,这里是默认位置,可修改成指定的位置
gitlab_rails['backup_path'] = "/opt/gitlab/backups"

#备份文件的默认权限
gitlab_rails['backup_archive_permissions'] = 0644

# 设置备份保存的时间,超过此时间的日志将会被新覆盖
gitlab_rails['backup_keep_time'] = 604800  # 单位秒,这里是默认设置,保存7天

# 特别注意:
#     如果自定义了备份保存位置,则要修改备份目录的权限,比如:
#     chown -R git.git /data/backup/gitlab

2)重新加载配置

gitlab-ctl reconfigure
  1. 执行命令备份
备份配置文件
cp /etc/gitlab/{gitlab.rb,gitlab-secrets.json} /opt/gitlab/backups/
#如果修改了nginx配置文件也可以备份/opt/gitlab/embedded/conf/

备份数据
[root@localhost ~]# gitlab-rake gitlab:backup:create

2021-11-10 22:25:19 +0800 -- Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2021-11-10 22:25:21 +0800 -- done
2021-11-10 22:25:21 +0800 -- Dumping repositories ...
 * gitlab-instance-5c764f63/monitoring (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b) ... 
 * gitlab-instance-5c764f63/monitoring (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b) ... [SKIPPED]
 * gitlab-instance-5c764f63/monitoring.wiki (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.wiki) ... 
 * gitlab-instance-5c764f63/monitoring.wiki (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.wiki) ... [SKIPPED]
 * gitlab-instance-5c764f63/monitoring.design (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.design) ... 
 * gitlab-instance-5c764f63/monitoring.design (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.design) ... [SKIPPED]
2021-11-10 22:25:21 +0800 -- done
2021-11-10 22:25:21 +0800 -- Dumping uploads ... 
2021-11-10 22:25:21 +0800 -- done
2021-11-10 22:25:21 +0800 -- Dumping builds ... 
2021-11-10 22:25:21 +0800 -- done
2021-11-10 22:25:21 +0800 -- Dumping artifacts ... 
2021-11-10 22:25:21 +0800 -- done
2021-11-10 22:25:21 +0800 -- Dumping pages ... 
2021-11-10 22:25:21 +0800 -- done
2021-11-10 22:25:21 +0800 -- Dumping lfs objects ... 
2021-11-10 22:25:21 +0800 -- done
2021-11-10 22:25:21 +0800 -- Dumping container registry images ... 
2021-11-10 22:25:21 +0800 -- [DISABLED]
Creating backup archive: 1636554321_2021_11_10_13.6.1_gitlab_backup.tar ... done
Uploading backup archive to remote storage  ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... done. (0 removed)
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data 
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.

4)设置定时任务

# 每天凌晨2点定时创建备份
# 将一下内容写入到定时任务中 crontab -e
0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create

# 备份策略建议:
#     本地保留3到7天,在异地备份永久保存

5)查看备份

可以到/data/gitlab/backups找到备份包,解压查看,会发现备份的还是比较全面的,数据库、repositories、build、upload等分类还是比较清晰的。
备份完成,会在备份目录中生成一个当天日期的tar包。

[root@localhost backups]# ls
1636554321_2021_11_10_13.6.1_gitlab_backup.tar
# 备份后的文件类似这样的形式:1636554321_2021_11_10_13.6.1_gitlab_backup.tar,可以根据前面的时间戳确认备份生成的时间
[root@localhost backups]# date -d @1636554321
20211110日 星期三 22:25:21 CST

9.3 数据恢复

注意:通过备份文件恢复gitlab必须保证两台主机的gitlab版本一致,否则会提示版本不匹配
本人是在另一台服务器上恢复的,所以需要恢复配置文件和配置文件gitlab.rb中的 访问URL

1)停止数据写入服务

#配置文件恢复
cp gitlab.rb gitlab-secrets.json /etc/gitlab/

#配置文件恢复之后重载或重启
gitlab-ctl reconfigure

# 停止数据写入服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

2)进行数据恢复并重启

#恢复之前需要加权限
chmod +777 1636638092_2021_11_11_13.6.1_gitlab_backup.tar
# 进行恢复
gitlab-rake gitlab:backup:restore BACKUP=1636638092_2021_11_11_13.6.1  # 这个时间戳就是刚刚备份的文件前面的时间戳

# 重启
gitlab-ctl restart

十、创建项目

需要先创建用户组–>创建用户–>创建项目–>将用户添加入项目并赋予权限

10.1创建群组

在这里插入图片描述
在这里插入图片描述

10.2 创建用户

在这里插入图片描述
在这里插入图片描述

10.3 创建项目

创建好项目之后,只有管理员有权限初始化仓库,普通用户没有
在这里插入图片描述
在这里插入图片描述

10.4 邀请成员

在这里插入图片描述

十一、用户添加公钥

公钥 随便生成就可以 我这个是root用户生成的密钥,所以克隆仓库的时候root用户不需要输入密码

[root@localhost src]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ucSIgbQTiijrM1o94xaMR7MkerP7IC/9+4/NxOIkjsQ root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|  o              |
|oo +             |
|= + .            |
|....+o o .       |
|.. *.o. S        |
|o.+.=  o .       |
|.*E+=.o +        |
|o===.* *         |
|..+=*o+.+        |
+----[SHA256]-----+

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值