Gitlab搭建
GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。
1 安装
安装Postfix
Postfix支持Gitlab发信功能
apt install postfix
设置Postfix开机自启,并启动
systemctl enable postfix
systemctl start postfix
在Postfix安装期间,可能会出现配置屏幕。选择“Internet Site”并按Enter键。使用服务器的外部DNS作为“邮件名称”,然后按Enter键。如果出现其他屏幕,请继续按Enter键接受默认值。
1.0.1 使用官方源安装
# ubuntu
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | bash
EXTERNAL_URL="http://gitlab.example.com" apt install gitlab-ce
# ubuntu极狐
curl -fsSL https://packages.gitlab.cn/repository/raw/scripts/setup.sh | /bin/bash
EXTERNAL_URL="https://gitlab.example.com" apt install gitlab-jh
更改 https://gitlab.example.com 为您要访问GitLab实例的URL
1.0.2 使用国内源安装(推荐)
如果提示连接超时,可以使用清华大学开源软件镜像站
首先信任 GitLab 的 GPG 公钥:
curl https://packages.gitlab.com/gpg.key 2> /dev/null | apt-key add - &>/dev/null
# CentOS
cat > /etc/yum.repos.d/gitlab-ce.repo <<'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu focal main
EOF
# Ubuntu
tee > /etc/apt/sources.list.d/gitlab-ce.list <<'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu focal main
EOF
安装 gitlab-ce:
apt-get update
apt-get install gitlab-ce
1.1 配置
GitLab基本配置集中在/etc/gitlab/gitlab.rb
文件, 每个参数的作用和配置请参考官网的配置说明.
需要注意的几个参数
1.1.1 访问GitLab 实例的 URL
external_url 'http://192.168.1.83'
1.1.2 备份存储路径
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/gitlab/backups"
gitlab_rails['backup_gitaly_backup_path'] = "/data/gitlab/gitaly-backup"
1.1.3 数据存储目录
git_data_dirs({
"default" => {
"path" => "/data/gitlab/git-data"
}
})
1.1.4 prometheus占用磁盘空间的上限
prometheus['flags'] = {
'storage.tsdb.path' => "/var/opt/gitlab/prometheus/data",
'storage.tsdb.retention.time' => "15d",
'storage.tsdb.retention.size' => "2GB",
'config.file' => "/var/opt/gitlab/prometheus/prometheus.yml"
}
关闭Prometheus
prometheus_monitoring['enable'] = false
1.2 启动
gitlab-ctl reconfigure # 首次启动也要用此命令。重新加载配置并启动
gitlab-ctl start # 启动
进行配置改动后输入以下命令使配置生效
gitlab-ctl reconfigure
gitlab-ctl start
2 基于docker-compser
2.1 创建文件夹
mkdir -p /data/gitlab/
cd docker/gitlab/
mkdir -p /data/gitlab/{config,logs,data}
export GITLAB_HOME=/data/gitlab
GitLab容器使用主机卷来存储持久数据
本地位置 | 容器中的位置 | 用途 |
---|---|---|
/data/gitlab/data | /var/opt/gitlab | 存储应用程序数据 |
/data/gitlab/logs | /var/log/gitlab | 存储日志 |
/data/gitlab/config | /etc/gitlab | 存储GitLab配置文件 |
2.2 创建docker-compose.yml文件
vim docker-compose.yml
docker-compose.yml
内容如下
version: '2.3'
services:
gitlab-ce:
image: 'gitlab/gitlab-ce:latest'
container_name: gitlab-ce
restart: always
# hostname: 'gitlab.example.com'
hostname: '192.168.1.10'
environment:
TZ: 'Asia/Shanghai'
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://192.168.1.10'
gitlab_rails['time_zone'] = 'Asia/Shanghai'
# 需要配置到 gitlab.rb 中的配置可以在这里配置,每个配置一行,注意缩进。
# 比如下面的电子邮件的配置:
# gitlab_rails['smtp_enable'] = true
# gitlab_rails['smtp_address'] = "smtp.qiye.aliyun.com"
# gitlab_rails['smtp_port'] = 465
# gitlab_rails['smtp_user_name'] = "xxxx@xx.com"
# gitlab_rails['smtp_password'] = "password"
# gitlab_rails['smtp_authentication'] = "login"
# gitlab_rails['smtp_enable_starttls_auto'] = true
# gitlab_rails['smtp_tls'] = true
# gitlab_rails['gitlab_email_from'] = 'xxxx@xx.com'
# ssh端口不修改会冲突
gitlab_rails['gitlab_shell_ssh_port'] = 2222
# root账号的密码,注意不能小于八位
gitlab_rails['initial_root_password'] = "root@123"
# Add any other gitlab.rb configuration here, each on its own line
ports:
- '80:80'
- '443:443'
- '2222:22'
volumes:
- '$GITLAB_HOME/config:/etc/gitlab'
- '$GITLAB_HOME/logs:/var/log/gitlab'
- '$GITLAB_HOME/data:/var/opt/gitlab'
2.3 启动
docker-compose up -d
docker-compose
方式并没有进行服务拆分,可以直接docker启动
docker run -d \
-p 443:443 -p 80:80 -p 2222:22 \
--hostname 192.168.1.10 \
--name gitlab \
--restart always \
-e TZ=Asia/Shanghai \
-e GITLAB_ROOT_PASSWORD=root@123 \
-v $GITLAB_HOME/config:/etc/gitlab \
-v $GITLAB_HOME/logs:/var/log/gitlab \
-v $GITLAB_HOME/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
如果没有指定root密码,看这里
docker exec -it gitlab cat /etc/gitlab/initial_root_password
3 升级
3.1 升级GitLab
由于升级不能跨越大版本号,因此只能升级到当前大版本号到最高版本,方可升级到下一个大版本号。
- GitLab 14:
14.0.12
>14.3.6
>14.9.5
>14.10.5
. - GitLab 15:
15.0.5
>15.1.6
>15.4.6
>15.11.13
. - GitLab 16:
16.0.8
>16.1.6
>16.2.9
>16.3.7
>16.7.z
> latest16.Y.Z
.
跨版本升级会看到如下错误提示
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be upgraded:
gitlab-jh
...
gitlab preinstall: It seems you are upgrading from 16.2 to 16.7.
gitlab preinstall: It is required to upgrade to the latest 16.3.x version first before proceeding.
...
升级到 xx 版本需要需要先升级到最新的 xx 版本
设置 Gitlab 的备份目录
vi /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/gitlab/backups"
gitlab_rails['backup_archive_permissions'] = 0644
gitlab_rails['backup_keep_time'] = 604800
mkdir -p /data/gitlab/backups
chown -R git.git /data/gitlab/backups
重载 Gitlab 配置文件
gitlab-ctl reconfigure
停止部分 Gitlab 服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx
手动备份 Gitlab(可有可无,自行决定需不需要)
gitlab-rake gitlab:backup:create
查询版本
# Gitlab-ce
apt-cache madison gitlab-ce|awk '{print $3}'
# 极狐Gitlab
apt-cache madison gitlab-jh|awk '{print $3}'
安装升级包
# Gitlab-ce
apt install -y gitlab-ce=16.2.3-ce.0
# 极狐Gitlab
apt install -y gitlab-jh=16.2.3-jh.0
升级完成!如果您的 GitLab 服务器出现故障,请尝试运行
gitlab-ctl restart
继续升级,直至想要升级的版本,升级前记得停止部分服务
apt install -y gitlab-ce=12.10.14-ce.0
...
apt install -y gitlab-ce=13.0.14-ce.0
...
apt install -y gitlab-ce=13.3.2-ce.0
在新版的 Gitlab 中官方默认已经支持中文版本了,所以不再需要打补丁包了。
3.2 关闭 GitLab Version Check
If you disable version check, this information isn’t collected. To enable or disable it:
- On the top bar, select Menu > {admin} Admin.
- In the left sidebar, select Settings > Metrics and profiling, and expand Usage statistics.
- Enable or disable Version check.
- Select Save changes.
4 常见问题
4.1 切换中文
Settings -> Preferences -> Localization -> Language
4.2 邮件配置
主要修改两个地方
vi /etc/gitlab/gitlab.rb
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'gitlab@example.com'
gitlab_rails['gitlab_email_display_name'] = 'Gitlab'
gitlab_rails['gitlab_email_reply_to'] = 'gitlab@example.com'
gitlab_rails['gitlab_email_subject_suffix'] = ''
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qiye.aliyun.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "gitlab@example.com"
gitlab_rails['smtp_password'] = "123456"
gitlab_rails['smtp_domain'] = "qiye.aliyun.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = false
gitlab_rails['smtp_tls'] = true
修改完后重新加载配置
gitlab-ctl reconfigure
通过命令行测试邮件是否发送成功
gitlab-rails console
irb(main):001:0> Notify.test_email('1624717079@qq.com', 'Message Subject', 'Message Body').deliver_now
4.3 解决访问GitLab很慢问题
GitLab中默认启用用户头像是自动随机生成的,由于Gravatar被墙,加载很长时间头像也无法显示,影响加载速度。
4.3.1 关闭头像生成
管理中心-通用-账户和限制
取消勾选 启用 Gravatar
4.3.2 更换Gravatar生成地址
vi /var/opt/gitlab/gitlab-rails/etc/gitlab.yml
将plain_url的地址改为以下其一
https://sdn.geekzu.org/avatar/%{hash}?s=%{size}&d=identicon
https://gravatar.loli.net/avatar/%{hash}?s=%{size}&d=identicon
然后执行
gitlab-ctl restart
gitlab-rake cache:clear RAILS_ENV=production
4.4 数据备份
配置备份
gitlab-ctl backup-etc
# cron
15 04 * * 2-6 gitlab-ctl backup-etc && cd /etc/gitlab/config_backup && cp $(ls -t | head -n1) /data/gitlab/backups/
存储库备份
gitlab-backup create
# cron
0 2 * * * gitlab-backup create CRON=1
参考链接: