Table of Contents
1 Gerritをインストールする
個人の開発でJenkinsと連携するだけの為に使う場合は認証方法は DEVELOPMENT_BECOME_ANY_ACCOUNTのままで良いでしょう。
以下のスクリプトはGerritをインストールします。
#!/bin/sh -e# Access to http://<hostname>:8080/.cat <<EOF | sudo tee /etc/apt/sources.list.d/gerritforge.listdeb [trusted=yes] http://deb.gerritforge.com/ gerrit contribEOFsudo apt update -ysudo apt install -y openjdk-8-jdk havegedsudo update-java-alternatives -s java-1.8.0-openjdk-amd64sudo apt install -y gerritsudo systemctl enable gerritsudo reboot
以下のURLにアクセスします。
http://<hostname>:8080
初期ページが表示されます。
DEVELOPMENT_BECOME_ANY_ACCOUNTではどのユーザにもなることができます。
git cloneはHTTP経由でもSSH経由でも実行できます。
$ git clone http://<hostname>:8080/<project>.git$ git clone ssh://<hostname>:49128/<project>.git
2 認証方法をHTTPに変更する
クローズドな環境、少人数の開発体制、LDAPが用意されていない場合は、認証方法をHTTPにしても良いでしょう。
ApacheのDigest認証のユーザ名とパスワードがGerritのユーザ名とパスワードとなります。
以下のスクリプトはインストールしたGerritの認証方法をHTTPに変更して、 Apacheをセットアップします。
#!/bin/sh -eGERRIT_ADMIN_PASSWD=gerritPORT=8080FQDN=$(hostname -f)# Make gerrit to accept only http://localhost:8080/ but to recognize# http://<hostname>/ as web site URL.sudo cp /etc/gerrit/gerrit.config /etc/gerrit/gerrit.config.origsudo sed -i /etc/gerrit/gerrit.config -e “s;type = DEVELOPMENT_BECOME_ANY_ACCOUNT;type = HTTP;g” -e “s;canonicalWebUrl = .*;canonicalWebUrl = http://${FQDN}/;g” -e “s;listenUrl = .*;listenUrl = proxy-http://localhost:${PORT}/;g” -e “s;firstTimeRedirectUrl = (.*);;g”# If gerrit.service is enabled and still not complete to start, wait it.while [ “$(systemctl is-active gerrit.service)x” = “activatingx” ]; do sleep 1donesudo systemctl restart gerrit# Make apache to map http://<hostname>/ to http://localhost:8080/.sudo apt install -y apache2sudo systemctl enable apache2cat <<EOF | sudo tee /etc/apache2/sites-available/gerrit.confProxyPass / http://localhost:${PORT}/ nocanonProxyPassReverse / http://localhost:${PORT}/ nocanonProxyRequests Off<Proxy http://localhost:${PORT}/> Order deny,allow Allow from all</Proxy><Location /> AuthType Digest AuthName “gerrit” AuthUserFile /etc/apache2/.htdigest Require valid-user</Location>EOFsudo a2enmod proxy_httpsudo a2enmod auth_digestsudo a2ensite gerritsudo systemctl restart apache2# Add gerrit user. You need to add htdigest for each user.sudo apt install -y expectexpect -c “set timeout -1spawn sudo htdigest -c /etc/apache2/.htdigest gerrit adminexpect “New password: “send “${GERRIT_ADMIN_PASSWD}n”expect “Re-type new password: “send “${GERRIT_ADMIN_PASSWD}n”expect eof”expect -c “set timeout -1spawn sudo htdigest /etc/apache2/.htdigest gerrit “${USER}”expect “New password: “send “${GERRIT_ADMIN_PASSWD}n”expect “Re-type new password: “send “${GERRIT_ADMIN_PASSWD}n”expect eof”
以下のURLにアクセスします。
http://<hostname>
Digest認証のダイアログが表示されます。
adminユーザでも”Switch Account”がありません。
git cloneはHTTP経由でもSSH経由でも実行できます。 HTTP経由の場合はDigest認証のユーザ名とパスワードが必要になります。
$ git clone http://<hostname>/gerrit/<project>.git$ git clone ssh://<hostname>:49128/<project>.git
新規にユーザを追加する場合はhtdigestでエントリを追加する必要があります。
$ sudo htdigest /etc/apache2/.htdigest “gerrit” ${USER}Adding user hiroom2 in realm gerritNew password:Re-type new password:
追加したユーザでDigest認証を実施します。
ユーザのプロファイルページが表示されます。
git-reviewやSSH経由のgit cloneで使うSSH公開鍵を登録します。 emailは~/.gitconfigに記載されたものを登録します。