Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1142621
  • 博文数量: 312
  • 博客积分: 12522
  • 博客等级: 上将
  • 技术积分: 3376
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-27 18:35
文章分类

全部博文(312)

文章存档

2016年(3)

2015年(1)

2013年(1)

2012年(28)

2011年(101)

2010年(72)

2009年(13)

2008年(93)

分类: Android平台

2016-08-01 17:07:49

前提

Ubuntu之邮件服务器(Postfix, Dovecot, MySql)
Gitlab预配置
Gerrit预配置
Jenkins预配置

前提

  • 创建邮箱 user1@vzjc.com
  • 创建邮箱 jenkins@vzjc.com

Gitlab配置

Gitlab已经配置了root账户的邮箱为admin@vzjc.com

为Gitlab的root用户添加ssh密钥

系统上创建admin用户

$ sudo adduser admin

可以得到系统的用户名admin和用户自己设置的密码

系统上登录admin账户,生成SSH密钥

$ su - admin
$ ssh-keygen -C admin@vzjc.com
$ cat .ssh/id_rsa.pub
$ exit
  • 1
  • 2
  • 3
  • 4

这里写图片描述

在Gitlab系统上更新SSH公钥

  1. 用root账号登录
  2. 点击页面右上角的Profile Settings - 点击左侧的SSH Keys小钥匙图标 - 点击Add SSH Key。在Key对应的输入框中输入上段落$cat .ssh/id_rsa.pub显示的公钥全文,点击Title,应该会自动填充为admin@vzjc.com
    这里写图片描述
    用了圈点Skitch,感觉萌萌哒

创建用户user1,作为一般用户

Gitlab上创建用户user1

  1. 打开, 如果当前页面为登录状态就退出登录
  2. 进入注册页面,输入注册信息,点击Sign up按钮来注册用户user1。

    这里写图片描述

  3. 登录邮箱点击验证连接,在Gitlab上成功创建user1账号

Ubuntu系统上创建user1账号,生成SSH秘钥

$ sudo adduser user1
$ su - user1
$ ssh-keygen -C user1@vzjc.com
$ cat .ssh/id_rsa.pub
$ exit
  • 1
  • 2
  • 3
  • 4
  • 5

这里写图片描述

这里写图片描述

用user1登录Gitlab,更新SSH公钥

这里写图片描述

Gitlab创建组和项目,并配置user1身份

用root登录Gitlab,创建新Group为dev-group

这里写图片描述

这里写图片描述

在dev-group中创建新项目test-project1

这里写图片描述

这里写图片描述

点击 test-project1 工程界面左上的 dev-group

这里写图片描述

点击 Members

这里写图片描述

user1 作为 Reporter 身份添加到组 dev-group (Reporter可以下载代码但是没上传权限)

这里写图片描述

用user1登录Gitlab,查看新项目test-project1的git地址

地址为git@gitlab.zjc.com:dev-group/test-project1.git

这里写图片描述

这里写图片描述

系统上登录user1,克隆工程,测试权限

$ su - user1
$ cd ~
$ git clone git@gitlab.zjc.com:dev-group/test-project1.git
$ exit
  • 1
  • 2
  • 3
  • 4

这里写图片描述

$ su - user1
$ cd ~/test-project1/
$ git config --global user.name 'user1' $ git config --global user.email 'user1@vzjc.com' $ touch testfile
$ git add .
$ git commit -m 'user1 add testfile' $ git push
$ exit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这里写图片描述

这里写图片描述

这里写图片描述

Gerrit配置

已经有了第一个账户admin并配置信箱为admin@vzjc.com

系统上登录admin,并获取上步创建的公钥

$ su - admin
$ cat ~/.ssh/id_rsa.pub
$ exit
  • 1
  • 2
  • 3

这里写图片描述

用admin账户登录gerrit,更新SSH公钥

这里写图片描述

这里写图片描述

为Gerrit系统创建用户user1并配置

  1. 使用htpasswd工具创建user1和密码

    $ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf user1

  2. 使用user1登录gerrit,更新信箱为user1@vzjc.com

  3. 更新SSH公钥 (user1@zjc.com那个)

Jenkins配置

Jenkins系统已经创建了管理员账户jenkins并安装了Gerrit Trigger插件

配置jenkins用户

  1. 为Gerrit系统创建用户jenkins
    $ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf jenkins

  2. 为系统用户jenkins创建ssh秘钥,并查看公钥
    也可以用前面的su - jenkins登录后再操作。这样就不用sudo -u jenkins -H前缀了

    $ sudo -u jenkins -H ssh-keygen -C jenkins@vzjc.com
    $ sudo -u jenkins -H cat /home/jenkins/.ssh/id_rsa.pub
    		
    • 1
    • 2

    这里写图片描述

  3. 用jenkins登录Gerrit更新邮箱并验证

    • 此处有个问题,如果在安装Jenkins系统前没有创建jenkins用户(安装时候系统会自动创建)并且使用的Dovecot来接收邮件并且用默认配置。那么要先查一下是否系统用户jenkins的uid是否大于500,如果不是,需要修改配置文件
    zjc@zjc:~$ id jenkins
    uid=122(jenkins) gid=131(jenkins) groups=131(jenkins)
    zjc@zjc:~$ sudo vim /etc/dovecot/conf.d/10-mail.conf
    		
    • 1
    • 2
    • 3

    然后修改first_valid_uid这个值

  4. 用jenkins登录Gerrit,更新SSH公钥为系统用户jenkins的公钥

Jenkins系统的SMTP设置 (根据具体情况配置)

主页面->Manage Jenkins->Configure System

  • Jenkins Location ->System Admin e-mail address : jenkins@vzjc.com

  • E-mail Notification

    • SMTP server : mail.vzjc.com
    • Default user e-mail suffix : @vzjc.com
    • 勾选Use SMTP Authentication
    • User Name : jenkins@vzjc.com
    • Password : 输入您设置的密码
    • 勾选Use SSL
    • SMTP Port : 587
    • Reply-To Address : jenkins@vzjc.com
    • Charset : UTF-8
      这里写图片描述
      这里写图片描述
      • 勾选Test configuration by sending test e-mail,然后在出现的框中填admin@vzjc.com,测试如果出错(好吧,如果用vzjc.com的邮件服务器是肯定会出错)Unrecognized SSL message, plaintext connection,则参考这个设置

设置Gerrit Trigger

主页面->Manage Jenkins->Gerrit Trigger->Add New Server

  • Add New Server : Check4Gerrit
  • 勾选 Gerrit Server With Default Configurations

这里写图片描述

  • Hostname : review.zjc.com
  • Frontend URL :
  • Username : jenkins
  • E-mail : jenkins@vzjc.com

这里写图片描述

如果点击Test Connection后出现如图所示的错误User jenkins has no capability to connect to Gerrit event stream.,这是因为因Jenkins 没有权限监听 Gerrit 的 ‘Stream Events’,可如此配置

点击Test Connection,出现的字符串为 success 则代表配置成功

点击下方的保存按钮来保存设置

Gerrit 和 Jenkins 整合

让 Gerrit 支持 Jenkins

如果安装Gerrit时没有或者没有选择添加Verified标签功能[‘lable Verified’],需要自己添加

$ su - admin
$ git init cfg; cd cfg
$ git config --global user.name 'admin' $ git config --global user.email 'admin@vzjc.com' $ git remote add origin ssh://admin@review.zjc.com:29418/All-Projects
$ git pull origin refs/meta/config
$ vim project.config
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在文件末添加

[label "Verified"] function = MaxWithBlock value = -1 Fails value = 0 No score value = +1 Verified
  • 1
  • 2
  • 3
  • 4
  • 5
$ git commit -a -m 'Updated permissions' $ git push origin HEAD:refs/meta/config
  • 1
  • 2

这里写图片描述
这里写图片描述

用admin登录Gerrit

现在提交的 Review 请求只有 Code Rivew 审核,我们要求的是需要 Jenkins 的 Verified 和 Code Review 双重保障,在 Projects 的 Access 栏里,针对 Reference: refs/heads/ 项添加 Verified 功能

  • Projects -> List -> All-Projects
  • Projects -> Access -> Edit -> 找到 Reference: refs/heads/* 项 -> Add Permission -> Label Verified -> Group Name 里输入 Non-Interactive Users -> 回车 或者 点击Add 按钮 -> 在最下面点击 Save Changes 保存更改

Gitlab上设置test-project1工程

前面我们在Gitlab上搭建了一个 test-project1 的工程,普通用户是没有办法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在于项目目录里。

用 admin用户添加.gitreview 文件

$ su - admin
$ git clone git@gitlab.zjc.com:dev-group/test-project1.git
$ cd test-project1
$ vim .gitreview
  • 1
  • 2
  • 3
  • 4

文件内容为:

[gerrit] host=review.zjc.com port=29418 project=test-project1.git
  • 1
  • 2
  • 3
  • 4

这里写图片描述

添加.gitreview到版本库

$ git add .gitreview
$ git config --global user.name 'admin' $ git config --global user.email 'admin@vzjc.com' $ git commit .gitreview -m 'add .gitreview file by admin.' $ git push origin master
$ exit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这里写图片描述

用 admin用户添加.testr.conf 文件

Python 代码我使用了 testr,需要先安装 testr 命令

$ sudo apt-get install python-pip
$ sudo pip install testrepository
  • 1
  • 2

在 test-project1 这个项目中添加 .testr.conf 文件

$ su - admin
$ cd test-project1
$ vim .testr.conf
  • 1
  • 2
  • 3

文件内容为:

[DEFAULT]
test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ ./ $LISTOPT $IDOPTION test_id_option=--load-list $IDFILE test_list_option=—list
  • 1
  • 2
  • 3
  • 4

提交到版本库中

$ git add .testr.conf
$ git commit .testr.conf -m 'add .testr.conf file by admin' $ git push origin master
$ exit
  • 1
  • 2
  • 3
  • 4

这里写图片描述

Gerrit上设置 test-project1工程

在 Gerrit 上创建 test-project1 项目

要知道 review 是在 gerrit 上,而 gerrit 上现在是没有项目的,想让 gitlab 上的项目能在 gerrit 上 review 的话,必须在 gerrit 上创建相同的项目,并有相同的仓库文件.

用 admin 用户在 Gerrit 上创建 test-project1 项目

$ su - admin
$ ssh -p 29418 admin@vzjc.com gerrit create-project test-project1
$ exit
  • 1
  • 2
  • 3

这里写图片描述

clone –bare Gitlab 上的仓库到 Gerrit

因为gerrit用户无访问gitlab的权限。所以要先看是否gerrit用户下已经存在了id_rsa密钥,如果没有则创建,然后把公钥加入到gitlab的admin账户上(这里加到 admin 账户上,是因为后面Gerrit系统还会有个复制 git 库到 Gitlab的功能,需要管理员权限)

赋予gerrit用户访问 test-project1 的权限
$ sudo -u gerrit -H ls /home/gerrit/.ssh
$ sudo -u gerrit -H ssh-keygen -C gerrit@vzjc.com
$ sudo -u gerrit -H cat /home/gerrit/.ssh/id_rsa.pub
  • 1
  • 2
  • 3
  • 这里写图片描述

  • root 登录Gitlab,把公钥添加到 admin 的SSH Key上 - 额,这是因为,Gerrit使用gerrit用户身份运行的,而Gerrit得git库复制功能需要权限复制到Gitlab上

以 gerrit身份克隆 test-project1
$ cd /home/gerrit/gerrit/git
$ sudo -u gerrit -H rm -fr test-project1.git
$ sudo -u gerrit -H  git clone --bare git@gitlab.zjc.com:dev-group/test-project1.git
  • 1
  • 2
  • 3

这里写图片描述

同步 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 功能,同时我们在安装 Gerrit 时候默认安装了这个 Plugin。现在只需要添加一个 replication.config 给 Gerrit

$ sudo -u gerrit -H vim /home/gerrit/gerrit/etc/replication.config
  • 1

文件内容为:

[remote "test-project1"] # Gerrit 上要同步项目的名字  projects = test-project1
  url = git@gitlab.zjc.com:dev-group/test-project1.git
  push = +refs/heads/*:refs/heads/* push = +refs/tags/*:refs/tags/* push = +refs/changes/*:refs/changes/* threads = 3
设置gerrit用户的 ~/.ssh/config
$ sudo -u gerrit -H vim /home/gerrit/.ssh/config
  • 1

文件内容为:

Host gitlab.zjc.com:
    IdentityFile ~/.ssh/id_rsa
    PreferredAuthentications publickey
  • 1
  • 2
  • 3
在gerrit用户的~/.ssh/known_hosts 中,给 gitlab.zjc.com 添加 rsa 密钥
$ sudo -u gerrit -H sh -c "ssh-keyscan -t rsa gitlab.zjc.com >> /home/gerrit/.ssh/known_hosts"$ sudo -u gerrit -H sh -c ruguo"ssh-keygen -H -f /home/gerrit/.ssh/known_hosts"

ssh-keyscan -p 10022 -t rsa 172.16.2.94 >> ~/.ssh/known_hosts 
重新启动 Gerrit 服务
$ sudo /etc/init.d/gerrit restart
Gerrit 的复制功能配置完毕

在 gerrit 文档中有一个 ${name} 变量用来复制 Gerrit 的所有项目,这里并不需要。如果有多个项目需要复制,则在 replication.config 中添加多个 [remote ….] 字段即可。务必按照上面步骤配置复制功能。

在 Jenkins 上对 test-project1 项目创建构建任务

Jenkins上安装git插件

安装 Git Plugin

  • jenkins 登录
  • Manage Jenkins -> Manage Plugins -> Avalabile -> 安装 Git Plugin

Jenkins上创建项目

添加 test-project1工程

  • jenkins 登录 Jenkins
  • New Item页面设置
    • Item name : test-project1
    • Freestyle project
      这里写图片描述
  • Git页面设置

    • Source Code Management : Git
    • Respostories :
    • Branches to build : origin/$GERRIT_BRANCH
    • Build Triggers : Gerrit event
    • 这里写图片描述
    • 按下文来配置,上面的无效 - 来自



    To get the Git Plugin to download your change; set Refspec to $GERRIT_REFSPEC and the Choosing strategy to Gerrit Trigger. You may also need to set 'Branches to build' to $GERRIT_BRANCH. 本系统中可用的是下面的配置 If this does not work for you set Refspec to refs/changes/*:refs/changes/* and 'Branches to build' to $GERRIT_REFSPEC 
    
    Note: Be aware that $GERRIT_BRANCH and $GERRIT_REFSPEC are not set in the Ref Updated case. If you want to trigger a build, you can set Refspec and 'Branches to build' to $GERRIT_REFNAME.


  • 这里 Choose a Server 选的是 Any Server 因为只有一个服务器 Check4Gerrit, 所以要确保这个服务器是 Enable 状态。可以去服务器那里去检查一下看看

  • 这里写图片描述

  • Gerrit Event页面设置
    • Trigger on : 点击 Add 分别添加 Patchset CreatedDraft Published
    • Dynamic Trigger Configuration
      • Pattern : test-project1
      • Branches : master

    • 这里写图片描述
  • Build页面设置
    • Add build step -> Execute shell
      cd $WORKSPACE
      [ ! -e .testrepository ] && testr init
      testr run

    • 这里写图片描述
  • 测试

    系统上 用 user1 提交一个更改

    1. user1 登录
    2. 删除目录 test-project1
    3. 克隆 test-project1 工程
    4. 进入 test-project1 目录
    5. 添加文件、提交
    6. git review 增加 review 到Gerrit
    $ su - user1
    $ rm -rf test-project1/
    $ git clone git@gitlab.zjc.com:dev-group/test-project1.git
    $ cd test-project1/
    $ touch testfile
    $ git add .
    $ git commit -m 'add by user1' $ git review
    $ exit

    我的环境还有一个问题 第一次 git review没有问题,但是如果用 git commit --amend修改了提交信息,再次 git reivew机会出现error: unpack failed: error Missing tree的错误,折中解决方案看这里

    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述

    admin 登录 Gerrit 系统,通过验证,提交

    这里写图片描述

    这里写图片描述

    这里写图片描述

    登录 Gitlab

    查看 test-project1 工程,可以看到新增加文件操作已经同步过来了

    这里写图片描述

    参考



    http://blog.csdn.net/stwstw0123/article/details/47615535

    阅读(2090) | 评论(0) | 转发(0) |
    0

    上一篇:android 创建本地repo server

    下一篇:没有了

    给主人留下些什么吧!~~