Files
MoShiro/ci/shared-web.sh
justme d1ece67a6a
ci/woodpecker/push/web-build Pipeline was successful
ci/woodpecker/push/verify Pipeline was canceled
ci/woodpecker/push/dev-image Pipeline was canceled
Build the frontend once per pipeline and reuse it in the dev backend image [CI]
2026-10-21 15:17:37 +08:00

45 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# 同一条流水线里,前端只构建一次:web-build workflow 构建完把 dist 放到流水线的共享目录,开发镜像的 Backend 步骤直接拿来用。
# 用法:
# shared-web.sh publish 把 web/display/dist 和 web/admin/dist 放进共享目录(先写临时目录,最后用一次 rename 加上 ready 标记)
# shared-web.sh fetch 等 ready 标记出现(最多 MOSHIRO_SHARED_WEB_WAIT 秒,默认 600),再复制回工作区;
# 退出码 0 = 已复制,1 = 没等到或流水线已经失败(调用方自己构建兜底)
# 共享目录和 ci/guard-step.py 用的是同一个:${MOSHIRO_CI_STATE}/${CI_REPO_ID}-${CI_PIPELINE_NUMBER}。
set -euo pipefail
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
state="${MOSHIRO_CI_STATE:-/home/justme/woodpecker/cache/pipeline-state}/${CI_REPO_ID:-0}-${CI_PIPELINE_NUMBER:-0}"
shared="$state/web-dist"
apps=(display admin)
case "${1:-}" in
publish)
mkdir -p "$state"
tmp="$state/web-dist.tmp.$$"
rm -rf "$tmp" "$shared"
for app in "${apps[@]}"; do
[[ -f "$root/web/$app/dist/index.html" ]] || { echo "web/$app/dist is missing" >&2; exit 1; }
mkdir -p "$tmp/$app"
cp -a "$root/web/$app/dist/." "$tmp/$app/"
done
mv "$tmp" "$shared"
;;
fetch)
deadline=$((SECONDS + ${MOSHIRO_SHARED_WEB_WAIT:-600}))
until [[ -f "$shared/display/index.html" && -f "$shared/admin/index.html" ]]; do
if [[ -e "$state/failed" ]]; then echo "the pipeline already failed; no shared web build" >&2; exit 1; fi
if ((SECONDS >= deadline)); then echo "the shared web build did not appear in time" >&2; exit 1; fi
sleep 2
done
for app in "${apps[@]}"; do
rm -rf "$root/web/$app/dist"
mkdir -p "$root/web/$app/dist"
cp -a "$shared/$app/." "$root/web/$app/dist/"
done
;;
*)
echo "usage: shared-web.sh publish|fetch" >&2
exit 2
;;
esac