描述:
Gitlab部署过程、操作手册,包含Code review流程、数据定时备份、自动验证提交代码质量、项目提交报告生成。
目的:
高效管理团队代码
参考:
目录
2.1.3 创建分支合并请求(假设已完成部分功能开发,要合并到总开发分支进行集成)
3.1 运行gitlab-ctl reconfigure时,卡在ruby_block[wait for grafana service socket] action run不动
一、安装部署
1.1 安装依赖
安装和启用policycoreutils、openssh-server和openssh-clients组件,并将它们开放防火墙
sudo yum install -y curl policycoreutils-python openssh-server perl
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld
安装邮件支持
sudo yum -y install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
1.2 下载Gitlab
配置yum源
# sudo vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
应用yum配置:sudo yum makecache
下载:sudo yum install -y gitlab-ce
,显示图中信息时,则表示下载成功。
查看包信息:yum info gitlab-ce
1.3 初始化配置
下载Gitlab后,主配置文件为:/etc/gitlab/gitlab.rb
,打开编辑external_url
项
# 域名或IP都可以
# 因为是内网,我这里使用IP,使用域名时需要DNS进行解析或在本地hosts文件里添加映射
...
external_url 'http://192.168.1.202'
...
注意项 ⚠️:
Gitlab默认端口为80
,若本机80
端口被占用,则需用指定端口进行配置。
external_url 'http://192.168.1.202'
# 指定Nginx监听端口
nginx['listen_port'] = 8899
修改保存后,执行gitlab-ctl reconfigure
使配置生效。
然后查看gitlab各服务是否正常:gitlab-ctl status
,都显示run
,则表示正常运行中。
1.4 管理员登陆
访问web页面,登陆管理员账号。
注意项 ⚠️:
默认管理员用户名为:root
密码终端键入:cat /etc/gitlab/initial_root_password
查看
此密码为初始密码,在生成后24小时后自动删除,登陆后尽快重置密码
二、扩展操作
2.1 Code review流程
参考地址:基于 Gitlab 的 Code Review 最佳实践 - 知乎
操作原理:基于Gitflow的分支性质,在发起合并分支请求时,指定相关人员进行评审、修正,冲突解决。。
2.1.1 创建项目和分支
创建项目
创建分支
分支说明
此演示仅涉及Develop
和Feature
分支,其他分支按实际情况分配。
2.1.2 在开发分支中,push代码到远端
2.1.3 创建分支合并请求(假设已完成部分功能开发,要合并到总开发分支进行集成)
这里指定需要 @zhangsan
@root
@lishun
审查
2.1.4 开始review
登陆@zhangsan
账号
点击merge request
进入合并请求页面,进行评审代码,指出问题或建议。
2.1.5 问题解决
登陆@lishun
账号,进入合并请求页面,查看全部待解决问题。
在本地修正后,提交并回复解决所有问题。
2.1.6 分支合并
全部问题解决完后,执行分支合并,源分支已被合并,目的分支已增加经评审后修正的内容。
2.2 备份及恢复
对gitlab进行备份将会创建一个包含所有库和附件的归档文件。对备份的恢复只能恢复到与备份时的gitlab相同的版本。将gitlab迁移到另一台服务器上的最佳方法就是通过备份和还原。
注意项:
如果自定义备份目录需要赋予git权限,需在配置文件中加入:
gitlab_rails['backup_path'] = '/data/backup/gitlab'
# 备份保留的时间(以秒为单位,这个是七天默认值)
gitlab_rails['backup_keep_time'] = 604800
mkdir /data/backup/gitlab
chown -R git.git /data/backup/gitlab
gitlab-ctl reconfigure
2.2.1 手动备份
执行gitlab-rake gitlab:backup:create
,生成一次备份。备份文件将保存在配置文件中定义的backup_path
中。
2.2.2 自动备份
添加定时任务:
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
CRON=1:如果没有任何错误发生,不输出过程进度
2.2.3 多主机自动备份
整体备份恢复逻辑如图:
通过定时任务执行脚本的形式,达到主机间数据同步。
2.2.4 数据恢复
数据恢复时,需要gitlab处于运行状态,备份文件位于:gitlab_rails['backup_path']
- 进入备份文件目录:
cd /var/opt/gitlab/backups
- 停止数据服务:
gitlab-ctl stop sidekiq && gitlab-ctl stop unicorn
- 数据恢复:
gitlab-rake gitlab:backup:restore BACKUP=1512811475_2022_03_11_3.2.2
- 完成后重启服务:
gitlab-ctl restart
- 检查:
gitlab-rake gitlab:check SANITIZE=true
2.3 提交前代码自动校验
具体实现方法使用git中hook功能,将其集成到Gitlab服务端中。达到本地上传代码,服务端自动校验并驳回的功能。降低Code review的人力成本。
参考链接:Git server hooks | GitLab
git中hooks流程阶段如下:
2.3.1 服务端集成(全局)
- 查看gitlab配置,寻找
custom_hooks_dir
项,修改保存后,使配置生效。
vim /etc/gitlab/gitlab.rb
gitaly['custom_hooks_dir'] = "/opt/gitlab/custom_hooks/"
gitlab-ctl reconfigure
- 创建
custom_hooks_dir
目录,同时创建pre-receive.d
子目录。并赋予权限
# 1. 创建目录
mkdir -p /opt/gitlab/custom_hooks/pre-receive.d
# 2. 提权
sudo chmod +x /opt/gitlab/custom_hooks/
# 3. 创建pre-recevie文件并提权
sudo touch /opt/gitlab/custom_hooks/pre-receive.d/pre-receive
sudo chmod 777 /opt/gitlab/custom_hooks/pre-receive.d/pre-receive
- 编写pre-receive脚本
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import sys
import subprocess
import tempfile
import pylint
# pylint运行参数
Pylint_argv = [
"pylint",
"--output-format=colorized",
"--fail-under=9.0",
"--disable=line-too-long, C0413, C0103, C0115, C0116, R1732, R0903",
"--msg-template='{path}:{line}:{column}: [{msg_id} {obj}] {msg}'",
]
class Trigger:
def __init__(self):
self.__get_git_info()
self.tempdir = self.__get_push_info()
def __get_git_info(self):
"""
pre-receive会传入old_version、new_version和分支信息
"""
self.old_version, self.new_version, self.ref = sys.stdin.readline().strip().split(' ')
def __get_push_info(self):
"""
获取push文件差异信息
"""
print(f"new {self.new_version}, old {self.old_version}, ref {self.ref}")
# 获取差异文件
p = subprocess.Popen(
f"git diff {self.new_version} {self.old_version} --name-only -- '*.py'",
shell=True,
stdout=subprocess.PIPE,
universal_newlines=True
)
pipes = p.stdout.readlines()
pipes = [p.replace("\n", "") for p in pipes]
# 创建临时目录
tempdir = tempfile.mkdtemp('git_hook')
os.system(f"touch {tempdir}/__init__.py")
# 获取文件具体内容
for file_name in pipes:
# print(f"file_path >> {file_name}")
content_p = subprocess.Popen(
f"git show {self.new_version}:{file_name}",
shell=True,
stdout=subprocess.PIPE,
universal_newlines=True
)
py_file = content_p.stdout.read()
# 将文件写入临时目录
d_path = os.path.join(tempdir, os.path.dirname(file_name))
if not os.path.exists(d_path):
os.makedirs(d_path)
# 运行pylint,必须为python库
if "__init__.py" not in os.listdir(d_path):
os.system(f"touch {d_path}/__init__.py")
with open(os.path.join(tempdir, file_name), 'w', encoding='utf-8') as f:
f.writelines(py_file)
return tempdir
def pylint(self):
# print(os.listdir(self.tempdir))
Pylint_argv.append(self.tempdir)
sys.argv = Pylint_argv
pylint.run_pylint()
if __name__ == "__main__":
t = Trigger()
t.pylint()
PS:
示例代码依赖pylint
库,操作前需安装:pip install pylint
pre-receive接收三个参数,分别是:git_old_version、git_new_version和本次提交的分支
pre-receive需返回退出code,0:验证通过;非0:验证不通过。本示例脚本中,校验逻辑使用pylint
库实现,其内部已实现退出code码。
pylint
库具体使用方式参考:Pylint 3.1.0-dev0 documentation
pylint
库支持的参数说明见附录(个人整理,更新中。。):https://www.yuque.com/lishun-qviff/egp0l0/vyrtkx#Uoqls
- 客户端代码提交测试
2.3.2 服务端集成(仓库)
- 查看Gitaly 相对路径(此路径是服务器hook脚本必须位于的位置)
登陆管理员账号,点击菜单,进入项目列表中
进入项目详情,可见Gitaly 相对路径
- 登陆服务器后台,根据gitaly相对路径找到源目录。
-
- 对于 Omnibus GitLab 安装,路径通常为 :
/var/opt/gitlab/git-data/repositories/<group>/<project>.git
- 对于从源代码安装,路径通常为 :
/home/git/repositories/<group>/<project>.git
- 对于 Omnibus GitLab 安装,路径通常为 :
- 创建
custom_hooks
目录;再创建有效名称的hook脚本,并增加其可执行权限。
- 编辑项目代码检查脚本。(这里以上文给出脚本为例,加入辨识输出行)
- 本地提交测试
可见,当全局和局部hook同时存在时,优先执行局部hook,再执行全局hook。
2.3.3 客户端集成
因客户端IDE工具众多,本文只贴部分工具相关链接,其它可自行查找。
IDE | 地址 |
Pycharm | https://pylint.pycqa.org/en/latest/user_guide/ide-integration.html#integrate-pylint-with-pycharm |
VScode |
2.4 git_stats集成
git_stats用于生成git提交报告,有利于统计代码量。
步骤说明:
# 1. 安装rvm
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL https://get.rvm.io | bash -s stable
# 2. 安装ruby
rvm install 2.6
# 3.替换gem源
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
# 4. 安装git_stats
gem install git_stats
# 5. 测试生成报告(须在git项目目录中),dist为指定生成的静态文件目录
git_stats generate -o dist --language zh_tw
# 6. 集成CI/CD
pages_job:
script:
# 初始化报告目录
- echo 'root123456' | su dell -c 'rm -rf /data/backend/gitlab_site/$CI_PROJECT_NAME'
- mkdir -p /data/backend/gitlab_site/$CI_PROJECT_NAME
# 移动仓库文件
- cp -r ./.git /data/backend/gitlab_site/$CI_PROJECT_NAME
- chmod 777 -R /data/backend/gitlab_site/$CI_PROJECT_NAME
- cd /data/backend/gitlab_site/$CI_PROJECT_NAME
# 生成报告
- echo 'root123456' | su dell -c 'git_stats generate -o dist -l zh_tw'
stage: build
only:
- develop
三、安装过程中问题
3.1 运行gitlab-ctl reconfigure
时,卡在ruby_block[wait for grafana service socket] action run
不动
解决方案:gitlab断电后重启_gitlab关闭了又启动-CSDN博客
问题原因:后台存在runing
状态任务,堵塞程序继续运行
四、附录
4.1 gitlab-ctl常用命令
命令 | 说明 |
gitlab-ctl start | 启动所有组件 |
gitlab-ctl status | 查看运行状态 |
gitlab-ctl stop | 停止所有组件 |
gitlab-ctl restart | 重启所有组件 |
gitlab-ctl show-config | 验证配置文件 |
gitlab-ctl reconfigure | 重载主配置文件 主配置文件为: |
gitlab-ctl tail <service name> | 查看服务日志 具体的日志文件路径: |
gitlab-ctl uninstall | 删除gitlab(保留数据) |
gitlab-ctl cleanse | 彻底删除gitlab |
4.2 gitlab常用组件
组件 | 说明 |
nginx | 静态Web服务器 |
gitlab-shell | 用于处理Git命令和修改authorized keys列表,我们的gitlab是以Git做为最层的,你操作实际上最后就是调用gitlab-shell命令进行处理。 |
gitlab-workhorse | 轻量级的反向代理服务器 |
logrotate | 日志文件管理工具 |
postgresql | 数据库 |
redis | 缓存数据库 |
sidekiq | 用于在后台执行队列任务(异步执行) |
unicorn | GitLab Rails应用是托管在这个服务器上面的 |
4.3 gitlab配置文件位置
说明 | 位置 |
主配置文件 | /etc/gitlab/gitlab.rb |
文档根目录 | /opt/gitlab |
默认存储库位置 | /var/opt/gitlab/git-data/repositories |
GitLab Nginx 配置文件路径 | /var/opt/gitlab/nginx/conf/gitlab-http.conf |
Postgresql 数据目录 | /var/opt/gitlab/postgresql/data |
4.4 Pylint参数说明
具体可运行pylint -h
查看帮助信息。
参数 | 说明 |
--output-format | 选择输出方式,可选参数有:
|
--fail-under | 设定评分验证值(默认10.0) pylint每次校验都会给代码打分(满分10分),不足10分会拒绝提交。该配置可降低评分验证值。 |
--disable | 排除的验证模块,可指定多个,用 |
--msg-template | 配置输出信息的格式 具体参考:https://pylint.pycqa.org/en/latest/user_guide/output.html |