在基本环境的基础上,须要用到:java
本例以PostgreSQL项目为例git
1.1安装 replication 插件
若是安装Gerrit时安装了replication插件,能够忽略这一项。
插件安装:web
[gerrit@localhost ~]$ java -jar gerrit-2.12.4.war init -d gerrit_site --batch --install-plugin replication
1.2 建立postgresql项目
该项目事先存在于Gitolite中sql
[gerrit@localhost git]$ pwd /home/gerrit/gerrit_site/git [gerrit@localhost git]$ ssh -p 29418 -i ~/.ssh/id_rsa 192.168.81.186 -l gerrit gerrit create-project postgresql [gerrit@localhost git]$ ll total 0 drwxrwxr-x. 7 gerrit gerrit 100 Jan 25 02:56 All-Projects.git drwxrwxr-x. 7 gerrit gerrit 100 Jan 25 02:56 All-Users.git drwxrwxr-x. 7 gerrit gerrit 100 Jan 29 04:35 postgresql.git [gerrit@localhost git]$ rm -rf postgresql.git/ [gerrit@localhost git]$ git clone --bare git@127.0.0.1:postgresql.git Cloning into bare repository 'postgresql.git'... remote: Counting objects: 515391, done. remote: Compressing objects: 100% (78522/78522), done. remote: Total 515391 (delta 434772), reused 515391 (delta 434772) Receiving objects: 100% (515391/515391), 151.21 MiB | 6.49 MiB/s, done. Resolving deltas: 100% (434772/434772), done.
1.3 添加replication.config给gerritshell
[gerrit@localhost ~]$ cd gerrit_site/etc/ [gerrit@localhost etc]$ vi replication.config
[remote "postgresql"] projects = postgresql url = git@127.0.0.1:postgresql.git push = +refs/heads/*:refs/heads/* push = +refs/tags/*:refs/tags/* push = +refs/changes/*:refs/changes/* threads = 3
1.4 设置gerrit用户的 ~/.ssh/configssh
[gerrit@localhost ~]$ sh -c "ssh-keyscan -t rsa 127.0.0.1 >> /home/gerrit/.ssh/known_hosts" [gerrit@localhost ~]$ sh -c "ssh-keygen -H -f /home/gerrit/.ssh/known_hosts"
1.5.从新启动 Gerrit 服务curl
[gerrit@localhost ~]$ /home/gerrit/gerrit_site/bin/gerrit.sh restart
2.1 安装Generic Webhook Trigger Plugin 插件
系统管理 =》管理插件 =》可选插件
使用过滤功能找到"Generic Webhook Trigger Plugin"插件并安装(因为我已经安装好了,所在不显示出来,能够在已安装找到)
2.2 新建PostgreSQL项目
构建触发器配置项目触发条件,勾选Generic Webhook Trigger,在此使用Request parameters,填写自定义的参数和值
构建 =》Execute shell
简单编写一个clone代码而后编译的动做post
git clone git@127.0.0.1:postgresql cd postgresql ./configure --prefix=$WORKSPACE/pgsql make make install
2.3 获取API Token
在“系统管理–管理用户–用户列表” 点击用户后面的设置图标,获取 API Token
测试
添加 hook
进入postgresql项目的hooks目录,添加post-receive,使用curl命令发送post请求url
[git@localhost hooks]$ pwd /home/git/repositories/postgresql.git/hooks [git@localhost hooks]$ touch post-receive [git@localhost hooks]$ vi post-receive
#!/bin/sh curl "http://highgo:8703ec11eeea10bb0be850d76c5ef2f7@192.168.81.186:8080/generic-webhook-trigger/invoke?start=1" # 命令参数说明:http://用户名:API_Token@IP:端口//generic-webhook-trigger/invoke?自定义参数=参数值
[git@localhost hooks]$ chmod +x post-receive
修改项目权限,普通用户不能写入
[git@localhost conf]$ pwd /home/git/gitolite-admin/conf [git@localhost conf]$ vi gitolite.conf
repo gitolite-admin RW+ = admin repo postgresql R = @all RW+ = admin gerrit repo testing RW+ = @all
提交修改
[git@localhost conf]$ git add gitolite.conf [git@localhost conf]$ git commit -am "change gitolite.conf" [git@localhost conf]$ git push origin master
用户clone代码,修改远程仓库到gerrit,修改提交
[highgo@localhost Desktop]$ git clone git@127.0.0.1:postgresql [highgo@localhost Desktop]$ cd postgresql/ [highgo@localhost postgresql]$ touch testfile.txt [highgo@localhost postgresql]$ git remote rm origin [highgo@localhost postgresql]$ git remote add origin ssh://highgo@192.168.81.186:29418/postgresql.git [highgo@localhost postgresql]$ git config user.name highgo [highgo@localhost postgresql]$ git config user.email highgo@test.com [highgo@localhost postgresql]$ git config remote.origin.push refs/heads/*:refs/for/* [highgo@localhost postgresql]$ scp -p -P 29418 highgo@192.168.81.186:hooks/commit-msg .git/hooks/ [highgo@localhost postgresql]$ git add testfile.txt [highgo@localhost postgresql]$ git commit -am "add testfile.txt" [highgo@localhost postgresql]$ git push
gerrit审核提交并经过
git通知Jenkins执行项目的编译安装 到此完成了简单的整合工做 (在用户提交到gerrit的时候,也会触发hook,审核经过再次触发,也就是说Jenkins会执行两次,有待解决)