首先声明一下:搭建架构时我的IP分配与端口
节点1:gitlab 192.168.0.177
节点2:gerrit http:192.168.0.179:9999
jenkins http://192.168.0.179:8080
如果搭建在一台服务器上面,请注意 gitlab默认会占用80端口和8080端口(这个我也想不明白为什么8080端口也会被占用),所以jenkins需要修改成其他端口
当时搭建的时候,也是参考别人的一个帖子http://www.codesec.net/view/215871.html,但是没有注明IP,很让我头疼,也走好多弯路。
1-1 gitlab搭建
GitLab 是一个使用使用 Ruby on Rails 搭建的,用于仓库管理系统的开源项目。使用 Git 作为代码管理工具,并在此基础上搭建起来的web服务
1.1.2. 环境搭建
a) 第一步:安装相应的在依赖包(postfix或者sendmail),其中选择internet site这种网络smtp邮件服务器这种方式来发送邮件,其他一些依赖包:curl \openssh-server\ ca-certificates\ postfix
If you install Postfix to send email please select 'Internet Site' during setup. Instead of using Postfix you can also use Sendmail or configure a custom SMTP server . If you wish to use Exim, please configure it as an SMTP server.
On Centos 6 and 7, the commands below will also open HTTP and SSH access in the system firewall.
操作命令:
sudo apt-get install curl openssh-server ca-certificates postfix
b) 第二步:Add the GitLab package server and install the package. 下载gitlab安装包,并且安装
方法1、增加yum源链接,使用sduo apt-get install gitlab-ee
Apt-get安装Curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh|sudo bash
操作命令:
sudo apt-get install gitlab-ee
If you are not comfortable installing the repository through a piped script, you can find the entire script here .
Alternatively you can select and download the package manually and install using
方法2、deb包安装下载deb包,然后安装
下载: sudo wget https://packages.gitlab.com/gitlab/gitlab-ee
安装: sudo dpkg -i gitlab-ee_8.1.2-ee.0_amd64.deb
建议一般下载后再装
c)第三步,启用配置,并且启动 GitLab
操作命令:
sudo gitlab-ctl reconfigure
d) 第四步:打开网页,进行注册,使用默认密码
http://192.168.0.177:80
Username: rootPassword: 5iveL!fe
e) 第五步:2.1.6. Upload your license(EE版本需要认证,CE) 这步我操作的时候是没有的(后面发现公司用的CE社区版本,大家请认清想要下载的版本)
Go toAdmin > Licenseand upload your.gitlab-licensefile.
Don't have a license yet? Get a subscription.
1-2 gitlab配置用户的SSH-KEY并创建test项目
第一步:打开gitlab网页:http://192.168.0.177,输入admin用户跟密码,并创建TEST项目
需要做以下动作:
1、在gerrit主机(192.168.0.179),创建用户gerrit,生成ssh-key,并把公钥上传到,gitlab的admin的ssh-key里面去
2、填入admin用户的邮箱,如denghuayi@szprize.com
操作如下:
在gerrit主机(192.168.0.179)上: sudo useradd gerrit 并设置密码 ,然后 su - gerrit后, 创建sshkey(生成私钥和公钥)
$ sudo -u gerrit -H ls /home/gerrit/.ssh
$ sudo -u gerrit -H ssh-keygen -C denghuayi@szprize.com 邮箱为gitlab的admin注册邮箱,两者共用
$ sudo -u gerrit -H cat /home/gerrit/.ssh/id_rsa.pub
以上为生成sshkey的图片
以上为把/home/gerrit/.ssh/id_rsa.pub里面的内容填入到gitlab用户的ssh-key。
起到的作用是,以后gerrit主机的gerrit用户可以直接凭私钥push代码,不需要验证,同时gerrit用户就相当于gitlab的管理员账户了
第二步:在gerrit主机:192.168.0.179中,安装git-review,readline,准备对远程gitlab 仓库的的空项目project增加.review文件,注意要安装依赖包pip \ git-review\readline
1.1.4. Gerrit Review支持前面我们在Gitlab上搭建了一个 test 的工程,普通用户是没有办法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在于项目目录里。
1.1.4.1安装支持库需要安装git-review, readline
Apt-get install git-review
linux :pip install readline
windows:pip install pyreadline
第三步:在gerrit主机上创建gitlab同名的管理员用户(root可以理解为gerrit)
1.1.4.2 用 gerrit(gitlab中的admin)用户给test项目添加 .gitreview 文件a) 拷贝gitlab的test项目,创建.gitreview文件,再push到gitlab仓库(简单来讲,就是用gerrit的主机,给gitlab的空project里面增加一个.gitreview文件)
gerrit@gerrit:~/cfg$ git init cfg
gerrit@gerrit:~/cfg$ git init cfg
gerrit@gerrit:~/cfg/cfg$ git config --global user.name 'root'
gerrit@gerrit:~/cfg/cfg$ git config --global user.email denghuayi@xxx.com
gerrit@gerrit:~/cfg/cfg$ git clone http://192.168.0.177/root/test.git
gerrit@gerrit:~/cfg/cfg$ cd test/
b) 添加gitreview文件 (注意,里面增加的是gerrit服务器的ip,因为reivew的工作是在192.168.0.179上)
gerrit@gerrit:~/cfg/cfg/test$ vim .gitreview
文件内容为:
[gerrit]
host=192.168.0.179
port=29418
project=你在gitlab上面新建的project名
c) 上传gitreview文件
gerrit@gerrit:~/cfg/cfg/test$ git add .gitreview
gerrit@gerrit:~/cfg/cfg/test$ git commit .gitreview -m 'add .gitreview file by admin.'
gerrit@gerrit:~/cfg/cfg/test$ git push origin master
经过以上步骤,gitlab服务器上面的project就有review功能了,这样普通用户就不能直接push代码了。
第四步:验证普通用户无法直接push代码
1、同理,在gerrit主机(192.168.0.179)创建普通用户 tanchun , 生成sshkey
2、同理,在gitlab页面(http://192.168.0.177)创建普通用户 tanchun , 并把上一步生成的sshkey填入,增加邮箱 tanchun@xxxx.com邮箱。
3、在gitlab页面(http://192.168.0.177)里面选择新建一个测试项目test_project1,选择项目时为private,增加tanchun用户 ,并设置他为reporter角色。(此时,测试用户tanchun是无法push的)
在linux系统上登录tanchun账号下,克隆工程test-project1.git,测试权限
[iyunv@115]# su - tanchun
[tanchun@115 ~]$ git clone git@192.168.1.177:dev-group/test-project1.git
Initialized empty Git repository in /home/wangshibo/test-project1/.git/
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 15 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (15/15), done.
[tanchun@115 ~]$ cd ~/test-project1/
[tanchun@115 ~]$ git config --global user.name 'wangshibo'
[tanchun@115 ~]$ git config --global user.email 'wangshibo@xqshijie.cn'
[tanchun@115 ~]$ touch testfile
[tanchun@115 ~]$ git add testfile
[tanchun@115 ~]$ git commit -m 'wangshibo add testfile'
[tanchun@115 ~]$git push
GitLab: You are not allowed to push code to this project.
fatal: The remote end hung up unexpectedly
上面有报错,因为普通用户没有直接push的权限。需要先review到gerrit上进行审核并commit后,才能更新到代码中心仓库里
2-1 gerrit安装
1.2. Gerrit 1.2.1 简介gerrit的工作模式是所有真正的merge由gerrit负责,
我们的提交只是提交给了gerrit,gerrit会通知项目的负责人来审核代码,
同时也会调用一些自动化测试构建工具来检查代码是否符合要求,只有都通过后才会的合并进主干代码。所以可以把gerrit看成是代码和提交者之间的一道门。
1.2.2. 环境搭建a) Jdk安装
安装最新版本Oracle JRE/JDK,使用PPA(personal package archieve)WEBUPD8的方式安装。
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
如果你正在使用OPenJDK,那么先将它移除以免产生任何的冲突
sudo apt-get remove --purge openjdk*
然后使用下面的命令安装Oracle Java 7
sudo apt-get install oracle-java7-installer
b) 添加gerrit用户(已经有了)
adduser gerrit
c) 建立存放代码的目录
mkdir /home/gerrit/gerrit_dir/
d) 安装git
sudo apt-get install git
e) 安装
sudo java -jar gerrit-2.11.4.war init -d /home/gerrit/gerrit_dir/
一路按回车,先以默认方式安装,之后再手动修改配置文件。
f) 配置gerrit
vim review_site/etc/gerrit.config
如下:
[gerrit]
basePath = /home/gerrit/gerrit_dir -----------存放代码的路径
canonicalWebUrl = http://192.168.0.179:8081/ -----------gerrit网页最终要跳转到页面
[database] ---------- 选择默认数据库
type = h2
database = db/ReviewDB
[index] ---------- 默认
type = LUCENE
[auth] -----------必选HTTP
type = HTTP
[sendemail]
enable
smtpServer = smtp.szprize.com
smtpUser = denghuayi@szprize.com
smtpPass = p8368608
from = gerrit_review<denghuayi@szprize.com>
[container] ----------默认
user = gerrit
javaHome = /usr/lib/jvm/java-7-oracle/jre
[sshd] -----------默认,sshd29418是gerrit代码库的clone端口
listenAddress = *:29418
[httpd] -----------默认
listenUrl = proxy-http://*:8081/
[cache] -----------默认
directory = cache
2-2 |