Files
marcus-web/scripts/remote_publish.sh

137 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
# Remote publish script for marcus-web
# Pushes local changes if needed, then builds on SDF
set -e
SDF_HOST="mnw@sdf.org"
REMOTE_DIR="~/marcus-web"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}=== Remote Publish ===${NC}"
echo ""
# Check if we're in a git repo
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo -e "${RED}Error: Not in a git repository${NC}"
exit 1
fi
# Fetch latest from remote to compare
echo "Checking repository status..."
git fetch origin 2>/dev/null || true
# Check for uncommitted changes (staged or unstaged)
if ! git diff-index --quiet HEAD -- 2>/dev/null || [ -n "$(git ls-files --others --exclude-standard)" ]; then
echo -e "${YELLOW}You have uncommitted changes:${NC}"
git status --short
echo ""
read -p "Would you like to commit these changes? (y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Stage all changes
git add -A
read -p "Custom commit message? (y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
read -p "Enter commit message: " COMMIT_MSG
else
# Try to find a post title from recently modified markdown files
COMMIT_MSG=""
RECENT_MD=$(git diff --cached --name-only | grep '\.md$' | head -1)
if [ -n "$RECENT_MD" ] && [ -f "$RECENT_MD" ]; then
# Extract title from frontmatter
TITLE=$(grep -m1 "^title:" "$RECENT_MD" 2>/dev/null | sed "s/^title:[[:space:]]*['\"]*//" | sed "s/['\"].*$//")
if [ -n "$TITLE" ]; then
COMMIT_MSG="Remote Publish Auto Commit - $TITLE"
fi
fi
# Fallback to date-based message
if [ -z "$COMMIT_MSG" ]; then
COMMIT_MSG="Auto Commit - $(date '+%Y-%m-%d %H:%M')"
fi
echo "Using commit message: $COMMIT_MSG"
fi
git commit -m "$COMMIT_MSG"
echo -e "${GREEN}Changes committed${NC}"
echo ""
else
echo -e "${YELLOW}Continuing without committing...${NC}"
read -p "Are you sure? Remote will not have these changes. (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 1
fi
fi
fi
# Check if local is ahead of remote
LOCAL=$(git rev-parse HEAD 2>/dev/null)
REMOTE=$(git rev-parse @{u} 2>/dev/null || echo "")
BASE=$(git merge-base HEAD @{u} 2>/dev/null || echo "")
if [ -z "$REMOTE" ]; then
echo -e "${YELLOW}Warning: No upstream branch configured${NC}"
elif [ "$LOCAL" != "$REMOTE" ]; then
if [ "$LOCAL" = "$BASE" ]; then
echo -e "${YELLOW}Local is behind remote. You may want to pull first.${NC}"
elif [ "$REMOTE" = "$BASE" ]; then
echo -e "${YELLOW}Local commits not pushed to remote:${NC}"
git log --oneline @{u}..HEAD
echo ""
read -p "Push changes before publishing? (y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Pushing..."
git push
echo -e "${GREEN}Pushed successfully${NC}"
else
echo -e "${YELLOW}Skipping push. Remote will not have latest changes.${NC}"
read -p "Continue anyway? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 1
fi
fi
else
echo -e "${YELLOW}Warning: Local and remote have diverged${NC}"
read -p "Continue anyway? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 1
fi
fi
else
echo -e "${GREEN}Repository is up to date with remote${NC}"
fi
echo ""
echo -e "${GREEN}=== Connecting to SDF ===${NC}"
echo ""
# Build the remote command
REMOTE_CMD="cd $REMOTE_DIR && git pull && hugo && mkhomepg -p"
# Try SSH with key auth first, fall back to password prompt
if ssh -o BatchMode=yes -o ConnectTimeout=5 "$SDF_HOST" "echo 'SSH key auth successful'" 2>/dev/null; then
echo "Using SSH key authentication..."
ssh "$SDF_HOST" "$REMOTE_CMD"
else
echo "SSH key auth failed or not configured, using password..."
ssh "$SDF_HOST" "$REMOTE_CMD"
fi
echo ""
echo -e "${GREEN}=== Published successfully! ===${NC}"
echo "Site is live at: https://mnw.sdf.org/"