CentOS 系统下Gitlab搭建与基本配置 以及代码备份迁移过程

时间:2023-03-09 00:36:34
CentOS 系统下Gitlab搭建与基本配置 以及代码备份迁移过程

CentOS 系统下Gitlab搭建与基本配置 以及代码备份迁移过程

GitLab 是一个开源的版本管理系统,提供了类似于 GitHub 的源代码浏览,管理缺陷和注释等功能,你可以将代码免费托管到 GitLab.com,而且不限项目数量和成员数。最吸引人的一点是,可以在自己的服务器上搭建 GitLab CE (社区免费版)版本,方便内部团队协作开发和代码管理。

gitlab 官网:https://about.gitlab.com

gitlab CE下载站点:https://packages.gitlab.com/gitlab/gitlab-ce?page=60

gitlab 清华镜像站点:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/

浙大开源镜像站:http://mirrors.lifetoy.org/gitlab-ce/yum/el7/

下面介绍如何在 CentOS 服务器上搭建 GitLab CE 版本,以及一些基本的配置。

1. 安装:

GitLab 提供了两种安装方式:源码手动编译安装和软件包管理安装。

源码手动编译安装虽然配置灵活,但过程比较麻烦,不容易安装成功,所以我这里选择软件包管理安装的形式。

1.1 使用 GitLab 提供仓库在线安装:

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
yum install gitlab-ce

国外的 GitLab 仓库访问速度较慢,可以使用国内的站点:

curl -sS http://packages.gitlab.cc/install/gitlab-ce/script.rpm.sh | sudo bash
yum install gitlab-ce

1.2 下载离线软件包安装
如果网络速度不理想,可以使用离线软件包 rpm 的方式进行安装,下面提供了几个站点的下载地址。

GitLab 官方:https://packages.gitlab.com/gitlab/gitlab-ce?filter=rpms

清华大学 TUNA 开源镜像站:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

浙大开源镜像站:http://mirrors.lifetoy.org/gitlab-ce/yum/el7/

下载好 rpm 软件安装包后上传到服务器指定的目录下,通过以下命令进行安装:

rpm -ivh gitlab-ce-8.9.-ce..el7.x86_64.rpm

记录一下 rpm 卸载软件安装包命令:

rpm -e --nodeps gitlab-ce-8.9.-ce..el7.x86_64

2. 启动 GitLab:

安装完成之后,打开配置文件 /etc/gitlab/gitlab.rb 将 external_url = 'http://git.example.com' 修改为自己的 IP 地址:external_url 'http://ip_address' ,然后执行下面的命令,对 GitLab 进行编译:

[root@localhost gitlab]# egrep -v "^$|^#" /etc/gitlab/gitlab.rb
external_url 'http://192.168.8.27'
gitlab_rails['gravatar_plain_url'] = 'http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'

gitlab-ctl reconfigure

完成后,使用浏览器访问:http://ip_address 可进入 GitLab 登录页面,首次访问系统会让你重新设置管理员的密码,默认的管理员账号是 root,如果你想更改默认管理员账号,登录系统后可以修改帐号名。

3. GitLab 基本配置:

GitLab 的相关参数配置都存在 /etc/gitlab/gitlab.rb 文件里。自 GitLab 7.6 开始的新安装包, 已经默认将所有的参数写入到 /etc/gitlab/gitlab.rb 配置文件中。

3.1 配置端口
GitLab 默认使用 80 端口对外提供服务,如果因为 80 端口被其他服务占用,需要更改。可以打开 /etc/gitlab/gitlab.rb 配置文件,修改 external_url 'http://ip_address' 为 external_url 'http://ip_address:new-port',

重新编译配置:

gitlab-ctl reconfigure
这时候就可以通更改后的 IP + 端口号码进行访问了。

3.2 邮箱配置
以下是 163 邮箱的配置参考,打开 /etc/gitlab/gitlab.rb 配置文件,添加以下内容:

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] =
gitlab_rails['smtp_user_name'] = "test@163.com"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['gitlab_email_from'] = "test@163.com"

注意: test@163.com 和 password 更新为自己邮箱地址和密码;邮箱需要开启 SMTP 协议。

重新编译配置即可生效:

gitlab-ctl reconfigure

其它邮箱的配置可参考:https://doc.gitlab.cc/omnibus/settings/smtp.html。

3.3 头像配置
GitLab 默认使用的是 Gravatar 头像服务,不过现在貌似 Gravatar 国内好像访问不了,导致 GitLab 默认头像破裂,无法显示,可以替换为多说 Gravatar 服务器。打开 /etc/gitlab/gitlab.rb 配置文件,增加下面这一行:

gitlab_rails['gravatar_plain_url'] = 'http://gravatar.duoshuo.com/avatar/%{hash}?s=%{size}&d=identicon'

再分别执行以下命令即可:

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

也可以关闭 Gravatar 头像显示配置,登录 GitLab 管理员账户,进入设置界面(路径地址:http://ip:port/admin/application_settings ),取消以下选项即可。

3.4 用户注册配置
管理员设置界面(路径地址:http://ip:port/admin/application_settings )以下选项可以控制用户注册配置,包括是否允许登录、注册和注册邮箱验证等选项。

3.5 常用命令
GitLab 服务启动、停止、状态查询、修改配置生效等命令:

gitlab-ctl start/stop/status/reconfigure       # 服务启动、停止、状态查询、修改配置生效
也可以查看帮助文档获取更多命令信息:

gitlab-ctl --help

 4. 代码迁移:

4.1我们可以使用gitlab自带的backup命令进行备份。在gitlab的help文档里面有相关的说明,URL地址是: http://your-gitlab-server/help/raketasks/backup_restore.md

利用gitlab的backup机制会生成一个名为[TIMESTAMP]_gitlab_backup.tar的tar文件,这个tar文件会包含所有的数据库数据、所有的repo数据,以及所有的附件。TIMESTAMP是以秒为单位的时间戳,用来区分不同的备份文件,如:1477287208_gitlab_backup.tar。需要注意的是,利用backup机制进行备份的话,对gitlab的版本是要求严格一致的。例如用8.6版的gitlab生成的备份文件,拿到8.7版的gitlab上进行恢复,是会报错的。

cat /opt/gitlab/embedded/service/gitlab-rails/VERSION    #查看git版本

backup的操作命令:

#如果使用包安装的gitlab的话,使用以下命令
sudo gitlab-rake gitlab:backup:create
#如果使用源码安装的gitlab话,使用以下命令
sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production

也可以使用SKIP变量来选择要备份的内容,SKIP变量的选项有:db, uploads (attachments), repositories, builds(CI build output logs), artifacts (CI build artifacts), lfs (LFS objects)。多个项之间用逗号隔开:

sudo gitlab-rake gitlab:backup:create SKIP=db,uploads

backup命令执行之后,终端上会出现导出数据库及repo数据等的的操作日志。

4.2 backup的相关配置

backup文件的保存位置在/etc/gitlab/gitlab.rb中配置。打开这个配置文件,可以看到backup的一段配置:

### Backup Settings
###! Docs: https://docs.gitlab.com/omnibus/settings/backups.html # gitlab_rails['manage_backup_path'] = true
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" ###! Docs: https://docs.gitlab.com/ce/raketasks/backup_restore.html#backup-archive-permissions
# gitlab_rails['backup_archive_permissions'] = # gitlab_rails['backup_pg_schema'] = 'public' ###! The duration in seconds to keep backups before they are allowed to be deleted
# gitlab_rails['backup_keep_time'] = 604800

# gitlab_rails['backup_upload_connection'] = {
  # 'provider' => 'AWS',
  # 'region' => 'eu-west-1',
  # 'aws_access_key_id' => 'AKIAKIAKI',
  # 'aws_secret_access_key' => 'secret123'

 

可以根据需要修改“”backup_path“” 的路径

默认情况下,备份文件是放在/var/opt/gitlab/backups目录下的,另外archive_permissions属性用来指定生成tar文件的权限属性,默认为0600。还可以通过upload设置,将备份上传到远程服务器,详细配置这里不做介绍。

4.3 利用backup文件恢复:

再次强调一下gitlab对版本是要求严格一致的。例如用8.6版的gitlab生成的备份文件,拿到8.7版的gitlab上进行恢复,是会报错的。

首先我们需要确保backup的tar文件在配置文件中指定的/var/opt/gitlab/backups目录下面。

cp 1393513186_gitlab_backup.tar /var/opt/gitlab/backups/

然后,就可以通过命令来执行恢复操作了:

# 先关闭连接数据库的进程
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq # 通过指定时间戳来执行restore操作,这个操作会复写gitlab的数据库
sudo gitlab-rake gitlab:backup:restore BACKUP=1393513186 #如果backups下只有一个备份文件可以直接执行
sudo gitlab-rake gitlab:backup:restore
# 再次启动gitlab sudo gitlab-ctl start # 通过下面命令检查gitlab sudo gitlab-rake gitlab:check SANITIZE=true

需要注意的是:backup生成的tar文件的备份是不会对gitlab的配置文件进行备份的,gitlab.rb, gitlab.yml, /etc/gitlab/gitlab-secrets.json(存放着数据库中为two-factor authentication加密信息的key)等这些配置文件需要另行备份。

4.4 配置备份的定时任务 
可以利用cron来进行定时备份操作:

  * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=

如果想指定备份文件的保存时间的话,可以在/etc/gitlab/gitlab.rb中进行配置:

# limit backup lifetime to  days -  seconds
gitlab_rails['backup_keep_time'] =

4.5 备份恢复之后如果原来密码进不去的话,可以通过以下方法修改root密码:

Start the console with this command:

gitlab-rails console production

Wait until the console has loaded.

There are multiple ways to find your user. You can search for email or username.

user = User.where(id: ).first

or

user = User.find_by(email: 'admin@local.host')

Now you can change your password:

user.password = 'secret_pass'
user.password_confirmation = 'secret_pass'

It’s important that you change both password and password_confirmation to make it work.

Don’t forget to save the changes.

user.save!
quit

Exit the console and try to login with your new password.

4.6 解决自定义头像无法显示问题:

 解决指向localhost的问题:

编辑gitlab的配置vi /etc/gitlab/gitlab.rb,修改external_url 参数值

[Mesogene@localhost ~]$ sudo vi /etc/gitlab/gitlab.rb
## Url on which GitLab will be reachable.
## For more details on configuring external_url see:
## https://gitlab.com/gitlab-org/omnibus-gitlab/blob/629def0a7a26e7c2326566f0758d4a27857b52a3/README.md#configuring-the-external-url-for-gitlab
#external_url 'http://localhost'    #注释掉,将localhost修改为你对应的iP或者域名
external_url 'http://10.10.10.152'

头像显示问题:

[Mesogene@localhost ~]$ sudo vi /etc/gitlab/gitlab.rb    #把下面这一行的注释去掉即可
gitlab_rails['gravatar_plain_url'] = 'http://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon' [Mesogene@localhost ~]$ sudo gitlab-ctl reconfigure
[Mesogene@localhost ~]$ sudo gitlab-rake cache:clear RAILS_ENV=production

记一次代码迁移过程中遇到的问题1:

情况是迁移代码到新服务器完成之后,所有代码都正常,但是添加SSH公钥的时候,直接返回500报错,查看日志没看到任何异常,后来突然想到会不会是openssh-clients没装

果然安装完openssh-clients之后就立马好了,

yum -y install openssh-clients

问题2:

代码迁移到内网之后,开发人员可能会需要从外网来git clone代码 但是此时ssh的方法肯定是用不了的,ssh连不到内网的gitlab服务器

解决办法就是:

1、使用http协议来克隆,但是http克隆的时候会要求输入账号密码,不是很方便,我们可以在克隆的时候直接指定账号密码

git clone http://账号:密码@IP/.../xxx.git    这样下次提交时也不需要输入密码。

如果你曾经克隆过
那就可以仓库目录下.git/config中的url = http://账号:密码@IP/仓库地址

2、还是使用SSH,但是需要在公司路由器上映射ssh端口,比如你映射的是公网9922端口到内网gitlab服务器22端口,那就可以这样来克隆

git clone ssh://git@IP:9922/.../xxx.git

例如:
git clone ssh://git@10.137.20.113:9922/root/test.git

问题3:

linux yum 安装的git版本太低导致在克隆的时候  报错:

error: The requested URL returned error: 401 Unauthorized while accessing http

解决办法:

指定用户 :git clone http://username:password@IP/.../xxx.git

或者 升级git 版本≥1.7.10

参考链接 https://*.com/questions/12538130/cant-clone-a-github-repo-on-linux-via-https