オンプレGitLabでgit+CI+npmサーバー2024 の履歴(No.1)

更新


プログラミング/svelte

自前サーバーで GitLab を動かしてプライベートなプロジェクトを置きたい

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 を動かすのは非常に簡単でした。

$ 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:
      - '8929: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:8929/gitlab
    ProxyPassReverse /gitlab http://localhost:8929/gitlab
</IfModule>

と設定して、

LANG:console
$ sudo a2enmod proxy
$ sudo service apache2 reload

結構重要なのが AllowEncodedSlashes NoDecode だ。

これをしておかないと GitLab を npm サーバーとして使う際に、パッケージリポジトリへの PUT が 404 で失敗する。

https://stackoverflow.com/questions/20496963/avoid-nginx-decoding-query-parameters-on-proxy-pass-equivalent-to-allowencodeds

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 で落ちるていたらこの設定を見直すといい。

root でログインする

初期設定で root という管理者ユーザーが登録されているので、 ウェブサーバーから https://myserver.com/gitlab へアクセスして root でログインする。

管理者 root の初期パスワードは gitlab の docker-compose.yml を作ったフォルダの下の etc フォルダを調べるとわかる。

LANG:console
$ sudo cat etc/initial_root_password 

ユーザーを登録する

https://myserver.com/gitlab/admin/users

ここでユーザーを登録できる。

と、その前にメールを送れるように設定する。

メールを送れるように設定する

ホストで 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 のパスワードを忘れてしまった場合

https://blog.k-bushi.com/post/tech/environment/construct-gitlab-runner-docker/#gitlab%E3%81%B8%E3%81%AE%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3

にあるように、

$ 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 で動かす


Counter: 49 (from 2010/06/03), today: 19, yesterday: 30