Skip to content

Instantly share code, notes, and snippets.

@marobo
Created May 15, 2025 00:00
Show Gist options
  • Save marobo/e2f7789e3e83a4bcb23c10590bc393ef to your computer and use it in GitHub Desktop.
Save marobo/e2f7789e3e83a4bcb23c10590bc393ef to your computer and use it in GitHub Desktop.
deploy_pythonanywhere.sh
#!/bin/bash
# === CONFIGURATION ===
GIT_REPO="[email protected]:marobo/house_estimator.git" # Git repository URL
GIT_BRANCH="dev" # Git branch
PROJECT_FOLDER="house_estimator" # Folder name after cloning
PROJECT_NAME="house_estimator" # Django project folder containing settings.py
PYTHON_VERSION="3.10" # Python version
PA_USERNAME="housestimator" # PythonAnywhere username
DOMAIN="housestimator.pythonanywhere.com" # PythonAnywhere domain
# === DEPLOY SCRIPT ===
echo "πŸ“ Navigating to home directory..."
cd ~ || exit
# === Generate SSH Key if not present ===
if [ ! -f ~/.ssh/id_rsa.pub ]; then
echo "πŸ”‘ Generating new SSH key..."
mkdir -p ~/.ssh
fi
ssh-keygen -t rsa -b 4096 -C "$PA_USERNAME@pythonanywhere" -N "" -f ~/.ssh/id_rsa
echo "πŸ—οΈ Public key generated:"
cat ~/.ssh/id_rsa.pub
else
echo "πŸ”‘ SSH key already exists."
fi
if [ ! -d "$PROJECT_FOLDER" ]; then
echo "πŸ” Cloning the Git repo..."
git clone "$GIT_REPO"
cd "$PROJECT_FOLDER" || exit
echo "πŸ“₯ Checking out branch $GIT_BRANCH..."
git checkout $GIT_BRANCH
else
cd "$PROJECT_FOLDER" || exit
echo "πŸ“₯ Checking out branch $GIT_BRANCH..."
git checkout $GIT_BRANCH
echo "πŸ“₯ Pulling latest changes from Git..."
git pull origin $GIT_BRANCH
cd ..
fi
cd "$PROJECT_FOLDER" || exit
# === Create and activate virtual environment ===
if [ ! -d "venv" ]; then
echo "🐍 Creating virtual environment..."
python$PYTHON_VERSION -m venv venv
fi
echo "βœ… Activating virtual environment..."
source venv/bin/activate
if [ -f "requirements.txt" ]; then
echo "πŸ“¦ Installing requirements..."
pip install --upgrade pip
pip install -r requirements.txt
fi
echo "🧱 Running migrations..."
python manage.py migrate
if [ -d "static" ]; then
echo "πŸ–ΌοΈ Collecting static files..."
python manage.py collectstatic --noinput
fi
# === Configure WSGI file ===
WSGI_FILE="/var/www/${PA_USERNAME}_pythonanywhere_com_wsgi.py"
echo "βš™οΈ Updating WSGI file at $WSGI_FILE..."
cat > "$WSGI_FILE" <<EOL
import sys
import os
path = '/home/$PA_USERNAME/$PROJECT_FOLDER'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = '$PROJECT_NAME.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
EOL
# === Reload web app ===
echo "πŸ”„ Reloading web app..."
touch /var/www/${PA_USERNAME}_pythonanywhere_com_wsgi.py
echo "βœ… Deployment complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment