gerrit配置和使用

参考http://www.cnblogs.com/tesky0125/p/5973642.html

1.安装gerrit replication插件
mkdir ~/tmp
cp gerrit-2.14.war tmp
cd tmp
unzip gerrit-2.14.war
cd WEB-INF/plugins
[gerrit2@pre-srv44 plugins]$ pwd
/home/gerrit2/tmp/WEB-INF/plugins
ssh -p 29418 [email protected] gerrit plugin install -n replication.jar - <replication.jar
ssh -p 29418 [email protected] gerrit plugin ls

[gerrit2@pre-srv44 .ssh]$ alias
alias ssh-gerrit='ssh -p 29418 -i ~/.ssh/gerritadmin 172.16.181.80 -l gerrit2'
等价于ssh -p 29418 -i ~/.ssh/gerritadmin [email protected]
[gerrit2@pre-srv44 .ssh]$ ssh-gerrit gerrit plugin ls
Name Version Status File
-------------------------------------------------------------------------------
[gerrit2@pre-srv44 .ssh]$ ssh-gerrit gerrit plugin install -n replication.jar - </home/gerrit2/tmp/WEB-INF/plugins/replication.jar
fatal: remote installation is disabled
[gerrit2@pre-srv44 .ssh]$ git config --file /home/gerrit2/gerrit_site_http/etc/gerrit.config plugins.allowRemoteAdmin true
[gerrit2@pre-srv44 etc]$ ssh-gerrit gerrit --help
[gerrit2@pre-srv44 etc]$ ssh-gerrit gerrit flush-caches
[gerrit2@pre-srv44 bin]$ ./gerrit.sh start
Starting Gerrit Code Review: OK
[gerrit2@pre-srv44 .ssh]$ ssh-gerrit gerrit plugin install -n replication.jar - </home/gerrit2/tmp/WEB-INF/plugins/replication.jar
[gerrit2@pre-srv44 bin]$ ssh-gerrit gerrit plugin ls
Name Version Status File
-------------------------------------------------------------------------------
replication v2.14 ENABLED replication.jar


gerrit增加ACCESS权限
Global Capabilities
Create Account: Administrators
Create Group: Administrators

80下用http克隆可以成功
[gerrit2@pre-srv44 devwork]$ git clone http://gitlab.test.mycompany.com/dev_group/test_project1.git
Cloning into 'test_project1'...
Username for 'http://gitlab.test.mycompany.com': [email protected]
Password for 'http://[email protected]@gitlab.test.mycompany.com':
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 11 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (11/11), done.
[gerrit2@pre-srv44 devwork]$ ls
test_project1

[gerrit2@pre-srv44 .ssh]$ ll
total 24
-rw-r-x---+ 1 gerrit2 root 1675 Jun 12 09:59 admin
-rw-r-xr--+ 1 gerrit2 root 387 Jun 12 09:59 admin.pub
-rw-r--r-- 1 gerrit2 root 112 Jul 8 17:37 config
-rw------- 1 gerrit2 root 1675 Jun 24 10:56 gerritadmin
-rw-r--r-- 1 gerrit2 root 393 Jun 24 10:56 gerritadmin.pub
-rw-r--r-- 1 gerrit2 root 1202 Jul 8 17:09 known_hosts
“+” 什么意思
[gerrit2@pre-srv44 .ssh]$ vi config
Host hostgitlab
   Hostname gitlab.test.mycompany.com
   User git
   IdentityFile ~/.ssh/admin.pub
[gerrit2@pre-srv44 devwork]$ git clone hostgitlab:dev_group/test_project1.git
Cloning into 'test_project1'...
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0654 for '/home/gerrit2/.ssh/admin.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /home/gerrit2/.ssh/admin.pub
[email protected]'s password:
Permission denied, please try again.

IdentityFile ~/.ssh/admin.pub 用公钥不对,应该指定私钥

[gerrit2@pre-srv44 .ssh]$ vi config
Host hostgitlab_admin
   Hostname gitlab.test.mycompany.com
   User git
   IdentityFile ~/.ssh/admin

Host hostgitlab_gerritadmin
   Hostname gitlab.test.mycompany.com
   User git
   IdentityFile ~/.ssh/gerritadmin

[gerrit2@pre-srv44 devwork]$ ssh -T hostgitlab_gerritadmin
Welcome to GitLab, gerrit2!
[gerrit2@pre-srv44 devwork]$ ssh -T hostgitlab_admin
Welcome to GitLab, gerrit2!
都成功了

ssh -vT hostgitlab_admin 调试

Gitlab上设置test-project1工程
前面我们在Gitlab上搭建了一个 test_project1 的工程,普通用户是没有办法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在于项目目录里。
用 gerrit2用户添加.gitreview 文件
[gerrit2@pre-srv44 ~]$ cd devwork/
[gerrit2@pre-srv44 devwork]$ ls
[gerrit2@pre-srv44 devwork]$ git clone hostgitlab_admin:dev_group/test_project1.git
Cloning into 'test_project1'...
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 11 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (11/11), done.
[gerrit2@pre-srv44 devwork]$ ls
test_project1
[gerrit2@pre-srv44 devwork]$ cd test_project1/
[gerrit2@pre-srv44 test_project1]$ vim .gitreview
[gerrit]
host=gitlab.test.mycompany.com
port=29418
project=test_project1.git

[gerrit2@pre-srv44 test_project1]$ git add .gitreview
[gerrit2@pre-srv44 test_project1]$ git config --global user.name 'gerrit2'
[gerrit2@pre-srv44 test_project1]$ git config --global user.email 'gerrit2@email'
[gerrit2@pre-srv44 test_project1]$ git commit .gitreview -m 'add .gitreview file by gerrit.'
[master 698ca36] add .gitreview file by gerrit.
1 file changed, 4 insertions(+)
create mode 100644 .gitreview
[gerrit2@pre-srv44 test_project1]$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 373 bytes | 373.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To hostgitlab_admin:dev_group/test_project1.git
b0e358d..698ca36 master -> master
[gerrit2@pre-srv44 test_project1]$

Gerrit上设置 test_project1工程
在Gerrit上创建 test_project1 项目
要知道review是在gerrit上,而gerrit上现在是没有项目的,想让gitlab上的项目能在gerrit上review的话,必须在gerrit上创建相同的项目,并有相同的仓库文件.
用gerrit2用户在 Gerrit 上创建 test-project1 项目
[gerrit2@pre-srv44 devwork]$ ssh-gerrit gerrit create-project test_project1
登陆gerrit界面,发现test_project1工程已经创建了。(这种方式创建的项目是空的)

clone --bare Gitlab上的仓库到 Gerrit (gerrit上的项目最好是从gitlab上git clone --bare过来,并且项目不要为空)
[gerrit2@pre-srv44 ~]$ cd gerrit_site_http
[gerrit2@pre-srv44 gerrit_site_http]$ ls
bin cache data db etc git index lib logs plugins static tmp
[gerrit2@pre-srv44 gerrit_site_http]$ cd git
[gerrit2@pre-srv44 git]$ ls
All-Projects.git All-Users.git test_project1.git
[gerrit2@pre-srv44 git]$ cd test_project1.git/
[gerrit2@pre-srv44 test_project1.git]$ ls
branches config HEAD hooks logs objects refs
[gerrit2@pre-srv44 test_project1.git]$ cd ..
[gerrit2@pre-srv44 git]$ ls
All-Projects.git All-Users.git test_project1.git
[gerrit2@pre-srv44 git]$ mv test_project1.git test_project1.git.bak
[gerrit2@pre-srv44 git]$ ls
All-Projects.git All-Users.git test_project1.git.bak
[gerrit2@pre-srv44 git]$ git clone --bare hostgitlab_admin:dev_group/test_project1.git
Cloning into bare repository 'test_project1.git'...
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 14 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (14/14), done.
[gerrit2@pre-srv44 git]$ ls
All-Projects.git All-Users.git test_project1.git test_project1.git.bak
[gerrit2@pre-srv44 git]$ cd test_project1.git
[gerrit2@pre-srv44 test_project1.git]$ ls
branches config description HEAD hooks info objects packed-refs refs
[gerrit2@pre-srv44 test_project1.git]$


同步 Gerrit的test_project1 项目到 Gitlab 上的 test_project1 项目目录中
当用户git review后,代码通过 jenkins 测试、人工 review 后,代码只是 merge 到了 Gerrit 的 test_project1 项目中,并没有 merge 到 Gitlab 的 test-project1 项目中,所以需要当 Gerrit test_project1 项目仓库有变化时自动同步到 Gitlab 的 test_project1 项目仓库中。
Gerrit 自带一个 Replication 功能可完成同步
现在只需要添加一个 replication.config 给 Gerrit

[gerrit2@pre-srv44 etc]$ pwd
/home/gerrit2/gerrit_site_http/etc
[gerrit2@pre-srv44 etc]$ vim replication.config
[remote "test_project1"]
projects = test_project1
url = [email protected]:dev_group/test_project1.git
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/*
threads = 3

重启
[gerrit2@pre-srv44 ~]$ ~/gerrit_site_http/bin/gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: OK


测试
sisi用户
git clone [email protected]:dev_group/test_project1.git

[sisi@pre-srv44 devwork]$ git clone [email protected]:dev_group/test_project1.git
Cloning into 'test_project1'...
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 14 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (14/14), done.
[sisi@pre-srv44 devwork]$ ls
test_project1
[sisi@pre-srv44 devwork]$ cd test_project1/
[sisi@pre-srv44 test_project1]$ ls
hello maintain
[sisi@pre-srv44 test_project1]$ touch testfile
[sisi@pre-srv44 test_project1]$ ls
hello maintain testfile
[sisi@pre-srv44 test_project1]$ git add testfile
[sisi@pre-srv44 test_project1]$ git commit -m 'add testfile'
[master a7b5f6c] add testfile
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 testfile
[sisi@pre-srv44 test_project1]$ git review
git: 'review' is not a git command. See 'git --help'.
[sisi@pre-srv44 test_project1]$ sudo yum install git-review
安装git review


[sisi@pre-srv44 test_project1]$ ssh -T [email protected]:29418
ssh: Could not resolve hostname gitlab.test.mycompany.com:29418: Name or service not known
[sisi@pre-srv44 test_project1]$ ssh -T [email protected]:29418
ssh: Could not resolve hostname 172.16.181.80:29418: Name or service not known
[sisi@pre-srv44 test_project1]$ ssh -T -p 29418 [email protected]
The authenticity of host '[172.16.181.80]:29418 ([172.16.181.80]:29418)' can't be established.
RSA key fingerprint is 34:b8:66:84:01:26:96:0a:77:ab:60:0f:07:84:6a:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[172.16.181.80]:29418' (RSA) to the list of known hosts.

**** Welcome to Gerrit Code Review ****

Hi sisi, you have successfully connected over SSH.

Unfortunately, interactive shells are disabled.
To clone a hosted Git repository, use:

git clone ssh://[email protected]:29418/REPOSITORY_NAME.git


[sisi@pre-srv44 test_project1]$ vi .gitreview
[gerrit]
host=172.16.181.80 #gerrit地址 前述写作gitlab地址错误
port=29418
project=test_project1.git

[sisi@pre-srv44 test_project1]$ git review
Creating a git remote called "gerrit" that maps to:
ssh://[email protected]:29418/test_project1.git
Errors running git rebase -p -i remotes/gerrit/master
Cannot rebase: You have unstaged changes.
Please commit or stash them.
[sisi@pre-srv44 test_project1]$
[sisi@pre-srv44 test_project1]$ git add .gitreview
[sisi@pre-srv44 test_project1]$ git commit -m 'mod .gitreview'
[master 45b7d34] mod .gitreview
1 file changed, 1 insertion(+), 1 deletion(-)
[sisi@pre-srv44 test_project1]$ git review
You are about to submit multiple commits. This is expected if you are
submitting a commit that is dependent on one or more in-review
commits. Otherwise you should consider squashing your changes into one
commit before submitting.

The outstanding commits are:

45b7d34 (HEAD -> master) mod .gitreview
a7b5f6c add testfile

Do you really want to submit the above commits?
Type 'yes' to confirm, other to cancel: yes
remote: Processing changes: refs: 1, done
remote: ERROR: [a7b5f6c] missing Change-Id in commit message footer
remote:
remote: Hint: To automatically insert Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 [email protected]:hooks/commit-msg ${gitdir}/hooks/
remote: And then amend the commit:
remote: git commit --amend
remote:
To ssh://172.16.181.80:29418/test_project1.git
! [remote rejected] HEAD -> refs/publish/master ([a7b5f6c] missing Change-Id in commit message footer)
error: failed to push some refs to 'ssh://[email protected]:29418/test_project1.git'
[sisi@pre-srv44 test_project1]$ gitdir=$(git rev-parse --git-dir); scp -p -P 29418 [email protected]:hooks/commit-msg ${gitdir}/hooks/
commit-msg 100% 4691 4.6KB/s 00:00
[sisi@pre-srv44 test_project1]$ git commit --amend

[sisi@pre-srv44 test_project1]$ git review
You are about to submit multiple commits. This is expected if you are
submitting a commit that is dependent on one or more in-review
commits. Otherwise you should consider squashing your changes into one
commit before submitting.

The outstanding commits are:

6e36ace (HEAD -> master) mod .gitreview
a7b5f6c add testfile

Do you really want to submit the above commits?
Type 'yes' to confirm, other to cancel: yes
remote: Processing changes: refs: 1, done
remote: ERROR: [a7b5f6c] missing Change-Id in commit message footer
remote:
remote: Hint: To automatically insert Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 [email protected]:hooks/commit-msg ${gitdir}/hooks/
remote: And then amend the commit:
remote: git commit --amend
remote:
To ssh://172.16.181.80:29418/test_project1.git
! [remote rejected] HEAD -> refs/publish/master ([a7b5f6c] missing Change-Id in commit message footer)
error: failed to push some refs to 'ssh://[email protected]:29418/test_project1.git'
还是失败是因为多个commit,但是只有commit有id,另外一个没有
[sisi@pre-srv44 test_project1]$ git log
commit 6e36acec3243f1c33959873a30bdbc61ebd600df (HEAD -> master)
Author: sisi <[email protected]>
Date: Sun Jul 8 22:45:07 2018 +0800

mod .gitreview

Change-Id: Ie390e2f6c049997d96a1190efc6c66bdbbb078fd

commit a7b5f6cadbd4f3585ceaac7d7a03883dcc60e238
Author: sisi <[email protected]>
Date: Sun Jul 8 22:12:52 2018 +0800

add testfile

commit 698ca363e60daca3f88760916112855687d9c3f2 (origin/master, origin/HEAD, gerrit/master)
Author: gerrit2 <gerrit2@email>
Date: Sun Jul 8 21:42:24 2018 +0800

add .gitreview file by gerrit.
硬回退
[sisi@pre-srv44 test_project1]$ git reset --hard 698c
HEAD is now at 698ca36 add .gitreview file by gerrit.
重新改,重新提交
[sisi@pre-srv44 test_project1]$ vi .gitreview
[gerrit]
host=172.16.181.80
port=29418
project=test_project1.git
[sisi@pre-srv44 test_project1]$ git add .gitreview
[sisi@pre-srv44 test_project1]$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: .gitreview

[sisi@pre-srv44 test_project1]$ git commit -m 'mod .gitreview'
[master e9a04cf] mod .gitreview
1 file changed, 1 insertion(+), 1 deletion(-)
[sisi@pre-srv44 test_project1]$ git review
remote: Processing changes: new: 1, refs: 1, done
remote:
remote: New Changes:
remote: http://172.16.181.80:8080/1 mod .gitreview
remote:
To ssh://172.16.181.80:29418/test_project1.git
* [new branch] HEAD -> refs/publish/master
成功

进入gerrit进行code_review, gerrit2和sisi各+1
但是verfied 无法+1,这个一般是由自动程序实现的,如jenkins

现在显示 Change 1 - Needs Verified Label

如何去掉Verified Label

方案1 改数据库(失败)
ssh-gerrit gerrit gsql

DELETE FROM approval_categories WHERE category_id = 'VRIF';
DELETE FROM approval_category_values WHERE category_id = 'VRIF';
找不到这两个表
https://stackoverflow.com/questions/7271208/gerrit-remove-need-verified-1-verified

方案2 参照安装verfied label 反向操作(成功)
https://www.cnblogs.com/kevingrace/p/5651447.html

[gerrit2@pre-srv44 .ssh]$ ls
admin admin.pub config gerritadmin gerritadmin.pub id_rsa known_hosts
[gerrit2@pre-srv44 .ssh]$ cp gerritadmin id_rsa #因为gerrit2用的是这私钥对应的公钥
[gerrit2@pre-srv44 .ssh]$ cd ~/devwork/cfg/
[gerrit2@pre-srv44 cfg]$ ls
[gerrit2@pre-srv44 cfg]$ git pull origin refs/meta/config
remote: Counting objects: 16, done
remote: Finding sources: 100% (16/16)
remote: Total 16 (delta 0), reused 1 (delta 0)
Unpacking objects: 100% (16/16), done.
From ssh://172.16.181.80:29418/All-Projects
* branch refs/meta/config -> FETCH_HEAD
[gerrit2@pre-srv44 cfg]$ vi project.config
删除[label "Verified"]及以下共7行
[gerrit2@pre-srv44 cfg]$ git add project.config
[gerrit2@pre-srv44 cfg]$ git commit -m 'del verified'
[master 2b01dc5] del verified
1 file changed, 7 deletions(-)
[gerrit2@pre-srv44 cfg]$ git push origin HEAD:refs/meta/config
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 301 bytes | 301.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1)
remote: Processing changes: refs: 1, done
To ssh://172.16.181.80:29418/All-Projects
36265fa..2b01dc5 HEAD -> refs/meta/config
[gerrit2@pre-srv44 bin]$ ./gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: OK
重启gerrit,再登录gerrit查看,已经没有了

猜你喜欢

转载自www.cnblogs.com/newalan/p/9286542.html