Files
userbase/.gitea/workflows/caddy-switch.yml
T

63 lines
3.2 KiB
YAML
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.
name: switch-caddy-to-fileserver
# 一次性:把 Caddy 对 /userbase* 的反代改为 file_server 直接读取软链接目录
# 触发:Gitea 网页 Actions -> 本工作流 -> Run workflow(仅执行一次)
# 做了什么:
# 1) 确保 /data/www/userbase/releases 与初始 front 软链接存在(caddy 启动前 root 必须存在)
# 2) 备份并原地修改宿主 Caddyfile:仅替换唯一行 `reverse_proxy userbase-front:80`
# -> root * /data/www/userbase/front + file_server + try_files(SPA 回退)
# 3) 按现有 caddy 容器的 image/端口/挂载/网络 重建(追加 /data/www/userbase 挂载)
# 4) 移除已被取代的 userbase-front 容器
# 回滚:/opt/docker/infra/Caddyfile.bak-* 备份 + docker rm -f caddy 后用旧参数重建。
on:
workflow_dispatch:
jobs:
switch:
runs-on: ubuntu-latest
container:
image: docker:24
steps:
- name: Recreate caddy (file_server + mount /data/www/userbase)
run: |
set -e
apk add --no-cache jq
# 0) 前端目录与初始软链接(避免 caddy 因 root 不存在而启动失败)
docker run --rm -v /data/www/userbase:/data/www/userbase alpine sh -c '
mkdir -p /data/www/userbase/releases
if [ ! -e /data/www/userbase/front ]; then
mkdir -p /data/www/userbase/releases/initial
ln -sfn /data/www/userbase/releases/initial /data/www/userbase/front
fi
'
# 1) 备份 + 替换宿主 Caddyfile 中唯一的前后端反代行
docker run --rm -v /opt/docker/infra:/opt/docker/infra alpine sh -c '
cp -f /opt/docker/infra/Caddyfile /opt/docker/infra/Caddyfile.bak-$(date +%Y%m%d-%H%M)
sed -i "s#reverse_proxy userbase-front:80#root * /data/www/userbase/front\n\tfile_server\n\ttry_files {path} /index.html#" /opt/docker/infra/Caddyfile
echo "=== 新的 /userbase 块 ==="
grep -n -A4 "handle_path /userbase" /opt/docker/infra/Caddyfile
'
# 2) 按现有 caddy 容器参数重建(保留原 image/端口/挂载/网络,追加前端目录挂载)
docker inspect caddy > /tmp/c.json
IMG=$(jq -r '.[0].Config.Image' /tmp/c.json)
NET=$(jq -r '.[0].HostConfig.NetworkMode' /tmp/c.json)
PORTS=$(jq -r '.[0].HostConfig.PortBindings | to_entries[]? | "-p \(.value[0].HostPort):\(.key | rtrimstr("/tcp"))"' /tmp/c.json | tr "\n" " ")
BINDS=$(jq -r '.[0].HostConfig.Binds[]? | "-v \(.)"' /tmp/c.json | tr "\n" " ")
CMD=$(jq -r '.[0].Config.Cmd | join(" ")' /tmp/c.json)
echo "recreate caddy: IMG=$IMG NET=$NET PORTS=[$PORTS] BINDS=[$BINDS] CMD=[$CMD]"
docker rm -f caddy 2>/dev/null || true
# shellcheck disable=SC2086
docker run -d --name caddy --restart unless-stopped --network "$NET" $PORTS $BINDS -v /data/www/userbase:/data/www/userbase "$IMG" $CMD
sleep 6
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}' | grep -E 'caddy|userbase' || true
# 3) 旧的 userbase-front 容器已被 Caddy file_server 取代
docker rm -f userbase-front 2>/dev/null || echo "userbase-front already removed"
echo "CADDY SWITCH DONE"