GitLab 版本控制与管理指南
GitLab 版本控制与管理指南
一、版本控制介绍
版本控制是指对软件开发过程中各种程序代码、配置文件及说明文档等文件变更的管理,是软件配置管理的核心思想之一。
版本控制最主要的功能就是追踪文件的变更。它将什么时候、什么人更改了文件的什么内容等信息如实地记录下来。每一次文件的改变,文件的版本号都将增加。除了记录版本变更外,版本控制的另一个重要功能是并行开发。
软件开发往往是多人协同作业,版本控制可以有效地解决版本的同步以及不同开发者之间的开发通信问题,提高协同开发的效率。并行开发中最常见的不同版本软件的错误(Bug)修正问题也可以通过版本控制中分支与合并的方法有效地解决。
版本控制流程
- 设定开发基线,确定各个配置项的开发初始版本
- 开发人员基于开发基线的版本开发出所需的目标版本
- 需求变更时评估变更影响范围
- 对被影响的配置项的版本进行修改
- 必要时可回退到以前的版本
常用版本控制工具
- GitHub
- GitLab
- Subversion
官网: https://about.gitlab.com/
国内镜像: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/
二、上线流程
- 开发代码(开发人员)
- 测试(测试人员)
- 发布(运维人员)
- 测试(测试人员)
- 发邮件申请发布(开发人员)
- 邮件发给开发的领导、抄送给运维团队
- 填写变更单(避免责任问题)
- 开发领导审批
- 评估影响范围(运维人员)
- 向运维领导汇报(运维人员)
- 与开发领导协商(运维领导)
- 发布(运维人员)——生产环境
- 测试(测试人员)——如出问题则回滚(运维人员)
三、GitLab 服务管理命令
命令 | 功能描述 |
---|---|
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 | 查看服务的日志 |
gitlab-ctl tail nginx | 查看GitLab下nginx日志 |
gitlab-rails console | 进入控制台 |
四、GitLab 部署
注意: 部署前确保环境纯净,内存尽量充足
最低配置: CPU 2核,内存 8G
环境准备
# 配置yum源
cd /etc/yum.repos.d/
rm -rf *
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo# 安装EPEL源
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
系统配置
- 关闭防火墙和SELinux
安装依赖
# 安装Git
yum -y install git# 安装依赖包
yum -y install curl openssh-server openssh-clients postfix cronie perl# 启动postfix服务
systemctl restart postfix
systemctl enable postfix
下载安装GitLab
cd /usr/src/
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-15.3.3-ce.0.el7.x86_64.rpm# 处理依赖问题
wget https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/policycoreutils-python-2.5-34.el7.x86_64.rpm# 可以省略
rpm -qa | grep policy
rpm -e policycoreutils
rpm -ivh --nodeps policycoreutils-python-2.5-34.el7.x86_64.rpm# 安装GitLab
rpm -ivh gitlab-ce-15.3.3-ce.0.el7.x86_64.rpm
配置GitLab
# 修改配置文件
vim /etc/gitlab/gitlab.rb
# 设置:external_url 'http://192.168.100.10'# 重载配置并重启
gitlab-ctl reconfigure
gitlab-ctl restart# 查看当前的gitlab版本
head -1 /opt/gitlab/version-manifest.txt
重置管理员密码
gitlab-rails console -e production# 在控制台中执行:
user = User.where(id:1).first
user.password = 'redhat123!'
user.password_confirmation = 'redhat123!'
user.save!
exit
五、GitLab 管理
访问方式
- 浏览器访问:
http://192.168.100.10
- 用户名:
root
- 密码: 之前设置的密码
汉化设置
- 用户旁边下拉箭头 → Preferences
- Localization → Language
- 选择"简体中文"
- 保存更改
关闭注册功能
- 管理员 → 设置
- 通用 → 注册限制
- 取消"已启用注册功能"勾选
- 保存更改
六、常用管理操作
核心管理功能
- 项目管理(创建新项目)
- 成员组管理(针对项目创建成员组)
- 用户管理(GitLab用户,非系统用户)
- 新员工:添加GitLab用户
- 离职员工:禁用或删除GitLab用户
Git 基本操作示例
# 克隆项目
git clone git@192.168.100.10:gitlab-instance-5dba1a04/linux.git# 配置用户信息
git config --global user.email "root@example.com"
git config --global user.name "root"# 基本操作流程
cd linux/
touch file1
git add file1
git commit -m "add file"
git push# 添加所有文件
touch file2
git add .
git commit -m "add file"
git push# 分支管理
git branch cy # 创建分支
git push origin cy # 提交分支到远程
git checkout cy # 切换到分支
touch file5
git add file5
git commit -m "add file5"
git push origin cy # 同步分支内容