欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

GitLab的安装教程与中文语言包配置指南

最编程 2024-07-30 16:27:03
...

一、Gitlab 安装

1. 环境准备

本教程主要以 CentOS 7为主,其他系统请查看官方文档对比。

1. 安装和打开 http 和 ssh 的权限
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd

sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld

2. 安装邮件服务 postfix
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

使用其他邮件服务,可以根据官方文档自行修改:
https://docs.gitlab.com/omnibus/settings/smtp.html

2. 镜像切换

Gitlab 国外镜像速度一向感人,而且是不是的还抽风,所以在国内搭建 Gitlab 使用的时候,还是切换成墙内镜像比较放心。目前官方支持的国内镜像是清华大学的镜像站,地址:https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/

1. 新建镜像 repo

新建 /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

2. 执行命令下载
sudo yum makecache
sudo yum install gitlab-ce

3. 配置使用

安装完成之后,可以在/etc/gitblab/gitlab.rb中按照需求修改配置,
主要需要修改external_url,改成自己使用的 url 地址。
执行命令配置生效:

gitlab-ctl reconfigure

如果想使用已有的 nginx,或者相对 gitlab 内置 nginx 进行高级配置的话,可以参考官方文档:
https://docs.gitlab.com/omnibus/settings/nginx.html
例如,修改监听端口,增加 ssl 配置,设置 header:

nginx['listen_port'] = 8081
# For GitLab
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key"
# proxy set header
nginx['proxy_set_headers'] = {
  "X-Forwarded-Proto" => "http",
  "CUSTOM_HEADER" => "VALUE"
 }

需要注意的是,修改配置之后,都需要执行gitlab-ctl reconfigure命令,使配置生效。

二、Gitlab 备份

Gitlab 成功运行起来之后,最终的事情就是定期的备份,遇到问题后的还原。

1. 备份配置

默认 Gitlab 的备份文件会创建在/var/opt/gitlab/backups文件夹中,格式为时间戳_日期_版本号_gitlab_backup.tar,例如:1515031353_2018_01_04_10.3.2_gitlab_backup.tar
修改备份文件夹,需要修改配置文件/etc/gitlab/gitlab.rb中的:

gitlab_rails['backup_path'] = '/your_wish/backups'

然后gitlabctl-reconfigure生效。

2. 手动备份

命令:gitlab-rake gitlab:backup:create
会在命令执行的时间点,在你配置的文件夹或者默认文件夹创建一个备份文件。

3. 自动备份

Gitlab 支持 crontab 来创建计划任务,执行命令:sudo crontab -e -u root,为 root 用户,创建并编辑 crontab。
例如,每周二到周六的早晨创建一个自己命名规则的备份:

15 04 * * 2-6  umask 0077; tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz") -C / etc/gitlab

crontab 可以参考:
http://www.pantz.org/software/cron/croninfo.html
更多备份用法,可以参考:
https://docs.gitlab.com/omnibus/settings/backups.html#backups

4. 恢复备份

先停止 gitlab 相关服务:

sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq

执行备份恢复命令:

gitlab-rake gitlab:backup:restore BACKUP=1515031353_2018_01_04_10.3.2

为了防止有问题,还是需要执行重新配置,用来重新配置和启动:

gitlab-ctl reconfigure

5. 迁移,及版本不一致问题
Gitlab 迁移

Gitlab 的迁移非常简单,只需要在目标服务器上安装好之后,把 Gitlab 的备份文件放到备份目录中,然后执行上一步的恢复备份命令即可。

Gitlab 迁移版本不一致问题

但是Gitlab 有个不爽的地方就是,不同版本号的备份文件不能相互使用。有时候备份文件是旧版本,安装的时候自动安装了最新版本,这时候备份文件就没办法使用了,怎么办呢?

  1. 先卸载掉已经安装的最新版Gitlab:sudo yum remove gitlab-ce
  2. 在 Gitlab package 地址https://packages.gitlab.com/gitlab/gitlab-ce中找到对应操作系统和版本的安装包,例如 CentOS 7,Gitlab 10.3.1对应的是gitlab-ce-10.3.1-ce.0.el7.x86_64.rpm
    执行安装命令
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install gitlab-ce-10.3.1-ce.0.el7.x86_64 

然后重复之前的安装、配置、恢复备份的目录即可。

三、Gitlab 汉化

Gitlab中文社区版地址:https://gitlab.com/xhang/gitlab

  1. 克隆版本库
cd /usr/local/src/
git clone https://gitlab.com/xhang/gitlab.git
  1. 获取当前Gitlab版本
gitlab_version=$(cat /opt/gitlab/embedded/service/gitlab-rails/VERSION)
  1. 比较汉化标签和原标签,导出patch用的diff文件
cd /usr/local/src/gitlab
git diff v${gitlab_version} v${gitlab_version}-zh > ../${gitlab_version}-zh.diff
  1. 先停止gitlab
gitlab-ctl stop
  1. 导入汉化补丁
patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < ../${gitlab_version}-zh.diff

PS:如果出现类似以下内容,则按住回车,一直跳过就行了

can't find file to patch at input line 5
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/app/assets/javascripts/awards_handler.js b/app/assets/javascripts/awards_handler.js
|index eb0f06e..73e4833 100644
|--- a/app/assets/javascripts/awards_handler.js
|+++ b/app/assets/javascripts/awards_handler.js
--------------------------
File to patch:
  1. 然后启动gitlab
gitlab-ctl start

此时访问http://IP就是中文页面了(启动后马上打开页面可能会显示502,稍等几秒刷新就好了)

image.png