Last active
February 20, 2026 23:17
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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