GitLabのリポジトリに表示されるGit URL(Clone URL)を変更する方法

4 min

構築したGitlabのリポジトリURLが正しくない

GitってCloneするときURLをコピーしてそのままgit clone http://hogehoge/~~/fugafuga.git でリポジトリをクローンしてくると思いますが、サーバ上に構築したGitlabは正しく自身のURLが反映されていませんでした。

最初はハッシュ値のようなものが埋め込まれていたのですが、とりあえずlocalhostに変更して撮影してみました。

このlocalhostを192.168.xxx.xxx:xxxxxという形にしてローカルネットワーク上のPCから簡単にクローンできるように変更します。

Clone URLを変更する方法

公式のChanging gitlab.yml and application.yml settingsによると、/etc/gitlab/gitlab.rbを基にgitlab.ymlが自動生成されるため、gitlab.ymlを直接いじるのは間違いとのこと。編集したい場合はgitlab.rbを編集しましょう。

gitlab.ymlのgitlab.hostの値を変更します。

/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

# This file is managed by gitlab-ctl. Manual changes will be
# erased! To change the contents below, edit /etc/gitlab/gitlab.rb
# and run `sudo gitlab-ctl reconfigure`.

production: &base
#
# 1. GitLab app settings
# ==========================

## GitLab settings
gitlab:
## Web server settings (note: host is the FQDN, do not include http://)
  host: b39f7126756e
  port: 80
  https: false

b39f7126756eをここでは、192.168.111.2:10080に変更します。

変更後

## GitLab settings
gitlab:
## Web server settings (note: host is the FQDN, do not include http://)
  host: 192.168.111.2:10080
  port: 80
  https: false

これでgitlabを再起動すればOKです。

$ sudo gitlab-ctl restart

失敗談:external_urlを変更する

gitlab.rbのexternal_urlを変更する方法もありますが、今回のようなローカルIP:ポートに変更の場合向いていない事がわかりました。

/etc/gitlab/gitlab.rb
external_url ‘http://xxx.xx.jp:10080’

## GitLab configuration settings
##! This file is generated during initial installation and **is not** modified
##! during upgrades.
##! Check out the latest version of this file to know about the different
##! settings that can be configured by this file, which may be found at:
##! https://gitlab.com/gitlab-org/omnibus-gitlab/raw/master/files/gitlab-config-template/gitlab.rb.template

## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
# external_url 'GENERATED_EXTERNAL_URL'

external_url ‘http://192.168.111.2:10080’

変更後はリコンフィグしてください。

$ gitlab-ctl reconfigure

この方法ポート指定するとリッスンポートとずれてアクセスできなくなります。。。(怒)

ポートを変更しないでみるとClone URLが変更されているのが確認できるのですが、いかんせんポートを指定するとアクセスできなくなってしまうので、この方法は今回は向きませんでした。

gitlab.rbを編集する

上述のとおりどうやらこっちで設定するのがベターみたい。

失敗談でも書いたようにexternal_urlに指定したポートと実際に起動しているポートがずれるとアクセスできなくなってしまうので、10080 -> 8080 へポートフォワーディングするのではなく、gitlab自体のポートを10080に変更してから下記作業を行いました。

gitlab.ymlとgitlab.rbについては公式のChanging gitlab.yml and application.yml settingsに詳細が書いてありますので、気になる方はどうぞ。

  • /etc/gitlab/gitlab.rb
## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
external_url 'http://192.168.200.50:10080'

~~~ omitted ~~~

################################################################################
################################################################################
##                Configuration Settings for GitLab CE and EE                 ##
################################################################################
################################################################################

################################################################################
## gitlab.yml configuration
##! Docs: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/gitlab.yml.md
################################################################################
# gitlab_rails['gitlab_ssh_host'] = 'ssh.host_example.com'
# gitlab_rails['time_zone'] = 'UTC'
gitlab_rails['gitlab_host'] = '192.168.200.50:10080'

上記のように2つ設定しました。

  • external_url ‘http://192.168.200.50:10080’
  • gitlab_rails[‘gitlab_host’] = ‘192.168.200.50:10080’

編集が完了したら保存して下記コマンドで設定を反映させます。

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

これで再起動しても設定がもとに戻ることはなくなりました!