|
1 | | -#!/bin/bash |
2 | | - |
3 | | -# Deployment script for pull-based deployment |
4 | | -# This script runs on the instance to pull and deploy |
5 | | - |
6 | | -set -e |
7 | | - |
8 | | -# Determine environment from argument or git branch |
9 | | -ENVIRONMENT=${1:-"production"} |
10 | | -BRANCH=${2:-"main"} |
11 | | - |
12 | | -if [ "$ENVIRONMENT" == "production" ]; then |
13 | | - DEPLOY_PATH="${DEPLOY_PATH:-$HOME/processordb-website}" |
14 | | - PM2_APP_NAME="ProcessorDB-website" |
15 | | - GIT_BRANCH="main" |
16 | | -else |
17 | | - DEPLOY_PATH="${STAGING_DEPLOY_PATH:-$HOME/processordb-website-staging}" |
18 | | - PM2_APP_NAME="ProcessorDB-website-staging" |
19 | | - GIT_BRANCH="dev" |
20 | | -fi |
21 | | - |
22 | | -echo "Starting deployment for $ENVIRONMENT..." |
23 | | -echo "Deploy path: $DEPLOY_PATH" |
24 | | -echo "Branch: $GIT_BRANCH" |
25 | | - |
26 | | -# Navigate to deployment directory |
27 | | -cd "$DEPLOY_PATH" || { |
28 | | - echo "Error: Deployment directory $DEPLOY_PATH does not exist" |
29 | | - exit 1 |
30 | | -} |
31 | | - |
32 | | -# Load .env file if it exists (for environment variables) |
33 | | -if [ -f .env ]; then |
34 | | - echo "Loading environment variables from .env file..." |
35 | | - set -a |
36 | | - source .env |
37 | | - set +a |
38 | | -fi |
39 | | - |
40 | | -# Pull latest code |
41 | | -echo "Pulling latest code from $GIT_BRANCH..." |
42 | | -git fetch origin |
43 | | -git checkout "$GIT_BRANCH" |
44 | | -# Clean any uncommitted changes and reset to remote branch |
45 | | -# This ensures we're always deploying what's on the remote dev branch |
46 | | -git clean -fd |
47 | | -git reset --hard "origin/$GIT_BRANCH" |
48 | | - |
49 | | -# Fix line endings in deploy script (in case repo has CRLF) |
50 | | -# This prevents "$'\r': command not found" errors |
51 | | -if [ -f "scripts/deploy.sh" ]; then |
52 | | - sed -i 's/\r$//' scripts/deploy.sh |
53 | | -fi |
54 | | - |
55 | | -# Load nvm |
56 | | -if [ -s "$HOME/.nvm/nvm.sh" ]; then |
57 | | - source "$HOME/.nvm/nvm.sh" |
58 | | -else |
59 | | - echo "Warning: nvm not found, assuming node is in PATH" |
60 | | -fi |
61 | | - |
62 | | -# Install dependencies |
63 | | -echo "Installing dependencies..." |
64 | | -# Ensure devDependencies are installed (npm ci installs them by default unless NODE_ENV=production) |
65 | | -NODE_ENV=development npm ci |
66 | | - |
67 | | -# Verify critical dependency is installed |
68 | | -if [ ! -d "node_modules/@nuxtjs/tailwindcss" ]; then |
69 | | - echo "Warning: @nuxtjs/tailwindcss not found, installing it explicitly..." |
70 | | - npm install @nuxtjs/tailwindcss --save-dev |
71 | | -fi |
72 | | - |
73 | | -# Prepare Nuxt modules (ensure this runs after all packages are installed) |
74 | | -echo "Preparing Nuxt modules..." |
75 | | -npx nuxt prepare || { |
76 | | - echo "Warning: nuxt prepare failed, but continuing with build..." |
77 | | -} |
78 | | - |
79 | | -# Build application |
80 | | -echo "Building application..." |
81 | | -# SITE_URL and BACKEND_URL should be loaded from .env if it exists |
82 | | -npm run build |
83 | | - |
84 | | -# Determine ecosystem config file |
85 | | -if [ "$ENVIRONMENT" == "staging" ] && [ -f "ecosystem.staging.config.cjs" ]; then |
86 | | - ECOSYSTEM_FILE="ecosystem.staging.config.cjs" |
87 | | -elif [ "$ENVIRONMENT" == "staging" ] && [ -f "ecosystem.staging.config.js" ]; then |
88 | | - ECOSYSTEM_FILE="ecosystem.staging.config.js" |
89 | | -else |
90 | | - ECOSYSTEM_FILE="ecosystem.config.js" |
91 | | -fi |
92 | | - |
93 | | -# Restart PM2 application |
94 | | -echo "Restarting PM2 application..." |
95 | | -pm2 restart "$PM2_APP_NAME" || pm2 start "$ECOSYSTEM_FILE" |
96 | | - |
97 | | -# Save PM2 process list |
98 | | -pm2 save --force |
99 | | - |
100 | | -echo "Deployment completed successfully for $ENVIRONMENT!" |
101 | | - |
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Deployment script for pull-based deployment |
| 4 | +# This script runs on the instance to pull and deploy |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Determine environment from argument or git branch |
| 9 | +ENVIRONMENT=${1:-"production"} |
| 10 | +BRANCH=${2:-"main"} |
| 11 | + |
| 12 | +if [ "$ENVIRONMENT" == "production" ]; then |
| 13 | + DEPLOY_PATH="${DEPLOY_PATH:-$HOME/processordb-website}" |
| 14 | + PM2_APP_NAME="ProcessorDB-website" |
| 15 | + GIT_BRANCH="main" |
| 16 | +else |
| 17 | + DEPLOY_PATH="${STAGING_DEPLOY_PATH:-$HOME/processordb-website-staging}" |
| 18 | + PM2_APP_NAME="ProcessorDB-website-staging" |
| 19 | + GIT_BRANCH="dev" |
| 20 | +fi |
| 21 | + |
| 22 | +echo "Starting deployment for $ENVIRONMENT..." |
| 23 | +echo "Deploy path: $DEPLOY_PATH" |
| 24 | +echo "Branch: $GIT_BRANCH" |
| 25 | + |
| 26 | +# Navigate to deployment directory |
| 27 | +cd "$DEPLOY_PATH" || { |
| 28 | + echo "Error: Deployment directory $DEPLOY_PATH does not exist" |
| 29 | + exit 1 |
| 30 | +} |
| 31 | + |
| 32 | +# Load .env file if it exists (for environment variables) |
| 33 | +if [ -f .env ]; then |
| 34 | + echo "Loading environment variables from .env file..." |
| 35 | + set -a |
| 36 | + source .env |
| 37 | + set +a |
| 38 | +fi |
| 39 | + |
| 40 | +# Pull latest code |
| 41 | +echo "Pulling latest code from $GIT_BRANCH..." |
| 42 | +git fetch origin |
| 43 | +git checkout "$GIT_BRANCH" |
| 44 | +# Clean any uncommitted changes and reset to remote branch |
| 45 | +# This ensures we're always deploying what's on the remote dev branch |
| 46 | +git clean -fd |
| 47 | +git reset --hard "origin/$GIT_BRANCH" |
| 48 | + |
| 49 | +# Fix line endings in deploy script (in case repo has CRLF) |
| 50 | +# This prevents "$'\r': command not found" errors |
| 51 | +if [ -f "scripts/deploy.sh" ]; then |
| 52 | + sed -i 's/\r$//' scripts/deploy.sh |
| 53 | +fi |
| 54 | + |
| 55 | +# Load nvm |
| 56 | +if [ -s "$HOME/.nvm/nvm.sh" ]; then |
| 57 | + source "$HOME/.nvm/nvm.sh" |
| 58 | +else |
| 59 | + echo "Warning: nvm not found, assuming node is in PATH" |
| 60 | +fi |
| 61 | + |
| 62 | +# Install dependencies |
| 63 | +echo "Installing dependencies..." |
| 64 | +# Ensure devDependencies are installed (npm ci installs them by default unless NODE_ENV=production) |
| 65 | +NODE_ENV=development npm ci |
| 66 | + |
| 67 | +# Verify critical dependency is installed |
| 68 | +if [ ! -d "node_modules/@nuxtjs/tailwindcss" ]; then |
| 69 | + echo "Warning: @nuxtjs/tailwindcss not found, installing it explicitly..." |
| 70 | + npm install @nuxtjs/tailwindcss --save-dev |
| 71 | +fi |
| 72 | + |
| 73 | +# Prepare Nuxt modules (ensure this runs after all packages are installed) |
| 74 | +echo "Preparing Nuxt modules..." |
| 75 | +npx nuxt prepare || { |
| 76 | + echo "Warning: nuxt prepare failed, but continuing with build..." |
| 77 | +} |
| 78 | + |
| 79 | +# Build application |
| 80 | +echo "Building application..." |
| 81 | +# SITE_URL and BACKEND_URL should be loaded from .env if it exists |
| 82 | +npm run build |
| 83 | + |
| 84 | +# Determine ecosystem config file |
| 85 | +if [ "$ENVIRONMENT" == "staging" ] && [ -f "ecosystem.staging.config.cjs" ]; then |
| 86 | + ECOSYSTEM_FILE="ecosystem.staging.config.cjs" |
| 87 | +elif [ "$ENVIRONMENT" == "staging" ] && [ -f "ecosystem.staging.config.js" ]; then |
| 88 | + ECOSYSTEM_FILE="ecosystem.staging.config.js" |
| 89 | +else |
| 90 | + ECOSYSTEM_FILE="ecosystem.config.js" |
| 91 | +fi |
| 92 | + |
| 93 | +# Restart PM2 application |
| 94 | +echo "Restarting PM2 application..." |
| 95 | +pm2 restart "$PM2_APP_NAME" || pm2 start "$ECOSYSTEM_FILE" |
| 96 | + |
| 97 | +# Save PM2 process list |
| 98 | +pm2 save --force |
| 99 | + |
| 100 | +echo "Deployment completed successfully for $ENVIRONMENT!" |
| 101 | + |
0 commit comments