设为首页 收藏本站
查看: 1000|回复: 0

[经验分享] gitlab+gerrit+jenkins持续集成框架

[复制链接]

尚未签到

发表于 2018-1-6 19:24:33 | 显示全部楼层 |阅读模式
1.持续集成之gitlab+gerrit+jenkins

1.1. GitLab

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

1.1.2. 环境搭建
  a) Install and configure the necessary dependencies
  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.
  On Centos 6 and 7, the commands below will also open HTTP and SSH access in the system firewall.
  sudo apt-get install curl openssh-server ca-certificates postfix
  b) Add the GitLab package server and install the package.




    • Apt-get安装

  Curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh|sudo bash
  sudo apt-get install gitlab-ee
  If you are not comfortable installing the repository through a piped script, you can find the entire script here.
  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的从备份恢复也非常简单:
  

    # 停止相关数据连接服务  

    gitlab-ctl stop unicorn  

    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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-432335-1-1.html 上篇帖子: Jenkins +JUnit 下篇帖子: jenkins 入门教程(下)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表