安装jdk11

下载jdk

# wget -O jdk-11.0.18_linux-x64_bin.tar.gz  https://download.oracle.com/otn/java/jdk/11.0.18+9/e9f4529b2fa84718a553a389cb2a6629/jdk-11.0.18_linux-x64_bin.tar.gz?AuthParam=xxxxxxxx

解压jdk

# tar xf jdk-11.0.18_linux-x64_bin.tar.gz -C /usr/local/

创建软连接

# ln -svf /usr/local/jdk-11.0.18/ /usr/local/jdk
‘/usr/local/jdk’ -> ‘/usr/local/jdk-11.0.18/’

添加环境变量

~# cat /etc/profile
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

重载环境变量

# source /etc/profile

验证jdk

# java --version
java 11.0.18 2023-01-17 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.18+9-LTS-195)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.18+9-LTS-195, mixed mode)

安装git

ubuntu

# apt -y install git

centos7

# yum -y install git

安装gerrit

创建gerrit用户

# adduser gerrit

下载gerrit

# wget https://gerrit-releases.storage.googleapis.com/gerrit-3.7.0.war

创建数据目录

# mkdir /data/apps/gerrit
# chown -R gerrit.gerrit /data/apps/gerrit

初始化gerrit

# su - gerrit
$ java -jar gerrit-3.7.0.war init -d /apps/gerrit

一路回车

============================================================================
Welcome to the Gerrit community

Find more information on the homepage: https://www.gerritcodereview.com
Discuss Gerrit on the mailing list: https://groups.google.com/g/repo-discuss
============================================================================
Initialized /data/apps/gerrit
Init complete, reindexing accounts,changes,groups,projects with: reindex --site-path /data/apps/gerrit --threads 1 --index accounts --index changes --index groups --index projects --disable-cache-statsReindexed 0 documents in accounts index in 0.0s (0.0/s)
Index accounts in version 12 is ready
Reindexing groups:      100% (2/2)
Reindexed 2 documents in groups index in 0.4s (5.7/s)
Index groups in version 9 is ready
Reindexing changes: Slicing projects: 100% (2/2), done    
Reindexed 0 documents in changes index in 0.0s (0.0/s)
Index changes in version 79 is ready
Reindexing projects:    100% (2/2)
Reindexed 2 documents in projects index in 0.0s (46.5/s)
Index projects in version 5 is ready
Executing /data/apps/gerrit/bin/gerrit.sh start
Starting Gerrit Code Review: WARNING: Could not adjust Gerrit's process for the kernel's out-of-memory killer.
         This may be caused by /data/apps/gerrit/bin/gerrit.sh not being run as root.
         Consider changing the OOM score adjustment manually for Gerrit's PID=53402 with e.g.:
         echo '-1000' | sudo tee /proc/53402/oom_score_adj
OK
Waiting for server on demo:8080 ... OK
Opening http://demo:8080/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:
  http://demo:8080/#/admin/projects/

gerrit.config

[gerrit]
	basePath = git
	canonicalWebUrl = http://demo:8080/                   # 本地IP加端口
	serverId = 220581aa-a9fe-42b8-99fb-e5f87feebca9
[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/local/jdk-11.0.18
[index]
	type = lucene
[auth]
	type = OPENID
	userNameCaseInsensitive = true
[receive]
	enableSignedPush = false
[sendemail]                   # 邮件设置
	smtpServer = localhost
[sshd]
	listenAddress = *:29418
[httpd]
	listenUrl = http://*:8080
[cache]
	directory = cache

查看端口

# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name       
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      802/sshd: /usr/sbin 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      713/systemd-resolve 
tcp6       0      0 :::29418                :::*                    LISTEN      51909/GerritCodeRev 
tcp6       0      0 :::8080                 :::*                    LISTEN      51909/GerritCodeRev 
tcp6       0      0 :::22                   :::*                    LISTEN      802/sshd: /usr/sbin

运行服务

# /data/apps/gerrit/bin/gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: WARNING: Could not adjust Gerrit's process for the kernel's out-of-memory killer.
         This may be caused by /data/apps/gerrit/bin/gerrit.sh not being run as root.
         Consider changing the OOM score adjustment manually for Gerrit's PID=54222 with e.g.:
         echo '-1000' | sudo tee /proc/54222/oom_score_adj
OK

修改监听端口

修改端口

# git config --file /data/apps/gerrit/etc/gerrit.config httpd.listenUrl 'http://192.168.174.249:18080'
# git config --file /data/apps/gerrit/etc/gerrit.config gerrit.canonicalWebUrl 'http://192.168.174.249:18080'

重启服务

# /data/apps/gerrit/bin/gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: WARNING: Could not adjust Gerrit's process for the kernel's out-of-memory killer.
         This may be caused by /data/apps/gerrit/bin/gerrit.sh not being run as root.
         Consider changing the OOM score adjustment manually for Gerrit's PID=54574 with e.g.:
         echo '-1000' | sudo tee /proc/54574/oom_score_adj
OK

查看端口

# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name       
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      802/sshd: /usr/sbin 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      713/systemd-resolve 
tcp6       0      0 :::29418                :::*                    LISTEN      54574/GerritCodeRev 
tcp6       0      0 192.168.174.249:18080   :::*                    LISTEN      54574/GerritCodeRev 
tcp6       0      0 :::22                   :::*                    LISTEN      802/sshd: /usr/sbin

访问dashboard

部署gerrit 3.7.0_nginx

nginx代理gerrit

server {
    listen        80;
    server_name gerrit.example.com;
    rewrite ^(.*)$ https://${server_name}$1 permanent;

}

server {
    listen       443 ssl;
    server_name  gerrit.example.com;
    access_log logs/gerrit_access.log main;
    error_log  logs/gerrit_error.log;
 
     ssl on;
     #从腾讯云获取到的第一个文件的全路径
     ssl_certificate /usr/local/nginx/ssl/Nginx/1_example.com_bundle.crt;
     #从腾讯云获取到的第二个文件的全路径
     ssl_certificate_key /usr/local/nginx/ssl/Nginx/2_example.com.key;
     ssl_session_timeout 5m;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
     ssl_prefer_server_ciphers on;

    location / {
        proxy_pass  http://192.168.174.249:18080;

        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_request_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;

   }
}

HTTP Basic Authentication

修改配置文件

# git config --file /data/apps/gerrit/etc/gerrit.config auth.type HTTP
# git config --file /data/apps/gerrit/etc/gerrit.config --unset auth.httpHeader
# git config --file $site_path/etc/gerrit.config auth.emailFormat '{0}@hard-chain.com'

重启gerrit

# /data/apps/gerrit/bin/gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: WARNING: Could not adjust Gerrit's process for the kernel's out-of-memory killer.
         This may be caused by /data/apps/gerrit/bin/gerrit.sh not being run as root.
         Consider changing the OOM score adjustment manually for Gerrit's PID=54574 with e.g.:
         echo '-1000' | sudo tee /proc/54574/oom_score_adj
OK

nginx.conf

server {
    listen        80;
    server_name gerrit.example.com;
    rewrite ^(.*)$ https://${server_name}$1 permanent;

}

server {
    listen       443 ssl;
    server_name  gerrit.example.com;
    access_log logs/gerrit_access.log main;
    error_log  logs/gerrit_error.log;
 
     ssl on;
     #从腾讯云获取到的第一个文件的全路径
     ssl_certificate /usr/local/nginx/ssl/Nginx/1_example.com_bundle.crt;
     #从腾讯云获取到的第二个文件的全路径
     ssl_certificate_key /usr/local/nginx/ssl/Nginx/2_example.com.key;
     ssl_session_timeout 5m;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
     ssl_prefer_server_ciphers on;

    location / {
        auth_basic "Gerrit Code Review";
        auth_basic_user_file /usr/local/nginx/passwd/passwd.test;
        proxy_pass  http://192.168.174.249:18080;

        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_request_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;

   }
}

访问dashboard

部署gerrit 3.7.0_git_02

部署gerrit 3.7.0_nginx_03

部署gerrit 3.7.0_git_04

参考文档

https://gerrit-documentation.storage.googleapis.com/Documentation/3.7.0/index.html