故事背景:

    公司计划搞持续化集成,而从 GitLab 8.0 开始,GitLab CI 就已经集成在 GitLab中,因此我不得不面对一个问题,升级!

目前环境:

    系统环境:Centos 6.7x64

    软件版本:Gitlab 源码安装7.14.3版本

目标环境:

    系统环境:Centos 7.2x64

    软件版本:GitLab 9.3.6 omnibus

升级步骤规划:

  1. 升级7.14.3 源码安装到omnibus 7.14.3

    1. 安装新的操作系统CentOS Linux release 7.2.1511 (Core) 

    2. 在Centos7系统上安装gitlab omnibus 7.14.3版本

    3. 迁移旧数据到新的服务器上

  2. 升级gitlab  omnibus 7.14.3版 到gitlab omnibus 9.3.6版,并进行测试等内容




背景介绍完毕,开始搞起来!

一、安装新环境

  1. 安装Centos 7.2系统(略,详情参考百度或者google)

       1)为了避免更换服务器导致大家的known_hosts失效,需要将原gitlab服务/etc/ssh/ssh_host_rsa_key*两个文件复制到新服务器上

       2)同时需要绑定hosts:127.0.0.1 gitlab.xxx.com到本机,以防之后的一些操作影响正常环境

二、在新系统部署gitlab omnibus 7.14.3版本

    2.1. 安装依赖ruby 2.3.0 、git-1.8.4

# 安装git 1.8.4
cd /data0/download/
wget https://github.com/git/git/archive/v1.8.4.tar.gz
tar xf v1.8.4.tar.gz
cd git-1.8.4
make prefix=/usr/local/git all
make prefix=/usr/local/git install

echo 'export PATH=/usr/local/git/bin:$PATH' >> /etc/profile
source /etc/profile

# 安装ruby 2.3.0
yum -y install gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel sqlite-devel
 # install RVM
curl -L get.rvm.io | bash -s stable

source /etc/profile.d/rvm.sh
rvm reload
rvm requirements run
 # intall 
rvm install 2.3.0 --disable-binary
rvm use 2.3.0 --default
ruby --version
# 输出ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]表示安装成功

    2.2. 安装gitlab omnibus 7.14.3版本

    2.2.1 安装依赖

yum -y install curl openssh-server openssh-clients postfix cronie
chkconfig postfix on
service postfix start
lokkit -s http -s ssh # 待定,防火墙已关闭,所以不需要执行

    2.2.2 安装Gitlab前的准备

# 获取安装包
wget -O gitlab-ce-7.14.3-ce.0.el7.x86_64.rpm https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-7.14.3-ce.0.el7.x86_64.rpm/download 

# 创建程序目录(未来程序安装到/var/opt/gitlab下,为了防止仓库、日志等信息占据根目录太大空间,通过软链的方式链接到该目录,实际消耗数据盘空间)
mkdir /data0/app/gitlab
ln -s /data0/app/gitlab /var/opt/

mkdir /data0/logs/gitlab/ 
ln -s /data0/logs/gitlab /var/log/   #默认gitlab日志在/var/log/gitlab下

    2.2.3 开始安装

rpm -ivh gitlab-ce-7.14.3-ce.0.el7.x86_64.rpm

sudo gitlab-ctl reconfigure #执行完这条命令后,gitlab已经开始运行
# 接下来修改配置文件/etc/gitlab/gitlab.rb 
cd /etc/gitlab
cp gitlab.rb gitlab.rb.default # 备份源文件
vi gitlab.rb 
...过程略,配置文件需要根据个人公司需求进行修改...
# 配置完成后,再次运行sudo gitlab-ctl reconfigure 即可完成配置更新

    此时的gitlab omnibus 7.14.3 已经安装完成,只不过里边空空如夜,没有数据而已。下一步就是迁移数据

三、迁移

3.1 停止新系统,备份数据

# 以下操作以git用户执行,并且git具有sudo权限
sudo /etc/init.d/gitlab stop
bundle exec rake gitlab:backup:create RAILS_ENV=production
# 执行完成之后,会在/home/git/gitlab/tmp/backups目录下生成备份文件,名称为1499741162_gitlab_backup.tar的格式
# 备份数据库
mysqldump --compatible=postgresql --default-character-set=utf8 -r gitlabhq_production.mysql -u root gitlab -p

3.2 将备份数据传输到新服务器

scp 1499741162_gitlab_backup.tar root@172.28.48.35:/data0/app/gitlab/backups
scp gitlabhq_production.mysql root@172.28.48.35:/data0/app/gitlab/backups

3.3 在新服务器上将mysqldump 文件转换为Postgresql 文件(omnibus按照的gitlab使用Postgresql做数据库,并且官方也推荐这么做)

# root身份执行
cd /data0/app/gitlab/backups
mdkir postgresql
mv 1499741162_gitlab_backup.tar gitlabhq_production.mysql postgresql/
cd postgresql
git clone https://github.com/gitlabhq/mysql-postgresql-converter.git -b gitlab
mkdir db
# 还需要修改db_converter.py文件,里边的第25行:num_lines = int(subprocess.check_output(["wc", "-l", input_filename]).strip().split()[0]) 会执行错误,当然也可以执行定义num_lines为shell下获取的wc -l db/database.sql的值
python mysql-postgresql-converter/db_converter.py gitlabhq_production.mysql db/database.sql
ed -s db/database.sql < mysql-postgresql-converter/move_drop_indexes.ed
gzip db/database.sql
tar rf 1499741162_gitlab_backup.tar db/database.sql.gz # 将数据库文件一起打包入备份文件
chmod 777 1499741162_gitlab_backup.tar
mv 1499741162_gitlab_backup.tar ../

3.4 恢复备份

LC_ALL="en_US.UTF-8" sudo gitlab-rake gitlab:backup:restore BACKUP=1499741162
chmod -R ug+rwX,o-rwx /var/opt/gitlab/git-data/repositories
chmod -R ug-s /var/opt/gitlab/git-data/repositories
find /var/opt/gitlab/git-data/repositories -type d -print0 | sudo xargs -0 chmod g+s
sudo gitlab-rake gitlab:satellites:create RAILS_ENV=production
sudo gitlab-ctl restart
sudo gitlab-rake gitlab:check

    执行到这步,数据已经成功迁移到了新的环境中。接下来测试发现如下几个问题,

问题1:需要将就服务器的gitlab用户的authorized_keys文件转移到新服务器,并且修改文件中gitlab-shell路径:/opt/gitlab/embedded/service/gitlab-shell/bin/gitlab-shell


问题2:gitlab pull正常,但push无法提交:

  现象:git push时报“The project you were looking for could not be found.”的错误

解决:编辑/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/gitaccess.rb 第60行,将return buildstatusobject(false, 'The project you were looking for could not be found.') 改为 return buildstatus_object(true) 恢复。这应该是一个BUG,但是升级到9.3.6版本后,发现这个文件已经被移除,不存在更新问题了。

    

    至此,新系统环境下的gitlab omnibus 7.14.3 就可以开始工作了。如果还有其他问题,需要单独解决。


四、升级到GitLab 9.3.6版本

# 备份
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx
sudo gitlab-rake gitlab:backup:create
# 升级(官方说,升级过程中,最好保持启动状态,我是关闭状态下升级的,也没问题)
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo touch /etc/gitlab/skip-auto-migrations
sudo yum update  gitlab-ce

sudo gitlab-ctl pg-upgrade
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

#系统优化
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled
sysctl -p


    整个升级完成。

另外,为了让gitlab所有日志都存放规范化,我还更改了服务日志的路径,

cd /opt/gitlab/ && find . -name "run" -type f  |xargs -i sed -i 's#/var/log#/data0/logs#g' {}


五、安装gitlab-ci-multi-runner

1. 获取仓库地址
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
2. 安装
yum install gitlab-ci-multi-runner
3. 注册到Gitlab,由于我是admin,因此我创建的shared runner,单独项目建议创建special runner
gitlab-ci-multi-runner register
4. 本地服务注册
mkdir /data0/build && chown -R lhop.lhop /data0/build && gitlab-ci-multi-runner install --user lhop --working-directory /data0/build
# 之后的所有的集成构建任务,都会在/data0/build/目录下进行生成,确保user 用户对于/data0/build目录具有项目部署

之后就没啥了,持续集成方面的其他配置,需要自己去琢磨了。另外需要注意的地方是,默认情况下,非tag提交的项目,是不会进行集成构建的,会提示错误job is struk xxxx。

    如果想让没有打tag标签的项目也进行构建,需要修改admin area->runners->edit->Run untagged jobs 打对√



# Git 迁移参考链接

http://www.linuxidc.com/Linux/2016-04/130194.htm  GitLab 7.9 升级到 8.0.1

https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/update/7.14-to-8.0.md

https://gitlab.com/jiaoyiping/gitlab/blob/8-13-5-zh/doc/install/installation.md

http://www.cnblogs.com/jiaoyiping/p/6112290.html   生产环境的gitlab大版本升级思路(从7.x升级到8.x)

https://docs.gitlab.com/omnibus/

https://docs.gitlab.com.cn/omnibus/README.html

http://opjasee.com/2016/01/28/gitlab-upgrade.html  Gitlab升级记录


# gitlab-runner相关参考地址

https://gitlab.com/gitlab-org/gitlab-ci-multi-runner

https://docs.gitlab.com/ce/ci/runners/#registering-a-shared-runner 注册runner

http://scarletsky.github.io/2016/07/29/use-gitlab-ci-for-continuous-integration/ Gitlab-CI 相关概念介绍

http://www.jianshu.com/p/2b43151fb92e  GitLab-CI与GitLab-Runner的关系,以及创建、shared 和special runner的区别

http://doc.gitlab.com/ce/ci/yaml/README.html 如何写自动构建的 gitlab-ci.yml文件