4

RHEL7/CentOS7 在线和离线安装 GitLab 配置使用实践

 2 years ago
source link: https://wsgzao.github.io/post/gitlab/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

GitLab Community Edition 新版本已经集成了 CI/CD 的支持,从某种程度上来说可以告别对于 Jenkins 的依赖,我本来计划是写基于 Docker 部署 GitLab 但感觉还是有点重,对于内网离线环境来说基于容器部署也未必是合理的方案,这里沿用传统的部署方式介绍简单且长期有效的 GitLab 部署方案供大家参考,或许未来会增加基于容器的部署方案和 CI/CD 的分享,因需而变。

RHEL7/CentOS7 在线和离线安装 GitLab 配置实践,GitLab 汉化配置使用小结

2018 年 04 月 19 日 - 完善内容
2018 年 04 月 10 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/gitlab/

扩展阅读

GitLab Installation - https://about.gitlab.com/installation

GitLab 简介

GitLab 是利用 Ruby On Rails 开发的一个开源版本管理系统,实现了一个自托管的 Git 项目仓库,是集代码托管,测试,部署于一体的开源 git 仓库管理软件,可通过 web 界面来进行访问公开的或私人项目。与 Github 类似,GitLab 能够浏览代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本,并提供一个文件历史库。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后需要的时候查找。

Git 的家族成员

  • Git:是一种版本控制系统,是一个命令,是一种工具。
  • Gitlib:是用于实现 Git 功能的开发库。
  • Github:是一个基于 Git 实现的在线代码托管仓库,公开项目是免费的,也可以付费创建私人项目。
  • GitLab:是一个基于 Git 实现的在线代码仓库托管软件,可以用 GitLab 搭建一套类似 Github 的系统。

GitLab 对硬件还是有一定要求的,1 核心的 CPU 基本上可以满足需求,大概支撑 100 个左右的用户,不过在运行 GitLab 网站的同时还需要运行多个后台 job,就会显得有点捉襟见肘了。需要至少 4GB 的可寻址内存(RAM 交换)来安装和使用 GitLab,操作系统和任何其他正在运行的应用程序也将使用内存,因此请记住,在运行 GitLab 之前,您至少需要 4GB 的可用空间。如果使用更少的内存,GitLab 将在重新配置运行期间给出奇怪的错误,我用虚拟机来分别新建 1G,2G 内存的 CentOS 系统来装 GitLab,确实非常捉襟见肘啊,伤不起。

Gitlab 的服务构成

  • Nginx:静态 web 服务器
  • gitlab-shell:用于处理 Git 命令和修改 authorized keys 列表
  • gitlab-workhorse: 轻量级的反向代理服务器
  • logrotate:日志文件管理工具
  • postgresql:数据库
  • redis:缓存数据库
  • sidekiq:用于在后台执行队列任务(异步执行)
  • unicorn:An HTTP server for Rack applications,GitLab Rails 应用是托管在这个服务器上面的

GitLab 工作流程

GitLab 安装

yum install curl policycoreutils openssh-server openssh-clients -y
systemctl enable sshd
systemctl start sshd
yum install postfix
systemctl enable postfix

# 检查修改邮件下面 2 项配置,不配置可以跳过
vim /etc/postfix/main.cf

inet_interfaces = all
inet_protocols = ipv4

systemctl start postfix

在线安装 gitlab-ce

# 安装必要的依赖包,如果不需要可以考虑跳过
yum install pygpgme yum-utils
# On CentOS 7 (and RedHat/Oracle/Scientific Linux 7), the commands below will also open HTTP and SSH access in the system firewall.
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
# Next, install Postfix to send notification emails. If you want to use another solution to send emails please skip this step and configure an external SMTP server after GitLab has been installed.
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

# 使用阿里云作加速
cd /etc/yum.repos.d/ && rm -f *.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 添加 GitLab 镜像源并安装,建议切换国内资源加速访问
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

# 创建 gitlab-ce 的 repo,使用清华大学加速
vim /etc/yum.repos.d/gitlab_gitlab-ce.repo

[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

# 配置并启动 GitLab
gitlab-ctl reconfigure

# 第一次访问 GitLab,系统会重定向页面到重定向到重置密码页面,你需要输入初始化管理员账号的密码,管理员的用户名为 root,重置密码后新密码即为刚输入的密码。
0.0.0.0:80

离线安装 gitlab-ce

# 使用阿里云作加速
cd /etc/yum.repos.d/ && rm -f *.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 添加 GitLab 镜像源并安装,建议切换国内资源加速访问
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

# 创建 gitlab-ce 的 repo,使用清华大学加速
vim /etc/yum.repos.d/gitlab_gitlab-ce.repo

[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

# 安装 yum-plugin-downloadonly 插件
yum install -y yum-plugin-downloadonly

# 下载 gitlab-ce 相关 rpm 到指定目录
mkdir -p /tmp/repo/gitlab-ce/
yum install --downloadonly --downloaddir=/tmp/repo/gitlab-ce/ gitlab-ce

# 拷贝文件至内网离线安装
rpm -ivh /tmp/repo/gitlab-ce/*

# 配置并启动 GitLab
gitlab-ctl reconfigure

# 第一次访问 GitLab,系统会重定向页面到重定向到重置密码页面,你需要输入初始化管理员账号的密码,管理员的用户名为 root,重置密码后新密码即为刚输入的密码。
0.0.0.0:80

GitLab 汉化

如果团队里英文水平都不错的话,是没必要汉化的,我个人的建议是坚持使用原版不做汉化

我这边测试 GitLab 10.6.3 新版本中官方已经集成了简体中文,虽然不完整但应该是一个好的开端,设置方式如下
右上角用户头像 - Settings-Profile-Main settings-Preferred language - 简体中文
保存选择 Update profile settings,然后 Sign out 重新登录就可以看到效果了
当然我还是希望大家习惯 English 界面,Trouble Shooting 也方便

GitLab 中文社区的项目,v7-v8.8 是由 Larry Li 发起的“GitLab 中文社区版项目”
https://gitlab.com/larryli/gitlab

从 v8.9 之后由 @xhang 开始继续汉化项目
https://gitlab.com/xhang/gitlab

# 如没安装 git,需提前安装 
yum install -y git

# 创建 gitlba 汉化包下载目录
mkdir -p /tmp/gitlab
cd /tmp/gitlab

# 下载最新的汉化包
git clone https://gitlab.com/xhang/gitlab.git

# 如果是要下载老版本的汉化包,需要加上老版本的分支,如果想下载 10.0.2,可以运行如下语句
git clone https://gitlab.com/xhang/gitlab.git -b v10.0.2-zh

# 停止 GitLab 并执行如下语句
gitlab-ctl stop
cp /tmp/gitlab/gitlab/* /opt/gitlab/embedded/service/gitlab-rails/ -rf

# 复制时可能不断提示是否要覆盖,这时可能是系统每次执行 cp 命令时,其实是执行了 cp -i 命令的别名。出现这种情况可以修改~/.bashrc,在“alias cp=’cp-i’” 前加 #注释即可。使用命令或者注销登录即可
source ~/.bashrc

# 接下来可以重新配置和启动
gitlab-ctl reconfigure
gitlab-ctl restart

GitLab 常用命令

# 语法
gitlab-ctl command (subcommand)

Service Management Commands

start # 启动所有服务
stop # 关闭所有服务
restart # 重启所有服务
status # 查看所有服务状态
tail # 查看日志信息
service-list # 列举所有启动服务
graceful-kill # 平稳停止一个服务

# 启动所有服务
gitlab-ctl start
# 启动单独一个服务
gitlab-ctl start nginx
# 查看日志,查看所有日志
gitlab-ctl tail
# 查看具体一个日志, 类似 tail -f
gitlab-ctl tail nginx

General Commands

help # 帮助
reconfigure # 修改配置文件之后,需要重新加载下
show-config # 查看所有服务配置文件信息
uninstall # 卸载这个软件
cleanse # 删除 gitlab 数据,重新白手起家

# 显示所有服务配置文件
gitlab-ctl show-config

# 卸载 gitlab
gitlab-ctl uninstall

公网邮箱配置

默认情况下,GitLab 用 qq 邮箱注册是发不出确认邮件的。查看了网上很多邮箱配置的教程,大部分都是误导的。像这类软件,归根到底总结为一句话:一切以官网文档为准。QQ 邮箱最好用企业邮箱,本人用个人邮箱进行测试是有些小问题的,正确配置如下:

其他邮箱可以参考官方给出的 SMTP 配置 SMTP Setting
https://docs.gitlab.com/omnibus/settings/smtp.html

QQ 企业邮箱

# 编辑 gitlab.rb
vim /etc/gitlab/gitlab.rb

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "[email protected]"
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'] = '[email protected]'

163 邮箱

# 编辑 gitlab.rb
vim /etc/gitlab/gitlab.rb

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "[email protected]"
gitlab_rails['smtp_password'] = "123456"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['gitlab_email_from'] = '[email protected]'
gitlab_rails['smtp_tls'] = false
gitlab_rails['smtp_openssl_verify_mode'] = false

阿里云企业邮箱

# 编辑 gitlab.rb
vim /etc/gitlab/gitlab.rb

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.mxhichina.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "[email protected]"
gitlab_rails['smtp_password'] = "************"
gitlab_rails['smtp_domain'] = "aliyun.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['gitlab_email_from'] = '[email protected]'

GitLab 使用

登录 GitLab

  1. 在浏览器的地址栏中输入 IP 即可登录 GitLab 的界面,老版本第一次登录使用的用户名和密码为 root5iveL!fe
  2. 首次登录会强制用户修改密码。密码修改成功后,输入新密码进行登录

创建 Project

  1. 安装 Git 工具 linux:安装 Git,使用自带的源安装

    yum install git

  2. 生成密钥文件:使用 ssh-keygen 生成密钥文件. ssh/id_rsa.pub。

    ssh-keygen -t rsa

  3. 在 GitLab 的主页中新建一个 Project

  4. 添加 ssh key 导入步骤 2 中生成的密钥文件内容:

    Profile Settings => SSH Keys => Add SSH key

# 配置使用 Git 仓库的人员姓名
git config --global user.name "wangao"
# 配置使用 Git 仓库的人员 email,填写自己的公司邮箱
git config --global user.email "[email protected]"
# 克隆项目,在本地生成同名目录,并且目录中会有所有的项目文件
git clone [email protected]:root/test.git
# 进入到项目目录
cd test/
# 创建需要上传到 GitLab 中的目标文件
echo "test" > test.sh
# 将 test.sh 文件加入到索引中
git add test.sh
# 将 test.sh 提交到本地仓库
git commit -m "test.sh"
# 将文件同步到 GitLab 服务器上
git push -u origin master
# 在网页中查看上传的 test.sh 文件已经同步到 GitLab 中

配置 GitLab

设置外部访问链接

# 编辑 /etc/gitlab/gitlab.rb
vim /etc/gitlab/gitlab.rb

# 修改第 13 行,改成 http(s)://ip(: 端口) 或者 http(s):// 域名 (: 端口),(如果云主机上存在 Web 服务占用了 80,443 端口,则 GitLab 不能适用 80,443),比如:
external_url "https://repo.gitlab.com"

HTTPS 配置

# 第 513 行开始,将 SSL 证书以及密钥放入 / etc/gitlab/ssl/
nginx['enable'] = true
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/server.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/server.key"
# 使配置生效
sudo gitlab-ctl reconfigure

# 开启 gitlab
sudo gitlab-ctl start

# 查看状态
sudo gitlab-ctl status

GitLab 备份升级迁移

GitLab 备份

ansible-rhel7.4@JQ/opt/gitlab/etc#gitlab-rake gitlab:backup:create
Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
* root/ansible ... [DONE]
* root/ansible.wiki ... [SKIPPED]
done
Dumping uploads ...
done
Dumping builds ...
done
Dumping artifacts ...
done
Dumping pages ...
done
Dumping lfs objects ...
done
Dumping container registry images ...
[DISABLED]
Creating backup archive: 1524016954_2018_04_18_10.6.3_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping

ansible-rhel7.4@JQ/opt/gitlab#cd /var/opt/gitlab/backups
ansible-rhel7.4@JQ/var/opt/gitlab/backups#ll
total 22372
-rw------- 1 git git 22906880 Apr 18 10:02 1524016954_2018_04_18_10.6.3_gitlab_backup.tar

GitLab 升级

# 停止 gitlab 相关服务
gitlab-ctl stop

# 下载离线包升级 gitlab,建议不要跨 3 个以上大版本升级,具体升级方案请参考官方文档
rpm -Uvh gitlab-ce-10.6.3-ce.0.el7.x86_64.rpm

# 重新配置 gitlab
gitlab-ctl reconfigure

# 启动 gitlab
gitlab-ctl start

GitLab 迁移

  1. GitLab 迁移的前提是新旧机器 GitLab 版本必须一致
  2. 在旧机器备份, 默认会在 / var/opt/gitlab/backups/ 创建备份文件 #gitlab-rake gitlab:backup:create
  3. 复制备份文件到新机器 /var/opt/gitlab/backups/
  4. 在新机器运行下面命令, 停止相关数据连接服务, 然后从备份文件恢复
  5. 启动 gitlab
# 停止 gitlab
gitlab-ctl stop
# 从 1524016954 编号备份中恢复
gitlab-rake gitlab:backup:restore BACKUP=1524016954
# 启动 gitlab
gitlab-ctl start

GitLab 常见问题

更改管理员密码

gitlab-rails console production

Loading production environment (Rails 4.8.5.2)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id: 1, email: "[email protected]", ...
irb(main):002:0> user.password=123456
=> 123456
irb(main):003:0> user.password_confirmation=123456
=> 123456
irb(main):004:0> user.save!
=> true
irb(main):005:0> quit

The requested URL returned error: 401 while accessing

这里遇到的问题是因为需要在参数里增加授权后的用户名和密码,不排除可能因为其它问题引起相同报错,如果是 403 可能是私有仓库没有授权导致

(ox-zRbE61dW) test101@JQ/home/ox/git$git clone http://172.31.96.248/wangao/ansible.git
Initialized empty Git repository in /home/ox/git/ansible/.git/
error: The requested URL returned error: 401 while accessing http://172.31.96.248/wangao/ansible.git/info/refs

fatal: HTTP request failed
(ox-zRbE61dW) test101@JQ/home/ox/git$git clone http://guest:[email protected]/wangao/ansible.git
Initialized empty Git repository in /home/ox/git/ansible/.git/
remote: Counting objects: 257, done.
remote: Compressing objects: 100% (153/153), done.
remote: Total 257 (delta 91), reused 254 (delta 91)
Receiving objects: 100% (257/257), 21.77 MiB | 19.18 MiB/s, done.
Resolving deltas: 100% (91/91), done.
(ox-zRbE61dW) test101@JQ/home/ox/git$

webhooks 不生效

https://gitlab.com/gitlab-org/gitlab-ce/issues/45334
https://gitlab.com/gitlab-org/omnibus-gitlab/issues/3307#note_64245578

Admin Area - Settings - Outbound request - 勾选以下内容
Allow request to the local network from hooks and services


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK