diff --git a/.github/workflows/reusable-release-frontend.yaml b/.github/workflows/reusable-release-frontend.yaml index d91e235..0998f45 100644 --- a/.github/workflows/reusable-release-frontend.yaml +++ b/.github/workflows/reusable-release-frontend.yaml @@ -4,6 +4,9 @@ # Node:请在 Runner 上预装 Node 20+(勿用 actions/setup-node,避免每次从 GitHub 拉包)。 # npm:通过 npm_registry 输入使用国内镜像(默认 npmmirror)。 # Checkout:不用 actions/checkout(host 模式下 JS Action 仍会进无 Node 的容器);改用 git + github.token。 +# 重要:act_runner 会把 run: 脚本写到 $GITHUB_WORKSPACE/workflow/*.sh, +# 所以 checkout 绝对不能在 workspace 根上 rm -rf ./*,否则会把自己这个脚本一起删了。 +# 解决办法:克隆到 $GITHUB_WORKSPACE/_src 子目录,后续 step 都在该子目录操作。 # 浅拉不要用「裸 SHA」作 fetch 参数(Gitea 上易失败);按分支 clone 再对齐 SHA。 name: Reusable Frontend Deploy @@ -42,10 +45,13 @@ jobs: defaults: run: shell: bash + working-directory: ${{ github.workspace }}/_src env: NPM_CONFIG_REGISTRY: ${{ inputs.npm_registry }} steps: - name: Checkout + # 这一步在 workspace 根执行,不能用默认的 _src(此时还不存在) + working-directory: ${{ github.workspace }} env: GITEA_HOST: git.jc2009.com GITEA_TOKEN: ${{ github.token }} @@ -59,23 +65,25 @@ jobs: TOKEN="${GITEA_TOKEN:-${GITHUB_TOKEN:-}}" test -n "$TOKEN" || { echo "Checkout: empty token (github.token not available to this job)"; exit 1; } URL="https://x-access-token:${TOKEN}@${GITEA_HOST}/${REPO}.git" - WS="${GITHUB_WORKSPACE:-$(pwd)}" - cd "$WS" - shopt -s dotglob nullglob || true - rm -rf ./* ./.??* 2>/dev/null || true - git clone --depth 1 --branch "$BRANCH" "$URL" . + + SRC_DIR="${GITHUB_WORKSPACE}/_src" + # 只清理 _src,不动 workspace 根(保护 runner 写入的 workflow/*.sh) + rm -rf "$SRC_DIR" + git clone --depth 1 --branch "$BRANCH" "$URL" "$SRC_DIR" + cd "$SRC_DIR" if [ "$(git rev-parse HEAD)" != "$SHA" ]; then git fetch --depth 200 origin "refs/heads/${BRANCH}" git checkout --force "$SHA" fi + echo "Checked out $(git rev-parse HEAD) to $SRC_DIR" - name: Check Node.js (pre-installed on runner) run: | set -e command -v node >/dev/null || { echo "Runner 上未找到 node,请先安装 Node 20+"; exit 1; } - command -v npm >/dev/null || { echo "Runner 上未找到 npm"; exit 1; } + command -v npm >/dev/null || { echo "Runner 上未找到 npm"; exit 1; } node -v - npm -v + npm -v NODE_MAJOR=$(node -p "process.versions.node.split('.')[0]") test "$NODE_MAJOR" -ge 20 || { echo "需要 Node 20 及以上,当前: $(node -v)"; exit 1; } @@ -97,7 +105,7 @@ jobs: - name: Set ownership for Nginx (Baota www) run: | - case "${{ inputs.chown_www }}" in false|False|"false") exit 0 ;; esac + if [ "${{ inputs.chown_www }}" != "true" ]; then exit 0; fi DEST="${{ inputs.deploy_path }}" DEST="${DEST%/}" - chown -R www:www "$DEST" || chown -R nginx:nginx "$DEST" || true + chown -R www:www "$DEST" || chown -R nginx:nginx "$DEST" || true \ No newline at end of file