Created
May 15, 2025 00:00
-
-
Save marobo/e2f7789e3e83a4bcb23c10590bc393ef to your computer and use it in GitHub Desktop.
deploy_pythonanywhere.sh
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
#!/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