Fork me on GitHub

GitLab应用总结

GitLab介绍

GitLab简介

GitLab 是一个用于仓库管理系统的开源项目。即使用git作为代码管理工具,并在此基础上搭建起来的web服务。

GitLab特点

1、web框架使用ruby on rails
2、基于MIT代码发布协议
3、需要gitolite协调工作

GitLab架构

1、前端:Nginx,用于git tool走http或https协议
2、后端:gitlab服务,采用ruby on Rails框架,通过unicorn实现后台服务及多进程
3、SSHD:开启sshd服务,用于用户上传ssh key进行版本克隆及上传。注:用户上传的ssh key是保存到git账户中
4、数据库:目前仅支持MySQL和PostgreSQL
5、Redis:用于存储用户session和任务,任务包括新建仓库、发送邮件等等
6、Sidekiq:Rails框架自带的,订阅redis中的任务并执行

GitLab部署配置(RPM安装)

GitLab资料

官网部署说明:
https://about.gitlab.com/downloads/#centos6
http://www.gitlab.cc/downloads/#centos6(中文)

手册:http://doc.gitlab.com/omnibus/
资料:http://www.tuicool.com/articles/ZR7rmuz

官网支持的方式:
1)包含一切的rpm包(官方推荐)
https://about.gitlab.com/downloads/
2)手动安装(深入了解)
https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md
3)第三方docker镜像
https://github.com/sameersbn/docker-gitlab
资料:http://www.tuicool.com/articles/bYbi2mJ

安装配置依赖项

如果已经安装了Postfix来发送邮件请在安装期间选择 ‘Internet Site’. 你也可以用Sendmail或者使用自定义的SMTP服务器来代替Postfix. 如果希望使用 Exim, 请把它当做SMTP来配置.在Centos 6和7上, 下面的命令也会配置系统防火墙,把HTTP和SSH端口开放.

1
2
3
4
yum install curl openssh-server postfix cronie
service postfix start
chkconfig postfix on
lokkit -s http -s ssh

添加并安装GitLab软件包

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

如果不习惯这种通过管道命令安装的方式,可以在这里找到完整的安装脚本.或者你可以选择对应系统的GitLab安装包 并使用下面的命令进行安装

1
2
curl -LJO http://mirror.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-XXX.rpm/download
rpm -i gitlab-ce-XXX.rpm

(推荐使用gitlab-ce镜像站,下载后保留使用300M左右)
国内yum源:
https://mirror.tuna.tsinghua.edu.cn/gitlab-ce/

配置和使用GitLab

1
gitlab-ctl reconfigure

在浏览器访问GitLab主机名

1
2
Username: root
Password: 5iveL!fe

修改域名

默认的配置文件保存在 /etc/gitlab/gitlab.rb ,执行:

1
2
3
vim /etc/gitlab/gitlab.rb
external_url 'http://your.domain'
gitlab-ctl reconfigure

有一定概率出现 502 错误,刷新浏览器或者再次更新配置即可。

界面汉化(测试通过)

问题:保证版本一致性,否则上传代码会触发bug
由于服务对象是广大师生,为了降低新手上手的难度,所有进行汉化也是非常有必要的。好在国内有人已经进行了这方面的工作,我们只需要共享其成果即可(欢迎向原项目提交高质量翻译)。
首先确认版本:

1
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

并确认当前汉化版本的VERSION 是否相同,当前最新的汉化版本为8.6。如果安装版本小于当前汉化版本,请先升级。如果安装版本大于当前汉化版本,请在本项目中提交新的issue。如果版本相同,首先在本地 clone 仓库。
GitLab.com 仓库

1
git clone https://gitlab.com/larryli/gitlab.git

Coding.net 镜像

1
git clone https://git.coding.net/larryli/gitlab.git

根据我的测试,Coding.net的镜像不完整,clone之后无法checkout。然后比较汉化分支和原分支,导出 patch用的diff文件。

8.1 版本的汉化补丁

1
git diff origin/8-6-stable..8-6-zh > ../8.6.diff   (已经生成可以直接进行下面的操作)

然后上传 8.6.diff 文件到服务器。
停止 gitlab

1
2
gitlab-ctl stop
patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < 8.6.diff

确定没有.rej文件,重启 GitLab即可。

1
gitlab-ctl start

如果汉化中出现问题,请重新安装 GitLab( 注意备份数据 )。

GitLab管理

启动、停止、重启组件

1
2
3
gitlab-ctl start    启动所有 GitLab 组件
gitlab-ctl stop 停止所有 GitLab 组件
gitlab-ctl restart 重启所有 GitLab 组件

常用管理命令

相关操作

1
2
3
4
5
6
7
8
9
10
gitlab-ctl reconfigure       如果更改了主配置文件 [gitlab.rb文件],使配置文件生效 但是会初始化除了gitlab.rb之外的所有文件
gitlab-ctl show-config 验证配置文件
gitlab-ctl restart 重启gitlab服务
gitlab-ctl stop unicorn 停止组件内某一个服务
gitlab-ctl status unicorn 查看状态
gitlab-ctl kill unicorn kill掉某一个服务
gitlab-ctl status unicorn 再次查看状态
gitlab-ctl start unicorn 启动服务
gitlab-rake gitlab:env:info 系统信息监测
gitlab-rake gitlab:check 各种状态监测

相关目录

1
2
3
4
5
6
/var/opt/gitlab/git-data/repositories/root 库默认存储目录
/opt/gitlab 是gitlab的应用代码和相应的依赖程序
/var/opt/gitlab 此目录下是运行gitlab-ctl reconfigure命令编译后的应用数据和配置文件,不需要人为修改配置
/etc/gitlab 此目录下存放了以omnibus-gitlab包安装方式时的配置文件,这里的配置文件才需要管理员手动编译配置
/var/log/gitlab 此目录下存放了gitlab各个组件产生的日志
/var/opt/gitlab/backups/ 备份文件生成的目录

备份和恢复

备份

备份GitLab repositories and GitLab metadata
在 crontab 中加入如下命令:

1
0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create

案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@vm10-253-26-148 gitlab-workhorse]# /usr/bin/gitlab-rake gitlab:backup:create
Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
* root/jsws ... [DONE]
* root/jsws.wiki ... [SKIPPED]
done
Dumping uploads ...
done
Dumping builds ...
done
Dumping artifacts ...
done
Dumping lfs objects ...
done
Creating backup archive: 1461569556_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
Deleting old backups ... Skipping

默认生成备份文件的路径:

1
2
3
4
5
6
[root@vm10-253-26-148 gitlab-workhorse]# cd /var/opt/gitlab/backups/
[root@vm10-253-26-148 backups]# ls -al
total 88
drwx------ 2 git root 4096 Apr 25 15:32 .
drwxr-xr-x 13 root root 4096 Apr 22 15:25 ..
-rw------- 1 git git 81920 Apr 25 15:32 1461569556_gitlab_backup.tar

注:备份文件是一个归档文件,且开头是unix时间
在实际的生产环境中备份策略建议:本地保留三天,在异地备份永久保存
这里以unix时间戳来标记备份的时间,这个时间戳对人来说不好读懂,可使用date命令把其转换成人可读的格式,如下:

1
2
root@test1:~/backup# date -d @1453432213
2016年 01月 22日 星期五 11:10:13 CST

修改备份目录

1
2
3
4
root@test1:~/backup# vim /etc/gitlab/gitlab.rb:
gitlab_rails['backup_path'] = "/data/git-backups"
# limit backup lifetime to 7 days - 604800 seconds
gitlab_rails['backup_keep_time'] = 604800

创建备份目录,修改属主和属组:

1
2
root@test1:~# mkdir /data/git-backups
chown -R git.git /data/git-backups

手动进行一次备份,测试一下备份是否有效,测试备份正常,添加定时任务即可

恢复

首先进入备份 GitLab 的目录,这个目录是配置文件中的 gitlab_rails[‘backup_path’] ,默认为 /var/opt/gitlab/backups 。然后停止 unicorn 和 sidekiq ,保证数据库没有新的连接,不会有写数据情况。

1
2
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

然后恢复数据,1406691018为备份文件的时间戳

1
gitlab-rake gitlab:backup:restore BACKUP=1406691018

GitLab命令

1
2
3
4
5
6
7
8
9
10
11
[root@vm10-253-26-148 ~]# git
git
gitk
gitlab-rails
git-receive-pack
git-upload-archive
git-cvsserver
gitlab-ctl # GitLab服务管理
gitlab-rake
git-shell
git-upload-pack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@vm10-253-26-148 ~]# gitlab-ctl
I don\'t know that command.
/opt/gitlab/embedded/bin/omnibus-ctl: command (subcommand)
deploy-page
Put up the deploy page
remove-accounts
Delete *all* users and groups used by this package
upgrade
Run migrations after a package upgrade
General Commands:
cleanse
Delete *all* gitlab data, and start from scratch.
help
Print this help message.
reconfigure
Reconfigure the application.
show-config
Show the configuration that would be generated by reconfigure.
uninstall
Kill all processes and uninstall the process supervisor (data will be preserved).
Service Management Commands:
graceful-kill
Attempt a graceful stop, then SIGKILL the entire process group.
hup
Send the services a HUP.
int
Send the services an INT.
kill
Send the services a KILL.
once
Start the services if they are down. Do not restart them if they stop.
restart
Stop the services if they are running, then start them again.
service-list
List all the services (enabled services appear with a *.)
start
Start services if they are down, and restart them if they stop.
status
Show the status of all the services.
stop
Stop the services, and do not restart them.
tail
Watch the service logs of all enabled services.
term
Send the services a TERM.

gitlab-ce官方文档查看备录(重要)

文章地址:http://www.178linux.com/7985

邮箱设置

1)GitLab中使用postfix进行邮件发送。因此,可以卸载系统中自带的sendmail。
使用yum list installed查看系统中是否存在sendmail,若存在,则使用yum remove sendmail指令进行卸载。
2)测试系统是否可以正常发送邮件。

1
echo "Test mail from postfix" | mail -s "Test Postfix" [email protected]

注:上面的[email protected]为你希望收到邮件的邮箱地址。

当邮箱收到系统发送来的邮件时,将系统的地址复制下来,如:[email protected],打开/etc/gitlab/gitlab.rb,将
gitlab_rails[‘gitlab_email_from’] = ‘[email protected]
修改为
gitlab_rails[‘gitlab_email_from’] = ‘[email protected]
保存后,执行sudo gitlab-ctl reconfigure重新编译GitLab。如果邮箱的过滤功能较强,请添加系统的发件地址到邮箱的白名单中,防止邮件被过滤。
Note:系统中邮件发送的日志可通过tail /var/log/maillog命令进行查看。

开启HTTPS

FQA

1)在浏览器中访问GitLab出现502错误
原因:内存不足。
解决办法:检查系统的虚拟内存是否随机启动了,如果系统无虚拟内存,则增加虚拟内存,再重新启动系统。
2)80端口冲突
原因:Nginx默认使用了80端口。
解决办法:为了使Nginx与Apache能够共存,并且为了简化GitLab的URL地址,Nginx端口保持不变,修改Apache的端口为4040。这样就可以直接用使用ip访问Gitlab。而禅道则可以使用4040端口进行访问,像这样:xxx.xx.xxx.xx:4040/zentao。具体修改的地方在/etc/httpd/conf/httpd.conf这个文件中,找到Listen 80这一句并将之注释掉,在底下添加一句Listen 4040,保存后执行service httpd restart重启apache服务即可。

#Listen 80
Listen 4040
3)8080端口冲突
原因:由于unicorn默认使用的是8080端口。
解决办法:打开/etc/gitlab/gitlab.rb,打开# unicorn[‘port’] = 8080的注释,将8080修改为9090,保存后运行sudo gitlab-ctl reconfigure即可。
4)STMP设置
配置无效,暂时不知道原因。
5)GitLab头像无法正常显示
原因:gravatar被墙
解决办法:
编辑 /etc/gitlab/gitlab.rb,将

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

然后在命令行执行:

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

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/885609a5.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!