前提介绍
原文连接,看着更舒服:http://www.duozhi.online/2023/10/17/jenkins-%e9%9b%86%e6%88%90gerrit/
jenkins安装参考
http://www.duozhi.online/2023/10/12/jenkins%e5%ae%89%e8%a3%85/
gerrit安装参考
http://www.duozhi.online/2023/10/12/gerrit%e5%ae%89%e8%a3%85/
gerrit ldap配置
http://www.duozhi.online/2023/10/13/gerrit-ldap%e9%85%8d%e7%bd%ae/
后来虚拟机又重新创建了,所以IP换了,但是安装步骤是相同的
Jenkins 地址:http://192.168.72.135:8801/login
gerrit 地址:http://192.168.72.135:8802/
Jenkins服务器上,root用户生成公钥
一定要是-m PEM,否则jenkins配置gerrit trigger的时候,会报/root/.ssh/.id_rsa无效
root@lili:~# ssh-keygen -m PEM
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:JQgPP4MLWn9KoafgKjuYD53968H7t2yvgZWwBbU8enw root@lili
The key's randomart image is:
+---[RSA 3072]----+
o ...
* . o .
o o * o *
o + o o O o
o . = . S = E
.o * + + .
oo+ o o . .
*. . o .o.
++. .=o.o++.
+----[SHA256]-----+
root@lili:~#
root@lili:~# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6YvUJbs/1JJnFRdCxSzILEulp8XdSPf2+/QtCelnrM1YfVKpruGPVBENi3pU2/dxb78YFun16/pyC/Y9Xq50QG5bwnbTKn4QmmTBs0NtbhfpcB2w+oFQMPpLnaYy1edXEXXzUcShA6oDHhaylJlKRBntHUvucRxvveHN912F9Yxmura2pdoZ9jXGEkdBQ+OYH32qTzaOxCtZ9D4q/X81zYMqfnWdgKXDleSaZZr0JPcOnYhqtthsR/SQHaGQklEdH2XxHaIb+f4tgADZkzcZud3R82QJ8h92A5jmvUQf4mPnFYCl7kI9vN3uPFgabmT7l0C1Qv/m1sjxwecN47dQ7UOzcvmCHI7gt+sx4BhCwk/5Ed3+6au2vr+XBiOJLXZVYxKft/w7XB1b0fO44kj4MrUpPQ5PyLH6xtK8nfyBhPL2WIERXU4sLnH1DOx/rX7bSY5j3sf65VY//BeZl4cd51OiGtNwpgmMsFduX9gcU5Jidak59OkVGScK97mieVl8= root@lili
root@lili:~#
拷贝上面公钥到gerrit,因为jenkins要链接gerrit
登陆gerrit
settings
复制公钥
gerrit配置verified
配置git clone的用户
我这里配置了管理员的账号和邮箱
找一个能进行git clone的机器,你的笔记本或任何一个虚拟机都行,进行如下配置
root@lili:~/test# git config --global user.name "zhangsan"
root@lili:~/test# git config --global user.email "1394252416@qq.com"
root@lili:~/test# git config -l
user.name=zhangsan
user.email=1394252416@qq.com
root@lili:~/test#
在机器上生成公钥,放到gerrit中,参考上面
由于我是在jenkins服务器上操作的,所以不需要再次操作
把All project clone下来修改
root@lili:~/test# git clone "ssh://zhangsan@192.168.72.135:29418/All-Projects" && scp -p -P 29418 zhangsan@192.168.72.135:hooks/commit-msg "All-Projects/.git/hooks/"
Cloning into 'All-Projects'...
The authenticity of host '[192.168.72.135]:29418 ([192.168.72.135]:29418)' can't be established.
ECDSA key fingerprint is SHA256:AZ2pstJFa3dZCVMVw+mVsW4INghn1tFe4rOD3BKUqXo.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[192.168.72.135]:29418' (ECDSA) to the list of known hosts.
remote: Counting objects: 4, done
remote: Finding sources: 100% (4/4)
remote: Total 4 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4/4), done.
Note: switching to '123fea35f8a17ffae7610768ad4feddffcfb8fdf'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
commit-msg 100% 2272 1.7MB/s 00:00
root@lili:~/test# ls -l
total 4
drwxr-xr-x 3 root root 4096 Oct 17 05:47 All-Projects
root@lili:~/test# cd All-Projects/
root@lili:~/test/All-Projects# ls -l
total 8
-rw-r--r-- 1 root root 336 Oct 17 05:47 groups
-rw-r--r-- 1 root root 2204 Oct 17 05:47 project.config
root@lili:~/test/All-Projects# vim project.config
在文件结尾增加如下内容
[label "Verified"]
function = MaxWithBlock
value = -1 Fails
value = -2 Fails
value = 0 No score
value = +1 Verified
value = +2 Approved
root@lili:~/test/All-Projects# git add .
root@lili:~/test/All-Projects# git commit -am "wulili"
[detached HEAD 24bd924] wulili
1 file changed, 5 insertions(+)
我这里提交报错,说格式不对,可能是有空格什么的,复制不行,就手写吧
root@lili:~/test/All-Projects# git push origin HEAD:refs/meta/config
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (3/3), done.
remote: Resolving deltas: 100% (1/1)
Writing objects: 100% (3/3), 426 bytes 35.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Processing changes: refs: 1, done
To ssh://192.168.72.135:29418/All-Projects
123fea3..24bd924 HEAD -> refs/meta/config
提交后在网页上查看下
配置gerrit中权限
把zhangsan加到Service Users组中
配置All-Projects Access
gerrit中生成HTTP密码
AXpGjzr+yr84g0Kz19on/8mgw0cohSaJgqZO2y0SJg
复制下来保存好
Jenkins配置gerri trigger
安装gerrit插件
插件配置
点击create后,重新编辑
输入上面的http密码
gerrit中创建一个仓库
Jenkins中创建一个job
提交代码测试
root@lili:~/test# git clone "ssh://zhangsan@192.168.72.135:29418/test" && scp -p -P 29418 zhangsan@192.168.72.135:hooks/commit-msg "test/.git/hooks/"
Cloning into 'test'...
The authenticity of host '[192.168.72.135]:29418 ([192.168.72.135]:29418)' can't be established.
ECDSA key fingerprint is SHA256:AZ2pstJFa3dZCVMVw+mVsW4INghn1tFe4rOD3BKUqXo.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[192.168.72.135]:29418' (ECDSA) to the list of known hosts.
remote: Counting objects: 2, done
remote: Finding sources: 100% (2/2)
Receiving objects: 100% (2/2), 190 bytes 190.00 KiB/s, done.
remote: Total 2 (delta 0), reused 0 (delta 0)
commit-msg 100% 2272 3.1MB/s 00:00
root@lili:~/test# cd test/
root@lili:~/test/test# ls -l
total 0
root@lili:~/test/test# echo filetest>file1
root@lili:~/test/test# git add .
root@lili:~/test/test# git commit -am "add file1"
[master b76fb35] add file1
1 file changed, 1 insertion(+)
create mode 100644 file1
root@lili:~/test/test#
root@lili:~/test/test# git push origin HEAD:refs/for/master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 279 bytes 279.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Processing changes: refs: 1, new: 1, done
remote:
remote: SUCCESS
remote:
remote: http://192.168.72.135:8802/c/test/+/1 add file1 [NEW]
remote:
To ssh://192.168.72.135:29418/test
* [new branch] HEAD -> refs/for/master
root@lili:~/test/test#
会发现jenkins上有一个gerrit触发