故事背景:
公司計劃搞持續化集成,而從 GitLab 8.0 開始,GitLab CI 就已經集成在 GitLab中,因此我不得不面對一個問題,升級!
目前環境:
系統環境:Centos 6.7x64
軟件版本:Gitlab 源碼安裝7.14.3版本
目標環境:
系統環境:Centos 7.2x64
軟件版本:GitLab 9.3.6 omnibus
升級步驟規劃:
升級7.14.3 源碼安裝到omnibus 7.14.3
安裝新的操作系統CentOS Linux release 7.2.1511 (Core)
在Centos7系統上安裝gitlab omnibus 7.14.3版本
遷移舊數據到新的服務器上
升級gitlab omnibus 7.14.3版 到gitlab omnibus 9.3.6版,並進行測試等內容
背景介紹完畢,開始搞起來!
一、安裝新環境
安裝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 [email protected]:/data0/app/gitlab/backups scp gitlabhq_production.mysql [email protected]:/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文件