gitlab+gerrit+jenkins持续集成框架

1.持续集成之gitlab+gerrit+jenkins

1.1. GitLab

1.1.1. 简介

  GitLab 是一个使用使用Ruby on Rails搭建的,用于仓库管理系统的开源项目。使用Git做为代码管理工具,并在此基础上搭建起来的web服务java

1.1.2. 环境搭建

  a) Install and configure the necessary dependenciespython

    If you install Postfix to send email please select 'Internet Site' during setup. Instead of using Postfix you can also use Sendmail or configure a custom SMTP server. If you wish to use Exim, please configure it as an SMTP server.linux

    On Centos 6 and 7, the commands below will also open HTTP and SSH access in the system firewall.nginx

    sudo apt-get install curl openssh-server ca-certificates postfixgit

  b) Add the GitLab package server and install the package.web

    • Apt-get安装

      Curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh|sudo bashshell

      sudo apt-get install gitlab-eevim

      If you are not comfortable installing the repository through a piped script, you can find the entire script here.bash

      Alternatively you can select and download the package manually and install using服务器

    • deb包安装

      下载deb包

      https://packages.gitlab.com/gitlab/gitlab-ee

      安装deb包

      sudo dpkg -i gitlab-ee_8.1.2-ee.0_amd64.deb

  c) 1.1.2.3. Configure and start GitLab

    sudo gitlab-ctl reconfigure

  d) 1.1.2.4. Browse to the hostname and login

    http://10.24.6.8:80

    Username: root 
    Password: 5iveL!fe

  e)  2.1.6. Upload your license(EE版本须要认证)

    Go to Admin > License and upload your .gitlab-license file. 
    Don't have a license yet? Get a subscription.

 

1.1.3. 建立TEST项目

  建立test 项目

   

 

1.1.4.  Gerrit Review支持

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

 

1.1.4.1安装支持库

    须要安装git-review, readline

    Apt-get install git-review 

    Linux :pip install readline

    Windows:pip install pyreadline

    

1.1.4.2 用 admin用户给test项目添加.gitreview 文件

  a) 拷贝gitlab的test项目

    gerrit@gerrit:~/cfg$ git init cfg

    gerrit@gerrit:~/cfg$ git init cfg

    gerrit@gerrit:~/cfg/cfg$ git config --global user.name 'root'

    gerrit@gerrit:~/cfg/cfg$ git config --global user.email xxxxxxxx@qq.com

    gerrit@gerrit:~/cfg/cfg$ git clone http://10.24.6.8/root/test.git

    gerrit@gerrit:~/cfg/cfg$ cd test/

     

  b)  添加gitreview文件

    gerrit@gerrit:~/cfg/cfg/test$ vim .gitreview

    文件内容为:

     

  c)  上传gitreview文件

    gerrit@gerrit:~/cfg/cfg/test$ git add .gitreview

    gerrit@gerrit:~/cfg/cfg/test$ git commit .gitreview -m 'add .gitreview file by admin.'

    gerrit@gerrit:~/cfg/cfg/test$ git push origin master

    

1.1.5. 用户建立

  以admin帐户登陆建立帐户 

  

  

1.1.6 SSH设置

  a)  Su 进入linux对应帐户

  b)  建立sshkey

    $ sudo -u gerrit -H ls /home/gerrit/.ssh

    $ sudo -u gerrit -H ssh-keygen -C xxxxxx@qq.com

    邮箱为gitlab的帐户注册邮箱

 

  c)  获取ssh pub key

    $ sudo -u gerrit -H cat /home/gerrit/.ssh/id_rsa.pub

    

  d)  登陆新建立用户设置SSH Keys

    

    

1.1.7. Git自带code review功能

  • 强制 Review:在 Gitlab 上建立的项目,指定相关用户只有 Reporter 权限,这样用户没有权限使用 git push 功能,只能 git review Gerrit 系统上,Jenkins 在监听 Gerrit 上的项目事件会触发构建任务来测试代码,Jenkins 把测试结果经过 ssh gerrit 个这个项目打上 Verified 成功或失败标记,成功通知其它人员 Review
  • Ø  Gitlab 保护 Master 分支:在 Gitlab 上建立的项目能够把 Master 分支保护起来,普通用户能够本身建立分支并提交代码到本身的分支上,没有权限直接提交到 Master 分支,用户最后提交申请把本身的分支 Merge Master,管理员收到 Merge 请求后,Review 后选择是否合并。 

  a)  针对某次提交进行评论
    

  b) 负责人设置master/release分支受保护,只有负责人能够提交,其余人不能push

    Branch protected总开关

    

    项目分支开关

    

    

  c)  没被容许提交的用户没法提交

    

  d) GitLab提交合并请求给负责人

    同时发送邮件给该负责人,让他知道有人要合并分支 

    

    

  e)  负责人接收到合并请求

    

  f)  接受合并

    

1.1.8 备份

  • Gitlab 建立备份

    使用Gitlab一键安装包安装Gitlab很是简单, 一样的备份恢复与迁移也很是简单. 使用一条命令便可建立完整的Gitlab备份:

    gitlab-rake gitlab:backup:create

    使用以上命令会在/var/opt/gitlab/backups目录下建立一个名称相似为1393513186_gitlab_backup.tar的压缩包, 这个压缩包就是Gitlab整个的完整部分, 其中开头的1393513186是备份建立的日期.

  • Gitlab 修改备份文件默认目录

    你也能够经过修改/etc/gitlab/gitlab.rb来修改默认存放备份文件的目录:

    gitlab_rails['backup_path'] = '/mnt/backups'

    /mnt/backups修改成你想存放备份的目录便可, 修改完成以后使用gitlab-ctl reconfigure命令重载配置文件便可.

  • Gitlab 自动备份

    也能够经过crontab使用备份命令实现自动备份:

    sudo su -

    crontab -e

    加入如下, 实现天天凌晨2点进行一次自动备份:

    0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

  • Gitlab 恢复

    一样, Gitlab的从备份恢复也很是简单:

    #中止相关数据链接服务
stop unicorn    gitlab-ctl
    gitlab-ctl stop sidekiq
    # 从1393513186编号备份中恢复
    gitlab-rake gitlab:backup:restore BACKUP=1393513186
    # 启动Gitlab
    sudo gitlab-ctl start
  • Gitlab迁移

    迁移如同备份与恢复的步骤同样, 只须要将老服务器/var/opt/gitlab/backups目录下的备份文件拷贝到新服务器上的/var/opt/gitlab/backups便可(若是你没修改过默认备份目录的话). 可是须要注意的是新服务器上的Gitlab的版本必须与建立备份时的Gitlab版    本号相同. 好比新服务器安装的是最新的7.60版本的Gitlab, 那么迁移以前, 最好将老服务器的Gitlab 升级为7.60在进行备份.

  • 其余

    最新版本的Gitlab已经修复了HTTPS设备的BUG, 如今使用官方HTTPS配置便可轻松启用HTTPS.

1.1.9 遇到问题

  a)  Your configuration specifies to merge with the ref 'master'

    缘由:这多是 TortoiseGit 当前版本的一个 BUG

    解决:

    

1.2. Gerrit

1.2.1 简介

  gerrit的工做模式是全部真正的merge由gerrit负责,咱们的提交只是提交给了gerrit,gerrit会通知项目的负责人来审核代码,同时也会调用一些自动化测试构建工具来检查代码是否符合要求,只有都经过后才会的合并进主干代码。因此能够把gerrit当作是代码和提交者之间的一道门。

1.2.2. 环境搭建

  a)  Jdk安装

  安装最新版本Oracle JRE/JDK,使用PPA(personal package archieve)WEBUPD8的方式安装。

  sudo apt-get install python-software-properties

  sudo add-apt-repository ppa:webupd8team/java

  sudo apt-get update

 

  若是你正在使用OPenJDK,那么先将它移除以避免产生任何的冲突

  sudo apt-get remove --purge openjdk*

 

  而后使用下面的命令安装Oracle Java 7

  sudo apt-get install oracle-java7-installer

  b)  添加gerrit用户

    adduser gerrit

  c) 创建存放代码的目录

    mkdir /home/gerrit/gerrit_dir/

  d) 安装git

    sudo apt-get install git

  e) 安装

    sudo java -jar gerrit-2.11.4.war init -d /home/gerrit/gerrit_dir/

    一路按回车,先以默认方式安装,以后再手动修改配置文件。

  f) 配置gerrit

        vim review_site/etc/gerrit.config

     

  g) NGINX 安装

    sudo apt-get install nginx 

  h) NGINX 配置

    查看/etc/nginx/nginx.conf配置文件读取那些sites配置文件

    

    发现这里只include  /etc/nginx/sites-enabled里面的文件 

    在/etc/nginx/sites-enabled新增一个端口映射虚拟服务器文件gitlab

    配置nginx端口映射,8082映射到gerrit的8081端口

    

 

  i) 服务重启

1.2.3 帐户建立

  如下模拟admin帐户建立

  a) NGINX 帐户访问:建立 htpasswd.conf 文件,并添加 admin 用户、密码到文件中

    # touch /etc/gerrit_account/htpasswd.conf

    # htpasswd  /etc/gerrit_account/htpasswd.conf   admin

    默认第一个登陆 Gerrit 的用户是 Admin。

     

  b) 邮箱验证

    注册邮箱,并添加对应密钥

    

 

  c) 帐户登录

    默认第一个登陆 Gerrit 的用户是 Admin。

    

    

 

1.2.4  Gitlab 支持

1.2.4.1  克隆gitlab test项目

  a) 建立test项目

    

     

  b) 使用gitlab的 gerrit身份从gitlab克隆 test项目

    注意:gerrit的ssh key须要同时能登录gitlab和gerrit)

 

    $ cd /home/gerrit/gerrit_dir

    $ rm -rf test.git

    $ git clone --bare git@10.24.6.8:root/test.git

    
    

1.2.4.2  自动同步项目到gitlab

  同步 Gerrit 的test项目到 Gitlab 上的test项目

  当用户 git review 后,代码经过 jenkins 测试、人工 review 后,代码只是 merge 到了 Gerrit 的 test-project1 项目中,并无 merge 到 Gitlab 的 test 项目中,因此须要当 Gerrit test项目仓库有变化时自动同步到 Gitlab 的 test项目仓库中。Gerrit 自带一个 Replication 功能,同时咱们在安装 Gerrit 时候默认安装了这个 Plugin。如今只须要添加一个 replication.config 给 Gerrit

 

  a) 进入gerrit代码存放目录gerrit_dir,增长replication.config

    gerrit@gerrit:~/gerrit_dir$ vim etc/replication.config

     文件内容为:

    [remote "test"]

           projects = test

           url = git@10.24.6.8:root/test.git

      push = +refs/heads/*:refs/heads/*

             push = +refs/tags/*:refs/tags/*

      push = +refs/changes/*:refs/changes/*

      threads = 3

     

  b) 设置gerrit用户的 ~/.ssh/config

    文件内容为:

 

    Host 10.24.6.8:

        IdentityFile ~/.ssh/id_rsa

    PreferredAuthentications publickey
    

  c) gerrit用户的~/.ssh/known_hosts 中,给 10.24.6.8(gitlab) 添加 rsa 密钥

    gerrit@gerrit:~/gerrit_dir$ sh -c "ssh-keyscan -t rsa 10.24.6.8 >> /home/gerrit/.ssh/known_hosts"  

    

    gerrit@gerrit:~/gerrit_dir$ sh -c "ssh-keygen -H -f /home/gerrit/.ssh/known_hosts"

    

  d) 从新启动 Gerrit 服务

    gerrit@gerrit:~/gerrit_dir$ ./bin/gerrit.sh restart

    

  e) Gerrit 的自动功能配置完毕

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

1.2.5.  Jenkins 支持

1.2.5.1.  Jenkins 帐户

  a) 建立用户jenkins

    $ sudo -u gerrit -H htpasswd /home/gerrit_account/htpasswd.conf Jenkins

     

  b) 用jenkins用户登陆Gerrit更新邮箱xxxxxxx@qq.com并验证

  c) 建立jenkins密钥

    $ sudo ssh-keygen -C xxxxxx@qq.com

     

  d) 用jenkins用户登陆Gerrit服务器,更新SSH公钥

    获取公钥

      $ sudo -u jenkins -H cat /home/jenkins/.ssh/id_rsa.pub

    

    

  e) Gerrit 的 ‘Stream Events’配置

    管理员登陆gerrit

    Projects->List->All-Projects

    Projects->Access 

    Global Capabilities->Stream Events 点击 Non-Interactive Users

    添加 Jenkins@zjc.com 用户到 ‘Non-Interactive Users’ 组

    

1.2.5.2  Verified功能支持

     Verified功能,就是利用jenkins肯定gerrit提交的代码是否有问题,须要jenkins给个确认

    a) 若是安装Gerrit时没有或者没有选择添加Verified标签功能[‘lable Verified’],须要本身添加

      gerrit@gerrit:~$ cd cfg/

      gerrit@gerrit:~/cfg$git config --global user.name "admin"

      gerrit@gerrit:~/cfg$git config --global user.email "423884230@qq.com"

      gerrit@gerrit:~/cfg$git remote add origin ssh://admin@10.24.6.12:29418/All-Projects

      gerrit@gerrit:~/cfg$ git pull origin refs/meta/config

      gerrit@gerrit:~/cfg$ vim project.config

 

    在文件末添加

    [label "Verified"]

        function = MaxWithBlock

        value = -1 Fails

        value =  0 No score

      value = +1 Verified

 

    gerrit@gerrit:~/cfg$ git commit -a -m "Update permissions"

    gerrit@gerrit:~/cfg$ git push origin HEAD:refs/meta/config

    gerrit@gerrit:~$ rm -rf cfg/

  b) Verified 功能设置 

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

    l  Projects -> List -> All-Projects

    l  Projects -> Access -> Edit -> 找到 Reference: refs/heads/* 项 

    l  -> Add Permission -> Label Verified -> Group Name 里输入 Non-Interactive Users -> 回车 或者 点击Add 按钮 -> 在最下面点击 Save Changes 保存更改

 

1.2.6. 注意

  若是想 Gitlab 上建立的项目使用 Gerrit 的 Code Review 功能,两个系统的用户必须统一,也就是说无论哪一个用户使用 Gerrit,前提是这个用户在 Gitlab 和 Gerrit 上都已注册,邮箱一致、sshkey 一致。

1.2.7 遇到问题

  • Error Missing project All-Projects

    重装java -jar gerrit.war init -d /home/gerrit/gerrit_dir 

    注意:若是从新初始化了gerrit,而且版本库路径与之前的同样,记得删除版本看路径下的All-projects.git 这个目录, 否则第一个用户登陆失败,没法获取超级管理员权限

 

  • Unable to authenticate user by Authorization request header. Check container or server configuration

    问题缘由:因为版本库下有子目录,经过nginx代理是指定的目录不正确致使 把 proxy_pass http://192.168.0.122:8090/; 改成 proxy_pass http://192.168.0.122:8090; ##不要最后的斜杠

 

  • com.google.gerrit.httpd.auth.container.HttpLoginServlet : Unable to authenticate user by Authorization request header.  Check container or server configuration.

    注意:邮箱验证的时候,使用nginx提供的端口进入,不要直接使用gerrit的端口进入验证

    测试这里是使用8082端口,不要访问8081端口 

 

  • 使用nginx用户验证的方式,没法使用sign out, gerrit没法调用nginx

1.3. Jenkins

1.3.1 简介

  Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。

1.3.2. 环境搭建

  a) Install Jenkins

    wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

    sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'

    sudo apt-get update

    sudo apt-get install jenkins

  b) Starting Jenkins

    sudo nohup java -jar /usr/share/jenkins/jenkins.war > /var/log/jenkins_log 2>&1 & 

  c) 开启

    http://xxxxx:8080/ 

  d) 插件安装

    Git Plugin

    SSH Plugin

    URL Trigger Plugin

    Gerrit Trigger Plugin 

  e) 注册用户

    先开启用户注册按钮,用户注册完毕之后,关闭用户注册

    

    Gerrit Trigger

 

1.3.2.  Gerrit Verified支持

1.3.2.1.  Gerrit Triggle插件配置

  a) 安装gerrit triggle插件

    Gerrit Triggle 配置文档https://wiki.jenkins-ci.org/display/JENKINS/Gerrit+Trigger

  b) 设置Gerrit Trigger

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

    Add New Server : Check4Gerrit

    勾选 Gerrit Server With Default Configurations

    

    

  c) 填写gerrit的jenkins帐户信息,并测试

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

     

  d) Gerrit Triggle设置成功

     

  e) Gerrit Triggle重启保证状态正常

    

1.3.2.1.  Verified项目配置

    只要gerrit有提交代码,jenkins都能检测到,并verified代码正确性,而后把结果告诉gerrit

    a) Jenkins上建立test项目

      用 jenkins 登陆http://10.24.6.12:8080/

       

    b) Git页面设置 

      参考文档https://wiki.jenkins-ci.org/display/JENKINS/Gerrit+Trigger

        

      

      

    c) Build页面设置 

      Add build step -> Execute shell 
      cd $WORKSPACE 
      [ ! -e .testrepository ] && testr init 
      testr run

      此处测试代码可依据项目状况而定,可为空

      

1.3.2. 邮件支持

  

  

  邮件发送测试 

  

1.4.  Git 客户端 review支持

  须要安装git-review, readline

  Apt-get install git-review

 

  Linux :pip install readline

  Windows:pip install pyreadline

  

1.5.  Review+Test+Push实例

1.5.1. 流程

  a) 下载项目

    git clone http://10.24.6.8/root/test.git

    cd test

  b) 设置添加change_id钩子

    增长一个hook, 自动增长chang_id到提交日志里面,必须

    gitdir=$(git rev-parse --git-dir); scp -p -P 29418 test1@10.24.6.12:hooks/commit-msg ${gitdir}/hooks/

  c) 改变代码:

    vim t99.txt

  d) 上传代码:

    git add .

    注意使用amend进行提交changid

    git commit –amend

  e) 在gerrit上看到提交

    

  f) Jenkins 自动获取提交信息并判断是否verified

    

    

  g) 在gerrit上添加review成员

    

  h) REVIEW成员收到邮件

    

  i) REVIEW 人员登录处理

    

  j) admin点击CodeReview2

    

  k) Jenkins自动点击,而后submit

    

  l) GitLab 显示commit记录

    

  m) 邮件通知REVIEW人员说明

    

  n) REVIEW 提交者收到消息

    

1.5.2. 遇到问题

  a) 设置git提交的默认编辑器为vim

    If you want to set the editor only for Git, do either (you don’t need both):

    •   Set core.editor in your Git config: git config --global core.editor "vim"
    •   Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim

 

  b) missing Change-Id in commit message footer

    须要安装插件,并使用git commit –amend提交代码,看截图部分

    

  c) GIT REVIEW remote rejected for change closed

    remote: Processing changes: refs: 1, done
    To ssh://xxxxx@gerrit.dev.xxxxx.net:29418/xxxxxx
    ! [remote rejected] HEAD -> refs/for/master (change 14823 closed)
    error: failed to push some refs to 'ssh://xxxxx@gerrit.dev.xxxxx.net:29418/xxxxxx'
 
    缘由是注释和以前提交的同样,而且已经被closed
    使用git commit –amend修改注释

  d) git rebase –continue Bug for need merge

    

    例如冲突文件为foo.c
    使用git-svn rebase获取svn服务器上的最新文件,致使冲突,
    不过此时svn版本信息已经添加到本地git库中(经过git log能够查看)
    打开foo.c,修改代码,解决冲突;
    执行git rebase --continue,git提示我:
        You must edit all merge conflicts and then
        mark them as resolved using git add
    执行git add foo.c,告知git已完成冲突解决;
    再次执行git rebase --continue,提示"Applying: git v1",此时"git v1"版本又一次成功加入本地版本库,你可经过git log查看;
    执行git-svn dcommit将foo.c的改动同步到svn中心库,到此算是完成一次冲突解决。

    git比较智能,会提示你解决问题的方法,因此遇到问题时注意看提示文档。

2015-12-09 11:13:03

相关文章
相关标签/搜索