Files
quartz-research-note/content/Gitea Actionsとrsync、Caddyで静的サイトをホストする.md
T
tomoyanonymous 622433ff72
Build / build (push) Successful in 20m24s
update contents
2026-11-19 22:28:24 +09:00

164 lines
6.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
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]]を使ってなんとかすることにした。
[[Cloudflare Pages]]でいいならそっちの方がBranch Previewもできるので多分圧倒的に楽。
## 下準備
鍵をssh-keygenで作っておく。
```sh
ssh-keygen -t ed25519
```
秘密鍵をGitea側のSecretsに置く。公開鍵は後でcompose.ymlに書き込む
## Caddyサーバー側の構成
元々は独自のSSHでrsyncのファイルを受け取りcaddyで公開するだけのイメージを作っていたが、既存イメージでやりくりするように変更した。Caddyファイルもcompose.ymlに埋め込んでしまう。
```yml
services:
ssh:
# 以前は ubuntu に sshd と rsync を入れた独自イメージを build していたが、
# Portainer の Edge 環境では build できないので既製イメージにした。
image: lscr.io/linuxserver/openssh-server:latest
restart: unless-stopped
environment:
- PUID=1000
- PGID=1000
- USER_NAME=rsync
# CI (act_runner) の秘密鍵 PRIVATE_KEY に対応する公開鍵
- PUBLIC_KEY=ssh-ed25519 XXXXXXXXXXXXXXXXXXXXXXXXX
- DOCKER_MODS=linuxserver/mods:openssh-server-rsync
configs:
- source: www-owner
target: /custom-cont-init.d/10-www-owner
mode: 0755
volumes:
- docs:/var/www/html
ports:
- '25223:2222'
server:
image: caddy:latest
restart: unless-stopped
configs:
- source: caddyfile
target: /etc/caddy/Caddyfile
volumes:
- docs:/www/html:ro
ports:
- "8081:80"
volumes:
docs:
configs:
# 新しいボリュームは root 所有で作られるので、rsync ユーザーが書けるように起動時に持ち主を変える
www-owner:
content: |
#!/usr/bin/with-contenv sh
chown "$$PUID:$$PGID" /var/www/html
caddyfile:
content: |
: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 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 :rsync
で、リポジトリに`/.gitea/workflows/ci.yaml`を作る。[Gitea Actionsの`ubuntu-latest`ラベルのランナーはデフォルトでDebianベースの`node:16-bullseye`のDockerイメージのエイリアス](https://docs.gitea.com/usage/actions/act-runner#labels)で(これまじでややこしい)、QuartzはNode18以降でないと動かない。
GIteaの難点として、Github Actionsではデフォルトで使える`hashFiles()`関数が使えない。代わりに、`go-hashfiles`というアクションを使う必要がある。QuartzはGo非依存になったのだが、これがあるのでビルド側でもまだ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/[email protected]
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が手軽にできればいいんだけどなあ