stones/deploy.sh

118 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
# Deployment script for Stones Database Next.js application
set -e # Exit immediately if a command exits with a non-zero status
# Configuration
APP_NAME="stones-database"
APP_DIR="/var/www/$APP_NAME"
REPO_URL="git@git.boilerhaus.org:boiler/stones.git" # Updated to correct Gitea repo URL
BRANCH="main" # Or whatever branch you want to deploy
NODE_VERSION="18" # Make sure this matches your development environment
# Colors for pretty output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}Starting deployment of $APP_NAME...${NC}"
# Check if SSH key is set up for git user
if [ ! -f ~/.ssh/id_rsa ]; then
echo -e "${YELLOW}SSH key not found. Make sure your SSH key is set up for git user access.${NC}"
echo -e "${YELLOW}You may need to run: ssh-keygen -t rsa -b 4096 -C 'your_email@example.com'${NC}"
echo -e "${YELLOW}Then add the public key to your Gitea account.${NC}"
exit 1
fi
# Check if directory exists, if not create it
if [ ! -d "$APP_DIR" ]; then
echo -e "${YELLOW}Creating application directory...${NC}"
mkdir -p $APP_DIR
# Clone the repository if it's the first time
echo -e "${YELLOW}Cloning repository...${NC}"
git clone --branch $BRANCH $REPO_URL $APP_DIR
else
echo -e "${YELLOW}Pulling latest changes...${NC}"
cd $APP_DIR
git fetch --all
git reset --hard origin/$BRANCH
fi
cd $APP_DIR
# Make sure we're using the right Node.js version
echo -e "${YELLOW}Using Node.js version $NODE_VERSION...${NC}"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
nvm use $NODE_VERSION || { echo -e "${RED}Failed to switch Node.js version. Make sure NVM is installed.${NC}"; exit 1; }
# Install dependencies
echo -e "${YELLOW}Installing dependencies...${NC}"
npm ci || { echo -e "${RED}Failed to install dependencies.${NC}"; exit 1; }
# Build the application
echo -e "${YELLOW}Building the application...${NC}"
npm run build || { echo -e "${RED}Build failed.${NC}"; exit 1; }
# Setup environment variables
echo -e "${YELLOW}Setting up environment variables...${NC}"
if [ ! -f ".env.production" ]; then
echo -e "${YELLOW}Creating .env.production file...${NC}"
cat > .env.production << EOF
# Database Connection
DATABASE_URL="postgresql://username:password@localhost:5432/stones"
# Authentication
AUTH_SECRET="your-auth-secret" # Replace with a strong random string
# Application
NEXT_PUBLIC_APP_URL="https://contacts.boilerhaus.org"
EOF
echo -e "${YELLOW}Please update the .env.production file with your actual values.${NC}"
fi
# Setup PM2 process manager if not already configured
if ! command -v pm2 &> /dev/null; then
echo -e "${YELLOW}Installing PM2 process manager...${NC}"
npm install -g pm2
fi
# Check if the PM2 process already exists
if pm2 list | grep -q "$APP_NAME"; then
echo -e "${YELLOW}Restarting application with PM2...${NC}"
pm2 restart $APP_NAME
else
echo -e "${YELLOW}Setting up application with PM2...${NC}"
pm2 start npm --name $APP_NAME -- start -- -p 3001
# Save PM2 configuration to persist across server restarts
pm2 save
pm2 startup
fi
# Update Nginx configuration
echo -e "${YELLOW}Setting up Nginx configuration...${NC}"
NGINX_CONF="/etc/nginx/sites-available/contacts-boilerhaus-org.conf"
if [ ! -f "$NGINX_CONF" ]; then
echo -e "${YELLOW}Copying Nginx configuration file...${NC}"
# Assuming contacts-boilerhaus-org.conf is in the same directory as this script
cp ./contacts-boilerhaus-org.conf $NGINX_CONF
# Update paths in the Nginx configuration
sed -i "s|/path/to/your/app|$APP_DIR|g" $NGINX_CONF
# Create symlink if it doesn't exist
if [ ! -f "/etc/nginx/sites-enabled/contacts-boilerhaus-org.conf" ]; then
ln -s $NGINX_CONF /etc/nginx/sites-enabled/
fi
# Test Nginx configuration
nginx -t && systemctl reload nginx
else
echo -e "${YELLOW}Nginx configuration already exists.${NC}"
# Test Nginx configuration
nginx -t && systemctl reload nginx
fi
echo -e "${GREEN}Deployment completed successfully!${NC}"
echo -e "${GREEN}Your application should now be accessible at https://contacts.boilerhaus.org${NC}"