meanas/scripts/publish_docs_branch.sh

52 lines
1.3 KiB
Bash
Executable file

#!/bin/bash
set -Eeuo pipefail
if [[ $# -ne 2 ]]; then
echo "usage: $0 <site-dir> <branch>" >&2
exit 2
fi
SITE_DIR="$1"
BRANCH="$2"
if [[ ! -d "$SITE_DIR" ]]; then
echo "site directory not found: $SITE_DIR" >&2
exit 1
fi
if ! git rev-parse --git-dir >/dev/null 2>&1; then
echo "must be run inside a git repository" >&2
exit 1
fi
TMP_DIR="$(mktemp -d)"
cleanup() {
git worktree remove --force "$TMP_DIR" >/dev/null 2>&1 || true
}
trap cleanup EXIT
git fetch origin "$BRANCH" || true
if git show-ref --verify --quiet "refs/remotes/origin/$BRANCH"; then
git worktree add --detach "$TMP_DIR" "origin/$BRANCH"
else
git worktree add --detach "$TMP_DIR"
git -C "$TMP_DIR" checkout --orphan "$BRANCH"
fi
find "$TMP_DIR" -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} +
cp -R "$SITE_DIR"/. "$TMP_DIR"/
touch "$TMP_DIR/.nojekyll"
git -C "$TMP_DIR" config user.name "${GIT_AUTHOR_NAME:-Forgejo Actions}"
git -C "$TMP_DIR" config user.email "${GIT_AUTHOR_EMAIL:-forgejo-actions@localhost}"
git -C "$TMP_DIR" add -A
if git -C "$TMP_DIR" diff --cached --quiet; then
echo "no docs changes to publish"
exit 0
fi
git -C "$TMP_DIR" commit -m "Publish docs for ${GITHUB_SHA:-local build}"
git -C "$TMP_DIR" push --force origin "HEAD:${BRANCH}"