CI 系统搭建:五. GitLab、Gerrit、Jenkins 三者整合

2024-03-16 10:38

本文主要是介绍CI 系统搭建:五. GitLab、Gerrit、Jenkins 三者整合,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • CI 系统搭建:一. 基础环境设置、规划
  • CI 系统搭建:二. GitLab 的安装配置
  • CI 系统搭建:三. Gerrit 的安装配置
  • CI 系统搭建:四. Jenkins 的安装配置

目录

  • 1 Gerrit 和 Jenkins 整合
  • 2 GitLab 上为 openstack 项目的一些准备
    • 2.1 .gitreview
    • 2.2 .testr.conf
  • 3 Gerrit 上为 openstack 项目的一些准备
    • 3.1 在 Gerrit 上创建 openstack 项目
    • 3.2 clone –bare Gitlab 上的仓库到 Gerrit
    • 3.3 同步 Gerrit 的 openstack 项目到 Gitlab 上的 openstack 项目目录中
  • 4 在 Jenkins 上对 openstack 项目创建构建任务
  • 5 提交 Review 任务

Gerrit 和 Jenkins 整合

让 Gerrit 支持 Jenkins,Gerrit 在 2.7 版本后去掉了 ‘lable Verified’,需要自己添加

# cd /tmp
# git init cfg; cd cfg
# git config user.name 'admin'
# git config user.email 'admin@thstack.com'
# git remote add origin ssh://admin@review.thstack.com:29418/All-Projects
# git pull origin refs/meta/config
# vim project.config        # 在文件末尾添加下面 5 行
[label "Verified"]function = MaxWithBlockvalue = -1 Failsvalue =  0 No scorevalue = +1 Verified
# git commit -a -m 'Updated permissions'
# git push origin HEAD:refs/meta/config

查看 Jenkins 的 log 发现会一直出现下面的信息,是因为 Jenkins 没有权限监听 Gerrit 的 ‘Stream Events’

# tail -f /var/log/jenkins/jenkins.log 
Dec 26, 2013 1:16:49 AM com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler run
INFO: Ready to receive data from Gerrit
Dec 26, 2013 1:16:49 AM com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler run
INFO: Ready to receive data from Gerrit

在 Gerrit 全局设置中能看到 Gerrit 的 ‘Stream Events’ 动作权限默认对 ‘Non-Interactive Users’ 组开放

gerrit-Non-Interactive-Users

用 admin@thstack.com 用户登录 Gerrit 系统,添加 Jenkins@thstack.com 用户到 ‘Non-Interactive Users’ 组

点击 -> People -> List Groups -> Non-Interactive Users gerrit-add-jenkins-user-in-non-interactive-group

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

点击 Projects -> Access -> Edit -> 找到 Reference: refs/heads/* 项 -> Add Permission -> Label Verified -> Group Name 里输入 Non-Interactive Users -> Add -> 在最下面点击保存更改 gerrit-add-verified-lable

GitLab 上为 openstack 项目的一些准备

.gitreview

之前在 CI 系统搭建:二. GitLab 的安装配置 文章中在 GitLab 上创建了一个叫 openstack 项目,并设置了权限,普通用户是没有办法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在于项目目录里,用 admin@thstack.com 用户添加 .gitreview 文件

git clone git@gitlab.thstack.com:devgroup/openstack.git
cd openstack
git config user.name 'admin'
git config user.email 'admin@thstack.com'

编辑 .gitreview 文件

vim .gitreview
[gerrit]
host=review.thstack.com
port=29418
project=openstack.git

添加到版本库

git add .gitreview
git commit .gitreview -m 'Admin add .gitreview file.'
git push origin master

.testr.conf

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

apt-get install python-pip
pip install testrepository

在 openstack 这个项目中添加 .testr.conf 文件

cd openstack
vim .testr.conf
[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

提交到版本库中

git add .testr.conf
git commit .testr.conf -m 'Admin add .testr.conf file'
git push origin master

Gerrit 上为 openstack 项目的一些准备

在 Gerrit 上创建 openstack 项目

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

用 admin 用户在 Gerrit 上创建 openstack 项目

ssh -p 29418 admin@review.thstack.com gerrit create-project openstack

clone –bare Gitlab 上的仓库到 Gerrit

在 Gerrit 上 clone Gitlab 的 openstack 项目

cd /etc/gerrit/git
rm -fr openstack.git
git clone --bare git@gitlab.thstack.com:devgroup/openstack.git

同步 Gerrit 的 openstack 项目到 Gitlab 上的 openstack 项目目录中

当用户 git review 后,代码通过 jenkins 测试、人工 review 后,代码只是 merge 到了 Gerrit 的 openstack 项目中,并没有 merge 到 Gitlab 的 openstack 项目中,所以需要当 Gerrit openstack 项目仓库有变化时自动同步到 Gitlab 的 openstack 项目仓库中。Gerrit 自带一个 Replication 功能,同时我们在安装 Gerrit 时候默认安装了这个 Plugin。现在只需要添加一个 replication.config 给 Gerrit。

添加 replication.config 文件

vim /etc/gerrit/etc/replication.config
[remote "openstack"]# Gerrit 上要同步项目的名字      projects = openstackurl = root@gitlab.thstack.com:/home/git/repositories/devgroup/openstack.gitpush = +refs/heads/*:refs/heads/*push = +refs/tags/*:refs/tags/*push = +refs/changes/*:refs/changes/*threads = 3

上面的 url 是用 root 用户来做 Gerrit 的 openstack 项目复制到 Gitlab 的 openstack 项目中,需要免密码登录,生成密钥

ssh-copy-id -i gitlab.thstack.com  # 输入 root 用户密码

设置 ~/.ssh/config

vim ~/.ssh/config
Host gitlab.thstack.com:IdentityFile ~/.ssh/id_rsaPreferredAuthentications publickey

在 ~/.ssh/known_hosts 中,给 gitlab.thstack.com 添加 rsa 密钥

> ~/.ssh/known_hosts
ssh-keyscan -t rsa gitlab.thstack.com > ~/.ssh/known_hosts
ssh-keygen -H -f ~/.ssh/known_hosts

重新启动 Gerrit 服务

/etc/init.d/gerrit restart

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

在 Jenkins 上对 openstack 项目创建构建任务

用 admin 用户登录 jenkins,针对 openstack 项目创建构建任务 点击 新建 -> 输入任务名 -> 选中构建一个自由风格的软件项目 jenkins-create-task

选中 ‘git’ -> 在 ‘Repository URL’ 里输入 openstack 项目在 Gerrit 上的地址(在 gerrit 的 project list 中 openstack 后面的 gitweb 里有具体 url,) -> 在 ‘Branches to build’ 里输入 ‘origin/$GERRIT_BRANCH’ jenkins-create-task1

选中 Gerrit event -> 点击 Add 分别添加 ‘Patchset Created’ 和 ‘Draft Published’ -> 在第一个 plain 输入项目名字 openstack,第二个 plain 输入 master -> 点击 增加构建步骤 -> 选择 ‘Execute shell’ -> 写入要测试的脚本 -> 保存 同时也可以选择 ‘添加构建后操作步骤’ 来邮件通知一个群组等功能。 jenkins-create-task2

提交 Review 任务

切换到 longgeek 用户,之前创建了一个 longgeek 用户,方便测试

su - longgeek
cd openstack   # 之前添加过一个 testfile 文件,push 没有权限,所以用 git review 来代替 git push
git push
git review
Creating a git remote called gerrit that maps to:ssh://longgeek@review.thstack.com:29418/openstack.git
Your change was committed before the commit hook was installed
Amending the commit to add a gerrit change id
remote: Processing changes: new: 1, refs: 1, done    
remote: 
remote: New Changes:
remote:   http://review.thstack.com/1
remote: 
To ssh://longgeek@review.thstack.com:29418/openstack.git
* [new branch]      HEAD -> refs/for/master/master

用 admin 用户登录 http://review.thstack.com 就可以看到 longgeek 用户提交的 reivew 请求 gerrit-review

可以看到 jenkins 已经通过,并打上了 ‘绿勾’ 标记,接下来 admin 用户 +2 并提交就可以 Merge 代码 gerrit-review-submit

在 Merge 代码后,Gerrit 会自动同步到 Gitlab 上,如下图 gitlab-replication-ok

同步后,使用 git pull 命令就可以从 Gitlab 上的 openstack 仓库下来代码

cd openstack
git pull
From gitlab.thstack.com:devgroup/openstack70c59ff..7948aae  master     -> origin/master
Already up-to-date.

如此便是一个完整的 CI 体系流程了,剩下的最重要的是如何用、真正的用起来。

这篇关于CI 系统搭建:五. GitLab、Gerrit、Jenkins 三者整合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/815199

相关文章

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

Linux系统中的firewall-offline-cmd详解(收藏版)

《Linux系统中的firewall-offline-cmd详解(收藏版)》firewall-offline-cmd是firewalld的一个命令行工具,专门设计用于在没有运行firewalld服务的... 目录主要用途基本语法选项1. 状态管理2. 区域管理3. 服务管理4. 端口管理5. ICMP 阻断

SpringBoot整合Apache Flink的详细指南

《SpringBoot整合ApacheFlink的详细指南》这篇文章主要为大家详细介绍了SpringBoot整合ApacheFlink的详细过程,涵盖环境准备,依赖配置,代码实现及运行步骤,感兴趣的... 目录1. 背景与目标2. 环境准备2.1 开发工具2.2 技术版本3. 创建 Spring Boot

Spring Boot 整合 Apache Flink 的详细过程

《SpringBoot整合ApacheFlink的详细过程》ApacheFlink是一个高性能的分布式流处理框架,而SpringBoot提供了快速构建企业级应用的能力,下面给大家介绍Spri... 目录Spring Boot 整合 Apache Flink 教程一、背景与目标二、环境准备三、创建项目 & 添

如何搭建并配置HTTPD文件服务及访问权限控制

《如何搭建并配置HTTPD文件服务及访问权限控制》:本文主要介绍如何搭建并配置HTTPD文件服务及访问权限控制的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、安装HTTPD服务二、HTTPD服务目录结构三、配置修改四、服务启动五、基于用户访问权限控制六、

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.