〇、概述
0.1 背景及目的
我司新手村任务:
- 安装运行gerrit;
- 建立一个gerrit仓库,使用git/repo下载上传代码;
- 安装运行jenkins;
- 在jenkins新建一个item,当gerrit有新的change时自动触发,执行测试用例及代码扫描,输出测试用例/代码覆盖率/代码扫描结果;
- 在jenkins新建一个item,当gerrit有新的merge时自动触发,执行构建。
在此记录一下过程。
0.2 概念介绍
- Gerrit: 一种免费、开放源代码的代码审查软件,使用网页界面。利用网页浏览器,同一个团队的软件程序员,可以相互审阅彼此修改后的程序代码,决定是否能够提交,退回或者继续修改。它使用Git作为底层版本控制系统。它分支自Rietveld,作者为Google公司的Shawn Pearce,原先是为了管理Android计划而产生。(百度百科)
- Repo: Repo is a tool that we built on top of Git. Repo helps us manage the many Git repositories, does the uploads to our revision control system, and automates parts of the Android development workflow. Repo is not meant to replace Git, only to make it easier to work with Git in the context of Android. The repo command is an executable Python script that you can put anywhere in your path.(https://code.google.com/archive/p/git-repo/)
- Jenkins: The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.(https://www.jenkins.io/)
0.3 整体流程
jenkins.png一、环境安装
1.1 基础环境
- java
- git
- docker & docker-compose
基础环境安装省略。
1.2 安装gerrit
1.2.1 docker-compose配置
mkdir gerrit & cd gerrit
touch docker-compose.yaml & vim docker-compose.yaml
version: '3'
services:
gerrit:
image: gerritcodereview/gerrit
ports:
- "29418:29418"
- "80:8080"
depends_on:
- ldap
volumes:
- /Users/syrius/workspace/task1/gerrit/data/etc:/var/gerrit/etc
- /Users/syrius/workspace/task1/gerrit/data/git:/var/gerrit/git
- /Users/syrius/workspace/task1/gerrit/data/db:/var/gerrit/db
- /Users/syrius/workspace/task1/gerrit/data/index:/var/gerrit/index
- /Users/syrius/workspace/task1/gerrit/data/cache:/var/gerrit/cache
environment:
- CANONICAL_WEB_URL=http://localhost
# command: init
ldap:
image: osixia/openldap
ports:
- "389:389"
- "636:636"
environment:
- LDAP_ADMIN_PASSWORD=secret
volumes:
- /Users/syrius/workspace/task1/gerrit/data/ldap/var:/var/lib/ldap
- /Users/syrius/workspace/task1/gerrit/data/ldap/etc:/etc/ldap/slapd.d
ldap-admin:
image: osixia/phpldapadmin
ports:
- "6443:443"
environment:
- PHPLDAPADMIN_LDAP_HOSTS=ldap
1.2.2 初始化
取消注释docker-compose.yaml中的command: init
,然后运行docker-compose up gerrit
初始化
1.2.3 运行
注释docker-compose.yaml中的command: init
,然后运行docker-compose up -d
1.2.4 新建用户
-
访问ldap控制台: https://localhost:6443,输入默认用户名:
cn=admin,dc=example,dc=org
,默认密码:secret
登录。 -
新建两个用户,一个提交代码,一个code review:
cn=admin,dc=example,dc=org -> Create a child entry -> Courier Mail: Account -> 填写表单 -> Create Object
image-20220430125154724.png
-
访问http://localhost/, 登录gerrit
image-20220430125611572.png
-
配置ssh公钥
-
ssh-keygen -m PEM -t rsa -C "gerrit@syriusrobotics.com"
-
配置ssh登录用户
vim ~/.ssh/configHost localhost Hostname 192.168.0.104 User gerrit Port 29418 IdentityFile ~/.ssh/id_rsa
-
将
image-20220430132149596.png~/.ssh/id_rsa.pub
中的内容配置到gerrit:
-
-
将用户添加到Service User组
BROWSE -> Group -> Members -> Search & Add
1.2.5 新建仓库
- BROWSE -> Repository -> CREATE NEW
- 配置仓库访问权限
赋予Service User用户组 Label Code-Review 与 Label Verified +1/-1 投票权限
image-20220430134607590.png1.3 安装jenkins
1.3.1 运行
docker run --network=bridge -d --name jenkins -p 8080:8080 -p 50000:50000 -v /Users/syrius/workspace/task1/jenkins_home:/var/jenkins_home -v /Users/syrius/.ssh/:/var/jenkins_home/.ssh/ -v /Users/syrius/softs/apache-maven-3.8.5:/var/jenkins_home/maven jenkins/jenkins
1.3.2 登录
访问地址: http://localhost:8080/
默认用户名: admin
默认密码: cat jenkins_home/secrets/initialAdminPassword
1.3.3 安装Gerrit Trigger插件
-
在jenkins plugin manger搜索并安装ç插件;
-
Manage Jenkins -> Gerrit Trigger -> Add new Server,配置gerrit服务
ssh连接信息:
image-20220430131705897.pnghttp连接信息(需展开Advaned选项):
image-20220430131847346.png1.3.4 安装juint插件
juint插件用于收集单元测试报告
image-20220430133246400.png1.3.5 安装cobertura插件
cobertura插件用于收集单元测试代码覆盖率报告
image-20220430133520005.png1.3.6 安装Warnings Next Generation插件
Warnings Next Generation用于执行代码扫描
image-20220430133757338.png二、测试流水线
2.1 新建item
image-20220430134056234.png2.2 配置gerrit
Refspec: refs/changes/:refs/changes/
Branch Specifier: $GERRIT_REFSPEC
image-20220430135446275.png添加触发事件: Patchset Created
image-20220430135935348.png2.3 配置执行脚本
/var/jenkins_home/maven/bin/mvn clean compile surefire-report:report cobertura:cobertura checkstyle:checkstyle
2.4 测试报告收集
image-20220430140443061.png2.5 代码覆盖率报告收集
image-20220430140505029.png2.6 代码扫描结果收集
image-20220430140532692.png三、构建流水线
与测试流水线相似,但更简单。
- 添加触发事件: Change Merged
-
配置执行脚本
/var/jenkins_home/maven/bin/mvn clean package -Dcheckstyle.skip=true
四、演示
4.1 检出仓库
- 使用以下命令,检出仓库后添加钩子函数,用于向commit msg添加Change-Id
git clone "ssh://gerrit@localhost:29418/hello-syrius" && scp -p -P 29418 gerrit@localhost:hooks/commit-msg "hello-syrius/.git/hooks/"
或
git clone "http://gerrit@localhost/a/hello-syrius" && (cd "hello-syrius" && mkdir -p .git/hooks && curl -Lo
git rev-parse --git-dir/hooks/commit-msg http://gerrit@localhost/tools/hooks/commit-msg; chmod +x
git rev-parse --git-dir/hooks/commit-msg)
-
新建一个maven项目,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.syrius.task1</groupId> <artifactId>hello-syrius</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> <includes> <include>**/*Test.java</include> </includes> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.5.2</version> <configuration> <outputDirectory>target/report</outputDirectory> <encoding>UTF-8</encoding> <formats> <format>xml</format> </formats> <check/> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.0.0</version> <configuration> <!-- XXXX --> <!-- 是否有输出文件 --> <consoleOutput>true</consoleOutput> <!-- 编码方式 --> <encoding>utf-8</encoding> <!-- 是否因为不符合的规范中断mvn进程 --> <failsOnError>false</failsOnError> <linkXRef>false</linkXRef> <violationSeverity>error</violationSeverity> </configuration> <executions> <execution> <id>validate</id> <phase>validate</phase> <goals> <goal>check</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.0.0</version> <reportSets> <reportSet> <reports> <report>check</report> </reports> </reportSet> </reportSets> </plugin> </plugins> </reporting> </project>
4.2 commit & push
注意push时需要指定分支为HEAD:refs/for/master
,而不是目标分支master。这是gerrit生成review的机制。
截图中可以看到gerrit生成了id为123的change.
image-20220430142117185.png4.3 gerrit change
image-20220430142451798.png4.4 jenkins测试流水线
image-20220430142655957.png image-20220430142749345.png4.5 gerrit submit
- 当前的Code-Review +1 和 Verfied + 1是jenkins流水线返回的
- 人工Code-Review后,投出+2的票数,Code-Review通过
- Submit提交变更,Rebase到目前分支
4.6 jenkins 构建流水线
image-20220430143338348.png五、使用repo
5.1 下载安装
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
5.2 初始化
-
在gerrit新建一个manifests仓库,在仓库中放置一个default.xml文件,写入以下内容:
<?xml version="1.0" encoding="UTF-8"?> <manifest> <remote fetch="http://localhost" name="origin" review="http://localhost"/> <default remote="origin" revision="master"/> <project name="hello-syrius" path="hello-syrius"/> </manifest>
-
初始化仓库
在任意空目录执行以下命令:
repo init -u http://localhost/manifests --repo-url=https://gerrit-googlesource.lug.ustc.edu.cn/git-repo
-
拉取最新代码
repo sync
5.3 新建一个分支
repo start feature-1
5.4 提交一个变更
repo upload
六、后续步骤
mvn deploy 或 gradle publish 或 docker push
没装环境,不写了。
网友评论