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

2018-04-10 19:21:31 +08:00
 wsgzao

前言

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

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

更新记录

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 的家族成员

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

Gitlab 的服务构成

GitLab 工作流程

GitLab 安装

在线安装 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 中文社区的项目,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

QQ 邮箱配置

默认情况下,GitLab 用 qq 邮箱注册是发不出确认邮件的。查看了网上很多邮箱配置的教程,大部分都是误导的。像这类软件,归根到底总结为一句话:一切以官网文档为准。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'] = "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'

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 "wangao@test.com" 
# 克隆项目,在本地生成同名目录,并且目录中会有所有的项目文件
git clone git@172.28.70.126: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 中

3605 次点击
所在节点    程序员
3 条回复
ywgx
2018-04-10 19:44:33 +08:00
wwek
2018-04-10 20:37:42 +08:00
docker gitlab 了解一下
liwl
2018-04-11 10:10:40 +08:00
gitea 了解一下

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/445748

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX