Skip to content

Instantly share code, notes, and snippets.

@SamuelDavis
Last active February 20, 2026 23:17
Show Gist options
  • Select an option

  • Save SamuelDavis/171fc3c1fd2ea000477f9ca0768d728c to your computer and use it in GitHub Desktop.

Select an option

Save SamuelDavis/171fc3c1fd2ea000477f9ca0768d728c to your computer and use it in GitHub Desktop.
Deploy to gh-pages for projects with `npm run build` to `dist/` (e.g. Vite)
function deploy() {
local REMOTE_ORIGIN=$(git config --get remote.origin.url);
if [ $? -ne 0 ]; then
echo "Git repository not found." >&2
return
fi
local REPO_NAME=$(basename -s .git "$REMOTE_ORIGIN")
local DOMAIN=${1:-"$REPO_NAME.sdavis.online"}
local HOST=${2:-'git@github.com:SamuelDavis'}
local OUT_DIR='dist'
rm -rf "$OUT_DIR"
if ! npm run build -- --outDir "$OUT_DIR"; then
return $?
fi
cd "$OUT_DIR"
git init
echo "$DOMAIN" > CNAME
git add --all
if ! git commit --message 'deploy'; then
return $?
fi
local BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "Push $BRANCH to gh-pages on $REMOTE_ORIGIN? (y/N) "
read ANSWER
if [[ "$ANSWER" == "y" || "$ANSWER" == "Y" ]]; then
git push -f "$HOST/$REPO_NAME.git" "${BRANCH}:gh-pages"
fi
cd -
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment