|
| 1 | +#!/bin/bash |
| 2 | +# Script to update and deploy the portfolio website to Vercel |
| 3 | + |
| 4 | +# Set variables |
| 5 | +GITHUB_USERNAME="mayur5204" |
| 6 | +REPO_NAME="portfolio-website" |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +GREEN='\033[0;32m' |
| 10 | +YELLOW='\033[1;33m' |
| 11 | +RED='\033[0;31m' |
| 12 | +NC='\033[0m' # No Color |
| 13 | + |
| 14 | +# Print colored output |
| 15 | +print_success() { |
| 16 | + echo -e "${GREEN}$1${NC}" |
| 17 | +} |
| 18 | + |
| 19 | +print_warning() { |
| 20 | + echo -e "${YELLOW}$1${NC}" |
| 21 | +} |
| 22 | + |
| 23 | +print_error() { |
| 24 | + echo -e "${RED}$1${NC}" |
| 25 | +} |
| 26 | + |
| 27 | +# Check if Git is installed |
| 28 | +if ! [ -x "$(command -v git)" ]; then |
| 29 | + print_error "Error: git is not installed." |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Check if npm is installed |
| 34 | +if ! [ -x "$(command -v npm)" ]; then |
| 35 | + print_error "Error: npm is not installed." |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +# Main update function |
| 40 | +update_and_deploy() { |
| 41 | + print_warning "Updating from GitHub..." |
| 42 | + |
| 43 | + # Check if we're in a git repository |
| 44 | + if [ ! -d ".git" ]; then |
| 45 | + print_error "This is not a git repository. Please run this script from the repository root." |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + |
| 49 | + # Save any uncommitted changes |
| 50 | + if [[ -n $(git status -s) ]]; then |
| 51 | + print_warning "Uncommitted changes detected. Stashing changes..." |
| 52 | + git stash |
| 53 | + fi |
| 54 | + |
| 55 | + # Make sure we're on the main branch |
| 56 | + print_warning "Switching to main branch..." |
| 57 | + git checkout main |
| 58 | + |
| 59 | + if [ $? -ne 0 ]; then |
| 60 | + print_error "Failed to switch to main branch. Please check your repository." |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + # Pull the latest changes |
| 65 | + print_warning "Pulling latest changes from GitHub..." |
| 66 | + git pull origin main |
| 67 | + |
| 68 | + if [ $? -ne 0 ]; then |
| 69 | + print_error "Failed to pull from GitHub. Please check your internet connection and GitHub access." |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + |
| 73 | + # Install any new dependencies |
| 74 | + print_warning "Installing dependencies..." |
| 75 | + npm install |
| 76 | + |
| 77 | + if [ $? -ne 0 ]; then |
| 78 | + print_error "Failed to install dependencies. Please check npm and try again." |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + |
| 82 | + print_success "Update completed successfully!" |
| 83 | + |
| 84 | + # Ask if user wants to deploy |
| 85 | + read -p "Do you want to deploy to Vercel now? (y/n): " deploy_choice |
| 86 | + |
| 87 | + if [[ $deploy_choice == [Yy]* ]]; then |
| 88 | + # Install Vercel CLI locally if not in node_modules |
| 89 | + if [ ! -d "node_modules/vercel" ]; then |
| 90 | + print_warning "Vercel CLI not found in local dependencies. Installing locally..." |
| 91 | + npm install --save-dev vercel |
| 92 | + |
| 93 | + if [ $? -ne 0 ]; then |
| 94 | + print_error "Failed to install Vercel CLI. Please check npm permissions and try again." |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + fi |
| 98 | + |
| 99 | + # Check if user is logged in to Vercel, if not prompt to login |
| 100 | + print_warning "Checking Vercel authentication..." |
| 101 | + npx vercel whoami &>/dev/null |
| 102 | + |
| 103 | + if [ $? -ne 0 ]; then |
| 104 | + print_warning "You are not logged in to Vercel. Please log in now:" |
| 105 | + npx vercel login |
| 106 | + |
| 107 | + if [ $? -ne 0 ]; then |
| 108 | + print_error "Failed to log in to Vercel. Please try again." |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | + fi |
| 112 | + |
| 113 | + # Deploy to Vercel (production) using local installation |
| 114 | + print_warning "Deploying to Vercel..." |
| 115 | + npx vercel --prod |
| 116 | + |
| 117 | + if [ $? -eq 0 ]; then |
| 118 | + print_success "Deployed successfully to Vercel!" |
| 119 | + else |
| 120 | + print_error "Deployment to Vercel failed. Please check the error messages above." |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | + else |
| 124 | + print_warning "Deployment skipped. You can run './deploy.sh' later to deploy." |
| 125 | + fi |
| 126 | +} |
| 127 | + |
| 128 | +# Execute the update function |
| 129 | +update_and_deploy |
0 commit comments