オンプレGitLabでgit+CI+npmサーバー2024 の履歴(No.2)
更新自前サーバーで GitLab を動かしてプライベートな npm プロジェクトなどを置きたい†
GitLab は、
- GitHub のように使える git 利用開発支援機能
- テストやビルドやデプロイを自動化する CI サーバー機能
- npm パッケージサーバー機能
など、さまざまな機能を兼ね備えるフリーソフト。
インストールすれば自前のサーバーで動かせる。
GitLab を動かすには 4GB 以上のメモリが必要で、しばらく前だと二の足を踏んでいたのだけれど今なら軽いはず。
Docker を使ってサクッと動かす。(動かせるといいな)
目次†
GitLab サーバーを docker compose で動かす†
https://gitlab-docs.creationline.com/ee/install/docker.html#install-gitlab-using-docker-compose
を見ながら docker イメージで動作するよう設定する。
- サーバーにはすでに docker 環境があって、自分が管理者になっている
- サーバーには apache2 が https://myserver.com/ への通信を受け付ける状況になっている
- gitlab は https://myserver.com/gitlab/ というアドレスで動かす
という条件で GitLab を動かします。。
LANG:console $ cd ~ $ mkdir gitlab $ cd gitlab $ echo GITLAB_HOME=`pwd` > .env $ cat <<"EOS" > docker-compose.yml services: web: image: 'gitlab/gitlab-ce:latest' restart: always hostname: 'myserver.com' environment: GITLAB_OMNIBUS_CONFIG: | external_url 'https://myserver.com/gitlab' gitlab_rails['gitlab_shell_ssh_port'] = 2224 ports: - '8980:80' volumes: - '$GITLAB_HOME/etc:/etc/gitlab' - '$GITLAB_HOME/log:/var/log/gitlab' - '$GITLAB_HOME/data:/var/opt/gitlab' shm_size: '256m' EOS $ docker compose up -d [+] Running 10/10 ✔ web Pulled 58.2s ✔ 857cc8cb19c0 Pull complete 2.1s ✔ 28812802a434 Pull complete 2.1s ✔ 54e2e989e54c Pull complete 3.0s ✔ abb7892b26dc Pull complete 3.0s ✔ e9d667f5a8c1 Pull complete 3.0s ✔ a8891519352d Pull complete 3.1s ✔ 8b624a00a604 Pull complete 3.1s ✔ 0cf3370d74b6 Pull complete 3.1s ✔ 3253094bd895 Pull complete 55.9s [+] Running 2/2 ✔ Network gitlab_default Created 0.0s ✔ Container gitlab-web-1 Started 0.7s
これでもう立ち上がっている。
ホストマシンの /etc/apache2/sites-enabled/default-ssl.conf に
<IfModule mod_proxy.c> ProxyRequests Off <Proxy *> Require all granted </Proxy> AllowEncodedSlashes NoDecode <Location /gitlab> Order deny,allow Allow from all </Location> RequestHeader set X_FORWARDED_PROTO 'https' ProxyPass /gitlab http://localhost:8980/gitlab ProxyPassReverse /gitlab http://localhost:8980/gitlab </IfModule>
と設定して、
LANG:console $ sudo a2enmod proxy $ sudo service apache2 reload
とすることで apache2 がプロキシサーバーとして振る舞い、 https://myserver.com/gitlab 以下へのアクセスを http://localhost:8929/gitlab へ転送してくれる。
結構重要なのが AllowEncodedSlashes NoDecode だ。
これをしておかないと GitLab を npm サーバーとして使う際に、パッケージリポジトリへの PUT が 404 で失敗する。
LANG: console $ npm publish ... npm error code E404 npm error 404 Not Found - PUT https://myserver.com/gitlab/api/v4/projects/1/packages/npm/@myscope%2fmypackage npm error 404 npm error 404 '@myscope/mypackage@0.0.1' is not in this registry. npm error 404 npm error 404 Note that you can also install from a npm error 404 tarball, folder, http url, or git url.
のように、npm publish で認証は通過しているのに、そして、アップロードしようとしているのに 404 で落ちるていたらこの設定を見直すといい。
メールを送れるように設定する†
ホストで exim4 が動いていて、正しくメールが送れているとする。
LANG:console $ echo "MAIN_RELAY_NETS = 172.16.0.0/12" | sudo tee -a exim4.conf.localmacros $ sudo update-exim4.conf $ sudo systemctl restart exim4
とすることで、172.16.???.??? からのメール転送を許可する。
docker-compose.yml には
extra_hosts: - "host.docker.internal:host-gateway"
を追加して、コンテナ内から host.docker.internal という名前でホストマシンへ到達できるようにした。
etc/gitlab.rb には、
gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "host.docker.internal" gitlab_rails['smtp_port'] = 25
を設定した。
root でログインする†
初期設定で root という管理者ユーザーが登録されているので、 ウェブサーバーから https://myserver.com/gitlab へアクセスして root でログインする。
管理者 root の初期パスワードは gitlab の docker-compose.yml を作ったフォルダの下の etc フォルダを調べるとわかる。
LANG:console $ sudo cat etc/initial_root_password
root のパスワードを忘れてしまった場合†
にあるように、
LANG:console $ docker compose exec web gitlab-rake "gitlab:password:reset" Enter username: root Enter password: Confirm password: Password successfully updated for user with username root.
とすればパスワードを設定しなおせるらしい。
runner も docker で動かす†
同時に使用する人数と比較するとこのサーバーには余裕があるので、 CI サーバーも同じホストで動かしてしまう。
先の docker-compose.yml に以下を追加する。
runner: image: 'gitlab/gitlab-runner:latest' restart: unless-stopped volumes: - '$GITLAB_HOME/runner:/etc/gitlab-runner' - /var/run/docker.sock:/var/run/docker.sock ports: - '8093:8093'
そして、
LANG:console $ sudo docker compose up -d
runner を GitLab に追加する†
https://myserver.com/gitlab/admin/runners
から