整合Gitolite、Jenkins、Gerrit 协同工作

  • Gitolite 代码托管
  • Gerrit 代码审核
  • Jenkins 编译测试
    各个环境的搭建可以参考我的其他几篇文章

在基本环境的基础上,需要用到:

  • git hook
  • Jenkins 的 Generic Webhook Trigger插件
  • Gerrit 的 replication 插件

本例以PostgreSQL项目为例

1.Gerrit

1.1安装 replication 插件
如果安装Gerrit时安装了replication插件,可以忽略这一项。
插件安装:

[gerrit@localhost ~]$ java -jar gerrit-2.12.4.war init -d gerrit_site --batch --install-plugin replication

1.2 创建postgresql项目
该项目事先存在于Gitolite中

[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 [email protected]: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给gerrit

[gerrit@localhost ~]$ cd gerrit_site/etc/
[gerrit@localhost etc]$ vi replication.config
[remote "postgresql"]
projects = postgresql
url = [email protected]:postgresql.git
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/*
threads = 3

1.4 设置gerrit用户的 ~/.ssh/config

[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 服务

[gerrit@localhost ~]$ /home/gerrit/gerrit_site/bin/gerrit.sh restart

2.Jenkins

2.1 安装Generic Webhook Trigger Plugin 插件
系统管理 =》管理插件 =》可选插件
使用过滤功能找到"Generic Webhook Trigger Plugin"插件并安装(由于我已经安装好了,所在不显示出来,可以在已安装找到)
整合Gitolite、Jenkins、Gerrit 协同工作_第1张图片
整合Gitolite、Jenkins、Gerrit 协同工作_第2张图片
2.2 新建PostgreSQL项目
整合Gitolite、Jenkins、Gerrit 协同工作_第3张图片
构建触发器配置项目触发条件,勾选Generic Webhook Trigger,在此使用Request parameters,填写自定义的参数和值
整合Gitolite、Jenkins、Gerrit 协同工作_第4张图片
构建 =》Execute shell
简单编写一个clone代码然后编译的动作

git clone [email protected]:postgresql
cd postgresql
./configure --prefix=$WORKSPACE/pgsql
make 
make install

整合Gitolite、Jenkins、Gerrit 协同工作_第5张图片
2.3 获取API Token
在“系统管理–管理用户–用户列表” 点击用户后面的设置图标,获取 API Token
整合Gitolite、Jenkins、Gerrit 协同工作_第6张图片

3.Gitolite

添加 hook
进入postgresql项目的hooks目录,添加post-receive,使用curl命令发送post请求

[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:[email protected]: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

  1. 测试

用户clone代码,修改远程仓库到gerrit,修改提交

[highgo@localhost Desktop]$ git clone [email protected]: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://[email protected]:29418/postgresql.git
[highgo@localhost postgresql]$ git config user.name highgo
[highgo@localhost postgresql]$ git config user.email [email protected]
[highgo@localhost postgresql]$ git config remote.origin.push refs/heads/*:refs/for/*
[highgo@localhost postgresql]$ scp -p -P 29418 [email protected]: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审核提交并通过
整合Gitolite、Jenkins、Gerrit 协同工作_第7张图片
git通知Jenkins执行项目的编译安装
到此完成了简单的整合工作
(在用户提交到gerrit的时候,也会触发hook,审核通过再次触发,也就是说Jenkins会执行两次,有待解决)

转载于:https://my.oschina.net/u/3618133/blog/1615324

你可能感兴趣的:(整合Gitolite、Jenkins、Gerrit 协同工作)