--- date: "2024-02-05T12:26:59+0900" --- #self-hosted #server #tips Giteaは最近Actionが使えるようになった。Dockerで別にランナーマシンを立ち上げる必要があるほか、GitHub Actionsと比べると、Concurrencyなどちゃんと効かなかったり色々まだ機能が足りてない所はある。 ただ、公開されてるGithubs Actionはそのままuseできるのが強い。`actions/cache@v3`の代わりに`https://github.com/actions/cache@v3`のようにURLで指定できる。よく使うアクションはgitea側にもミラーされていることもあるが、なんか放置されてるやつも多々あるので注意。 Pagesの機能がないため、別途サーバーを立ち上げてrsyncで同期するくらいしか方法がない。そこそこ設定は面倒くさいが、nginxよりさらに簡易的に動かせるgo製のWebサーバーCaddyを使ってなんとかすることにした。 ## 下準備 鍵をssh-keygenで作っておく。(秘密鍵をGitea側のSecretsに置くので新しく生成しないとだめ) ```sh ssh-keygen -t ed25519 ``` ファイル名は`rsync_webserver`と`rsync_webserver.pub`とかにしておく ## Caddyサーバー側の構成 [tomoyanonymous/Caddy\_rsync\_webserver - Caddy\_rsync\_webserver - Tomoya Matsuura Gitea](https://git.matsuuratomoya.com/tomoyanonymous/Caddy_rsync_webserver) ### フォルダ構成 ```sh Dockerfile docker-compose.yml Caddyfile docs/ #空フォルダ rsync_webserver.pub #鍵をコピーしておく ``` rsyncでファイルを受け取る用のDockerイメージを作る ```Dockerfile FROM ubuntu:latest RUN apt-get update && apt-get install -y openssh-server rsync RUN mkdir /var/run/sshd RUN sed -i 's/#\?SyslogFacility AUTH/SyslogFacility AUTH/' /etc/ssh/sshd_config RUN sed -i 's/#\?LogLevel INFO/LogLevel Info/' /etc/ssh/sshd_config RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd ENV NOTVISIBLE "in users profile" RUN echo "export VISIBLE=now" >> /etc/profile COPY rsync_webserver.pub /root/authorized_keys RUN mkdir ~/.ssh && \ mv ~/authorized_keys ~/.ssh/authorized_keys && \ chmod 0600 ~/.ssh/authorized_keys && \ chmod 0700 ~/.ssh EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"] ``` パーミッションは必ず`~/.ssh/authorized_keys`が600、`~/.ssh`が700でないとssh接続できないので注意 `docker-compose.yml`でCaddyとの連携設定。共通のボリュームでhtmlを管理するが、今回はそのフォルダを`docs`という名前で作った。Caddyのイメージはそのまま使う。 ```yml version: '3' services: ssh: build: . volumes: - ./docs:/var/www/html ports: - '25222:22' server: image: caddy:latest volumes: - ./Caddyfile:/etc/caddy/Caddyfile - ./docs:/www/html ports: - "8080:80" ``` Caddyファイルは最小限。 ```Caddyfile :80 { root * /www/html file_server try_files {path}.html {path} handle_errors { rewrite * /404.html file_server } } ``` この辺参考。[Serving static files using Caddy](https://thedevelopercafe.com/articles/serving-static-files-using-caddy-8513e8f36e46)。Quartzでは`/記事名.html`に`example.com/記事名`でアクセスする必要があるので`try_files`の行が必要。 `docs`内に適当な`index.html`を置いて`docker-compose build`、`docker-compose up -d`で立ち上げ、`DockerホストのIP:8080`で繋ぐとアクセスできる。 これをそのまま[[Cloudflare Tunnel]]で公開している。 ## Gitea側の設定 Action RunnerつきでGiteaを立ち上げるところまでは省略。 ほぼこのサイトの例を借りる。[[Hugo]]じゃなく[[Quartz]]なので色々違うけど [CI/CD pipeline for a Hugo-built static page using Gitea Actions on a selfhosted Gitea instance · Julius Röttgermann | DevOps related Blog and Tutorials](https://julius-roettgermann.de/post/hugo-cicd/) リポジトリのSettings -> Actions -> Secretsに`PRIVATE_KEY`という名前で`rsync_webserver`の中身を貼り付ける。 同じくVariablesの方には以下を設定 - WEBSERVER_HOST (Caddyが立ち上がっているサーバーのIP) - WEBSERVER_PORT (Caddyが立ち上がっているサーバーのSSH用ポート、上の例では25222) - WEBSERVER_USER :root(これあんまり良くないのかな) で、リポジトリに`/.gitea/workflows/ci.yaml`を作る。[Gitea Actionsの`ubuntu-latest`ラベルのランナーはデフォルトでDebianベースの`node:16-bullseye`のDockerイメージのエイリアス](https://docs.gitea.com/usage/actions/act-runner#labels)で(これまじでややこしい)、QuartzはNode18以降でないと動かない。またGoも入ってないので入れる。 ```yml name: Build on: push: branches: - v4 jobs: build: env: RUNNER_TOOL_CACHE: /toolcache #これないとキャッシュが効かない runs-on: ubuntu-latest steps: - run: git config --global core.quotepath false - name: Install apt packages run: apt update && apt install -y rsync - uses: actions/checkout@v3 with: fetch-depth: 0 - uses: https://github.com/actions/setup-node@v4 with: node-version: 'latest' - uses: https://github.com/actions/setup-go@v4 with: go-version: '^1.20' - uses: https://gitea.com/actions/go-hashfiles@v0.0.1 id: get-hash with: patterns: |- **/package-lock.json - name: Cache dependencies uses: actions/cache@v3 with: path: ~/.npm key: ${{ runner.os }}-node-${{ steps.get-hash.outputs.hash }} restore-keys: | ${{ runner.os }}-node- - run: npm i - run: npx quartz build - name: Create private key run: | echo "${{ secrets.PRIVATE_KEY }}" > /tmp/act_runner_key chmod 600 /tmp/act_runner_key - name: rsync public directory run: | rsync -avz --delete -e "ssh -i /tmp/act_runner_key -o StrictHostKeyChecking=no -p ${{ vars.WEBSERVER_PORT }}" ${{ gitea.workspace }}/public/* ${{ vars.WEBSERVER_USER }}@${{ vars.WEBSERVER_HOST }}:/var/www/html ``` できてますねえ! ![[img/gitea_action_ss.png]] Branch Previewが手軽にできればいいんだけどなあ