Gitlab搭建

GitLab是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。

安装Postfix

Postfix支持Gitlab发信功能

bash

apt install postfix

设置Postfix开机自启,并启动

bash

systemctl enable postfix
systemctl start postfix

在Postfix安装期间,可能会出现配置屏幕。选择“Internet Site”并按Enter键。使用服务器的外部DNS作为“邮件名称”,然后按Enter键。如果出现其他屏幕,请继续按Enter键接受默认值。

bash

# 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

如果提示连接超时,可以使用清华大学开源软件镜像站

首先信任 GitLab 的 GPG 公钥:

bash

curl https://packages.gitlab.com/gpg.key 2> /dev/null | apt-key add - &>/dev/null

bash

# 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:

bash

apt-get update
apt-get install gitlab-ce

GitLab基本配置集中在/etc/gitlab/gitlab.rb文件, 每个参数的作用和配置请参考官网的配置说明. 需要注意的几个参数

text

external_url 'http://192.168.1.83'

text

gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/gitlab/backups"
gitlab_rails['backup_gitaly_backup_path'] = "/data/gitlab/gitaly-backup"

image-20230419113715320

text

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

image-20230419113652182

text

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

text

prometheus_monitoring['enable'] = false

image-20230904164445099

bash

gitlab-ctl reconfigure # 首次启动也要用此命令。重新加载配置并启动
gitlab-ctl start # 启动

进行配置改动后输入以下命令使配置生效

bash

gitlab-ctl reconfigure
gitlab-ctl start

bash

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配置文件

bash

vim docker-compose.yml

docker-compose.yml内容如下

yaml

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'

bash

docker-compose up -d

docker-compose方式并没有进行服务拆分,可以直接docker启动

bash

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密码,看这里

image-20210628144404489

bash

docker exec -it gitlab cat /etc/gitlab/initial_root_password

由于升级不能跨越大版本号,因此只能升级到当前大版本号到最高版本,方可升级到下一个大版本号。

官方升级步骤

跨版本升级会看到如下错误提示

text

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 的备份目录

bash

vi /etc/gitlab/gitlab.rb

text

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

bash

mkdir -p /data/gitlab/backups
chown -R git.git /data/gitlab/backups

重载 Gitlab 配置文件

bash

gitlab-ctl reconfigure

停止部分 Gitlab 服务

bash

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx

手动备份 Gitlab(可有可无,自行决定需不需要)

bash

gitlab-rake gitlab:backup:create

查询版本

bash

# Gitlab-ce
apt-cache madison gitlab-ce|awk '{print $3}'
# 极狐Gitlab
apt-cache madison gitlab-jh|awk '{print $3}'

安装升级包

bash

# Gitlab-ce
apt install -y gitlab-ce=16.2.3-ce.0
# 极狐Gitlab
apt install -y gitlab-jh=16.2.3-jh.0

升级完成!如果您的 GitLab 服务器出现故障,请尝试运行

text

gitlab-ctl restart

继续升级,直至想要升级的版本,升级前记得停止部分服务

bash

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 中官方默认已经支持中文版本了,所以不再需要打补丁包了。

image-20220606171020968

If you disable version check, this information isn’t collected. To enable or disable it:

  1. On the top bar, select Menu > {admin} Admin.
  2. In the left sidebar, select Settings > Metrics and profiling, and expand Usage statistics.
  3. Enable or disable Version check.
  4. Select Save changes.

Settings -> Preferences -> Localization -> Language

image-20230419103646173

主要修改两个地方

bash

vi /etc/gitlab/gitlab.rb

text

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'] = ''

text

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

修改完后重新加载配置

bash

gitlab-ctl reconfigure

通过命令行测试邮件是否发送成功

bash

gitlab-rails console

irb(main):001:0> Notify.test_email('1624717079@qq.com', 'Message Subject', 'Message Body').deliver_now

GitLab中默认启用用户头像是自动随机生成的,由于Gravatar被墙,加载很长时间头像也无法显示,影响加载速度。

image-20221128132924892

管理中心-通用-账户和限制

取消勾选 启用 Gravatar

image-20221128133112854

bash

vi /var/opt/gitlab/gitlab-rails/etc/gitlab.yml

将plain_url的地址改为以下其一

bash

https://sdn.geekzu.org/avatar/%{hash}?s=%{size}&d=identicon
https://gravatar.loli.net/avatar/%{hash}?s=%{size}&d=identicon

然后执行

bash

gitlab-ctl restart
gitlab-rake cache:clear RAILS_ENV=production

配置备份

bash

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/

存储库备份

bash

gitlab-backup create
# cron
0 2 * * * gitlab-backup create CRON=1

参考链接:

Backup

Back up and restore GitLab

配置选项

SMTP 配置

备份

备份和恢复极狐GitLab

相关内容