zoukankan      html  css  js  c++  java
  • 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 gerrit2@172.16.181.60 gerrit plugin install -n replication.jar - <replication.jar
    ##ssh -p 29418 gerrit2@172.16.181.80 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 gerrit2@172.16.181.80
    [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

    replication安装成功


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

    ### 以下斜体字可以忽略

    [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
    git@gitlab.test.mycompany.com'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用户是项目创建者,可以直接push,其他用户只能git review
    [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   #此处写错了,应该是host=172.16.181.80  gerrit地址 写作gitlab地址错误,后改正
    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

    replication配置

    [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 = git@gitlab.test.mycompany.com: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

    总结一下就是:现在gerrit上创建一个同名的project,然后删除,并在同样的位置从Gitlab克隆过来同名project覆盖。


    测试
    普通sisi用户

    [sisi@pre-srv44 devwork]$ git clone git@gitlab.test.mycompany.com: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'.

    没有Git review命令,

    安装git review
    [sisi@pre-srv44 test_project1]$ sudo yum install git-review

    测试连接gerrit
    [sisi@pre-srv44 test_project1]$ ssh -T -p 29418 sisi@172.16.181.80
    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://sisi@172.16.181.80: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://sisi@172.16.181.80: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(-)

    再执行Git review
    [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 sisi@172.16.181.80: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://sisi@172.16.181.80:29418/test_project1.git'

    失败,没有加添加change-id的钩子程序,这是gerrit必须的,按照提示命令执行
    [sisi@pre-srv44 test_project1]$ gitdir=$(git rev-parse --git-dir); scp -p -P 29418 sisi@172.16.181.80: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 sisi@172.16.181.80: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://sisi@172.16.181.80:29418/test_project1.git'
    还是失败是因为多个commit,但是只有之后一个commit有change-id,之前一个没有
    [sisi@pre-srv44 test_project1]$ git log
    commit 6e36acec3243f1c33959873a30bdbc61ebd600df (HEAD -> master)
    Author: sisi <sisi@mycompany.com>
    Date: Sun Jul 8 22:45:07 2018 +0800

    mod .gitreview

    Change-Id: Ie390e2f6c049997d96a1190efc6c66bdbbb078fd

    commit a7b5f6cadbd4f3585ceaac7d7a03883dcc60e238
    Author: sisi <sisi@mycompany.com>
    Date: Sun Jul 8 22:12:52 2018 +0800

    add testfile ##这个commit没有change-id

    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.  #原始clone后的位置


    硬回退到原始克隆时的位置
    [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
    这次提交成功,push到gerrit的refs/publish/master这个分支

    进入gerrit进行code_review, gerrit2和sisi各+1,code_review得分是2,通过
    但是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

    提交成功

    然后重启gerrit
    [gerrit2@pre-srv44 bin]$ ./gerrit.sh restart
    Stopping Gerrit Code Review: OK
    Starting Gerrit Code Review: OK
    再登录gerrit查看,已经没有verified label了

    点击summit,则此变更被merge到gerrit项目的主分支中了。

    查看merge到gerrit的变更是否同步到gitlab

    登录gitlab下,查看同名的项目,发现已经多了一个提交,这是通过gerrit的replication同步过来的。

    可以查看replication的日志,在gerrit安装目录下logs eplication.log中。

  • 相关阅读:
    oracel 备份导出报错 EXP-00091: Exporting questionable statistics
    将多张图片快速制作成一个PDF文件
    自连接表:M可能无下级,可能有下级
    STL迭代器失效总结
    DNS劫持和DNS污染的区别
    snprintf函数用法(转)
    sql查询面试题
    linux获取主机信息
    linux网络通信中的地址形式转换
    printf函数编程小技巧
  • 原文地址:https://www.cnblogs.com/newalan/p/9286542.html
Copyright © 2011-2022 走看看