在Docker Hub中gerrit的官方地址查看了相关说明文档,其推荐使用docker compose的方式运行服务,对应的docker-compose.yml文件样例如下:
version: '3'
services:
gerrit:
image: gerritcodereview/gerrit
ports:
- "29418:29418"
- "80:8080"
depends_on:
- ldap
volumes:
- /external/gerrit/etc:/var/gerrit/etc
- /external/gerrit/git:/var/gerrit/git
- /external/gerrit/db:/var/gerrit/db
- /external/gerrit/index:/var/gerrit/index
- /external/gerrit/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:
- /external/gerrit/ldap/var:/var/lib/ldap
- /external/gerrit/ldap/etc:/etc/ldap/slapd.d
ldap-admin:
image: osixia/phpldapadmin
ports:
- "6443:443"
environment:
- PHPLDAPADMIN_LDAP_HOSTS=ldap
推荐采用OpenLDAP来管理用户的帐户密码,尝试着按照推荐的方式搭建了服务,在第一步“配置Gerrit的管理员账号”访问形如“https:xxx.xxx.xxx.xxx:6443”地址时页面一直报“重定向次数过多”的错误,定位尝试许久,未能解决问题。于是尝试直接使用官方展示的单gerrit服务容器方案,对应的docker-compose.yml文件样例如下:
version: '1'
services:
gerrit:
image: gerritcodereview/gerrit:3.8.1
ports:
- "29418:29418"
- "80:8080"
volumes:
- /data/gerrit/etc:/var/gerrit/etc
- /data/gerrit/git:/var/gerrit/git
- /data/gerrit/db:/var/gerrit/db
- /data/gerrit/index:/var/gerrit/index
- /data/gerrit/cache:/var/gerrit/cache
environment:
- CANONICAL_WEB_URL=http://gerrit.example.com
command: init
通过配置的域名可以访问“http://gerrit.example.com”,出现了登录页面,要求使用OpenID登录或者使用OpenLDAP生成的帐户密码访问,尝试了相应方式,未能成功,考虑到前面搭建的gerrit服务采用的就是在http请求那里进行登录验证的方式,也就不折腾了,直接走已经验证过的路,分别参考了代码审查服务gerrit搭建+httpd反向代理、前面自己写的“CentOS 7 搭建gerrit服务总结”以及看起来像是官方的说明文档,确定了整体思路,接下来就是具体实施了。
看起来像是官方说明的链接地址访问需要,这里直接将相关信息罗列如下,有需要可参考:
HTTP Basic Authentication
When using HTTP authentication, Gerrit assumes that the servlet container or the frontend web server has performed all user authentication prior to handing the request off to Gerrit.
As a result of this assumption, Gerrit can assume that any and all requests have already been authenticated. The "Sign In" and "Sign Out" links are therefore not displayed in the web UI.
To enable this form of authentication:
git config --file $site_path/etc/gerrit.config auth.type HTTP
git config --file $site_path/etc/gerrit.config --unset auth.httpHeader
git config --file $site_path/etc/gerrit.config auth.emailFormat '{0}@example.com'
The auth.type must always be HTTP, indicating the user identity will be obtained from the HTTP authorization data.
The auth.httpHeader must always be unset. If set to any value (including Authorization) then Gerrit won’t correctly honor the standard Authorization HTTP header.
The auth.emailFormat field ('optional') sets the preferred email address during first login. Gerrit will replace {0} with the username, as obtained from the Authorization header. A format such as shown in the example would be typical, to add the domain name of the organization.
If Apache HTTPd is being used as the primary web server and the Apache server will be handling user authentication, a configuration such as the following is recommended to ensure Apache performs the authentication at the proper time:
<Location "/login/">
AuthType Basic
AuthName "Gerrit Code Review"
Require valid-user
...
</Location>
实际采用的docker-compose.yaml文件样例如下:
version: '1'
services:
gerrit:
image: gerritcodereview/gerrit:3.8.1
restart: always
ports:
- "29418:29418"
- "8080:8080"
volumes:
- /data/gerrit/etc:/var/gerrit/etc
- /data/gerrit/git:/var/gerrit/git
- /data/gerrit/db:/var/gerrit/db
- /data/gerrit/index:/var/gerrit/index
- /data/gerrit/cache:/var/gerrit/cache
environment:
- CANONICAL_WEB_URL=http://gerrit.example.com
command: init
新建文件内容可采用下面命令的形式简化操作:
cat > /root/docker/gerrit/docker-compose.yml << EOF
version: '1'
services:
gerrit:
image: gerritcodereview/gerrit:3.8.1
restart: always
...省略了文件内容,实际使用时按真实的来...
command: init
EOF
docker compose up gerrit
考虑到运行的gerrit服务容器中内部没有httpd服务,因此在宿主机上安装httpd服务并启动,同时也设置为自启动,在root帐户下需要执行的相关命令如下:
yum -y install httpd
systemctl start httpd
systemctl enable httpd
创建存放帐户密码的空文件:
touch /etc/httpd/conf.d/passwd
生成用于gerrit跳转的httpd配置文件
cat > /etc/httpd/conf.d/gerrit.conf << EOF
ServerName gerrit.example.com
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
AllowEncodedSlashes On
Order deny,allow
Allow from all
AuthType Basic
AuthName "Gerrit Code Review"
Require valid-user
AuthBasicProvider file
AuthUserFile /etc/httpd/conf.d/passwd
ProxyPass / http://127.0.0.1:8080/
EOF
增加用户(使用"-c"参数会创建文件,只是新增用户不需要该操作):
htpasswd -cb /etc/httpd/passwd admin 123456
htpasswd -b /etc/httpd/conf.d/passwd admin YourPassword
htpasswd -b /etc/httpd/conf.d/passwd zhangs YourPassword
更新配置文件gerrit.config(gerrit服务容器中对应地址为“/var/gerrit/etc/gerrit.config”,更新操作可直接在宿主机上相应文件操作即可)
样例如下:
[gerrit]
basePath = git
canonicalWebUrl = http://gerrit.example.com
serverId = f08520dc-9ea5-48b2-b49f-66d4fd2aad80
[container]
javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
user = gerrit
javaHome = /usr/lib/jvm/java-11-openjdk-11.0.19.0.7-4.el9.alma.x86_64
javaOptions = -Djava.security.egd=file:/dev/./urandom
javaOptions = --add-opens java.base/java.net=ALL-UNNAMED
javaOptions = --add-opens java.base/java.lang.invoke=ALL-UNNAMED
[index]
type = lucene
[auth]
type = HTTP
[receive]
enableSignedPush = true
[sendemail]
enable = true
smtpServer = smtp.mxhichina.com
smtpServerPort = 465
smtpEncryption = SSL
smtpUser = [email protected]
sslVerify = false
from = CodeReview<[email protected]>
[sshd]
listenAddress = *:29418
[httpd]
listenUrl = http://*:8080/
[cache]
directory = cache
更新发送邮箱密码
调整容器中文件“/var/gerrit/etc/secure.config”中sendemail部分的smtpPass配置,可修改发送邮箱的密码。样例如下:
[auth]
registerEmailPrivateKey = albUUgKWNFRcCMVhsLkVg87lQddBcmcMkm4=
[sendemail]
smtpPass = your@passwordhere
配置完毕之后执行下面命令重启httpd服务:
systemctl restart httpd
将docker-compose.yaml文件中的“command: init”注释掉,更新文件中的version值为“2”,文件内容样例如下:
version: '2'
services:
gerrit:
image: gerritcodereview/gerrit:3.8.1
restart: always
ports:
- "29418:29418"
- "8080:8080"
volumes:
- /data/gerrit/etc:/var/gerrit/etc
- /data/gerrit/git:/var/gerrit/git
- /data/gerrit/db:/var/gerrit/db
- /data/gerrit/index:/var/gerrit/index
- /data/gerrit/cache:/var/gerrit/cache
environment:
- CANONICAL_WEB_URL=http://gerrit.example.com
# command: init
再调用下面命令启动gerrit服务:
docker compose up -d
在容器中目录“/var/gerrit/etc/”下已经生成好了密钥对,可选择“ssh_host_ed25519_key.pub”配置到对应的gitlab帐户上,便于后续拉取代码。
使用下面命令进容器:
sudo docker exec -it -u root gerrit-gerrit-1 bash
在容器里面执行下面命令将ssh key放到指定位置:
cp /var/gerrit/etc/ssh_host_ed25519_key /var/gerrit/.ssh/id_ed25519
修改所有者信息:
chown gerrit:gerrit /var/gerrit/.ssh/id_ed25519
如果需要用到指定的域名解析,可在宿主机上进行配置,修改daemon.json文件(保持其他内容不变,更新dns信息配置),查看内容命令如下:
cat /etc/docker/daemon.json
{
"dns": ["172.18.0.52", "172.18.0.70", "183.XX.XX.XX"]
}
更新完配置后,调用下面命令重启docker:
systemctl daemon-reload
systemctl restart docker
要验证域名解析是否生效,可在容器中执行类似下面的命令验证:
ping gitlab.example.com
假设在gitlab上有一个项目testrepo,则进到gerrit网页在仓库那里相应创建一个testrepo项目。
使用下面命令进容器:
sudo docker exec -it -u gerrit gerrit-gerrit-1 bash
进入到目录“/var/gerrit/git”下,执行下面命令删除自动创建的目录:
cd /var/gerrit/git
rm -rf testrepo.git/
使用下面命令克隆gitlab代码:
git clone --bare [email protected]:test/testrepo.git ./testrepo.git
可在下面网址查看看插件状态:
http://gerrit.example.com/admin/plugins
使用下面命令生成replication.config文件(对应容器目录为“/var/gerrit/etc/”):
cd /var/gerrit/etc/
cat > replication.config << EOF
[remote "testrepo"]
projects = testrepo
url = [email protected]:test/testrepo.git
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/*
threads = 3
EOF
更新完replication.config文件后,在宿主机上docker-compose.yaml目录下使用下面的命令重启容器:
docker compose restart
用户权限的配置等相关使用请直接参考gerrit使用小结,在此就不赘述了。
参考链接:
docker-gerrit github网页