Skip to content

Instantly share code, notes, and snippets.

@SamuelDavis
Last active January 31, 2026 19:52
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() {
REMOTE_ORIGIN=$(git config --get remote.origin.url)
if [ $? -ne 0 ]; then
echo "Git repository not found." >&2
return
fi
REPO_NAME=$(basename -s .git $REMOTE_ORIGIN)
DOMAIN=${1:-"$REPO_NAME.sdavis.online"}
HOST=${2:-'[email protected]:SamuelDavis'}
OUT_DIR="dist"
npm run build -- --outDir "$OUT_DIR"
if [ $? -ne 0 ]; then
echo "Build failed." >&2
return
fi
cd "$OUT_DIR"
echo "$DOMAIN" > CNAME
git init
git add --all
git commit --message 'deploy'
BRANCH=$(git rev-parse --abbrev-ref HEAD)
read -p "Push $BRANCH to gh-pages on $HOST/$REPO_NAME.git? (y/N) " 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