How to deploy WellNuo mobile react app? #12

Open
opened 2025-06-18 11:40:39 -07:00 by robert · 2 comments
Owner
No description provided.
Author
Owner

cd ~
./deploy_well_mob.sh

cd ~ ./deploy_well_mob.sh
Author
Owner

#!/bin/bash

Deploy Well Mob PWA Script

Usage: ./deploy_well_mob.sh [log_identifier]

set -e # Exit on any error

Configuration

git_repo="https://github.com/zmrzli/well_mob_pwa.git"
local_repo_dir="$HOME/well_mob_pwa"
deploy_dir="/home/ubuntu/server-setup/well_mob_pwa"
server_setup_dir="$HOME/server-setup"

Color codes for output

RED='\033[0;31m'
GREEN='\033[0;92m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

Logging function

log() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}"
}

error() {
echo -e "${RED}[ERROR] $1${NC}" >&2
}

warning() {
echo -e "${YELLOW}[WARNING] $1${NC}"
}

Execute command with logging

exeLog() {
local cmd="$1"
local display_cmd="${2:-$1}"

log "Executing: $display_cmd"
if eval "$cmd"; then
    log "Success: $display_cmd"
    return 0
else
    error "Failed: $display_cmd"
    return 1
fi

}

Package check and install function

pkgChk() {
local packages="$1"
log "Checking/installing packages: $packages"

# Update package list
sudo apt-get update -qq

# Install packages
for pkg in $packages; do
    if ! dpkg -l | grep -q "^ii  $pkg "; then
        log "Installing $pkg..."
        sudo apt-get install -y "$pkg"
    else
        log "$pkg is already installed"
    fi
done

return 0

}

Main deployment function

deploy_well_mob() {
log "Starting Well Mob PWA deployment..."

# Check for GITTOKEN
if [[ -z "${GITTOKEN}" ]]; then
    error "GITTOKEN environment variable is not set."
    exit 1
fi

# Install required packages
if ! pkgChk "nodejs npm"; then
    error "Failed to install nodejs and npm"
    exit 1
fi

# Clean up existing repo and change to parent directory first
log "Cleaning up existing repository..."
cd "$HOME" || {
    error "Failed to change to home directory"
    exit 1
}
rm -rf "$local_repo_dir"

# Clone repository
if ! exeLog "git clone https://${GITTOKEN}@github.com/zmrzli/well_mob_pwa.git \"$local_repo_dir\"" "git clone $git_repo"; then
    exit 1
fi

# Change to repo directory
cd "$local_repo_dir" || {
    error "Failed to change directory to $local_repo_dir"
    exit 1
}

# Set up environment variables
if ! exeLog "echo \"REACT_APP_VERSION=\$(git rev-parse --short HEAD)\" > .env" "Setting REACT_APP_VERSION in .env"; then
    exit 1
fi

if ! exeLog "echo \"REACT_APP_API_BASE=https://eluxnetworks.net\" >> .env" "Setting REACT_APP_API_BASE in .env"; then
    exit 1
fi

# Install npm dependencies with timeout and better error handling
log "Installing npm dependencies (this may take a few minutes)..."
if ! timeout 600 npm install --verbose --no-optional --legacy-peer-deps; then
    warning "npm install failed or timed out, trying with cache clean..."
    npm cache clean --force
    if ! timeout 600 npm install --verbose --no-optional --legacy-peer-deps --force; then
        error "npm install failed after retry"
        exit 1
    fi
fi
log "Success: npm install completed"

# Build the project
if ! exeLog "time npm run build --verbose" "npm run build"; then
    exit 1
fi

# Deploy built files to the build subdirectory (mounted by Docker)
if ! exeLog "sudo cp -v -rf $local_repo_dir/build/* $deploy_dir/build/." "cp -v -rf $local_repo_dir/build/* $deploy_dir/build/."; then
    exit 1
fi

# Change back to server setup directory
cd "$server_setup_dir" || {
    error "Failed to change directory to $server_setup_dir"
    exit 1
}

# Restart nginx
if ! exeLog "docker-compose restart nginx" "docker-compose restart nginx"; then
    exit 1
fi

# Optional log command reminder
echo -e "${GREEN}--> <optional>run: wnDockerNginxLog \"$1\"${NC}"

log "Well Mob PWA deployment completed successfully!"

}

Check if script is being sourced or executed

if ; then
# Script is being executed directly
deploy_well_mob "$@"
else
# Script is being sourced
log "Script sourced. Call deploy_well_mob function to run deployment."
fi

#!/bin/bash # Deploy Well Mob PWA Script # Usage: ./deploy_well_mob.sh [log_identifier] set -e # Exit on any error # Configuration git_repo="https://github.com/zmrzli/well_mob_pwa.git" local_repo_dir="$HOME/well_mob_pwa" deploy_dir="/home/ubuntu/server-setup/well_mob_pwa" server_setup_dir="$HOME/server-setup" # Color codes for output RED='\033[0;31m' GREEN='\033[0;92m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Logging function log() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" } error() { echo -e "${RED}[ERROR] $1${NC}" >&2 } warning() { echo -e "${YELLOW}[WARNING] $1${NC}" } # Execute command with logging exeLog() { local cmd="$1" local display_cmd="${2:-$1}" log "Executing: $display_cmd" if eval "$cmd"; then log "Success: $display_cmd" return 0 else error "Failed: $display_cmd" return 1 fi } # Package check and install function pkgChk() { local packages="$1" log "Checking/installing packages: $packages" # Update package list sudo apt-get update -qq # Install packages for pkg in $packages; do if ! dpkg -l | grep -q "^ii $pkg "; then log "Installing $pkg..." sudo apt-get install -y "$pkg" else log "$pkg is already installed" fi done return 0 } # Main deployment function deploy_well_mob() { log "Starting Well Mob PWA deployment..." # Check for GITTOKEN if [[ -z "${GITTOKEN}" ]]; then error "GITTOKEN environment variable is not set." exit 1 fi # Install required packages if ! pkgChk "nodejs npm"; then error "Failed to install nodejs and npm" exit 1 fi # Clean up existing repo and change to parent directory first log "Cleaning up existing repository..." cd "$HOME" || { error "Failed to change to home directory" exit 1 } rm -rf "$local_repo_dir" # Clone repository if ! exeLog "git clone https://${GITTOKEN}@github.com/zmrzli/well_mob_pwa.git \"$local_repo_dir\"" "git clone $git_repo"; then exit 1 fi # Change to repo directory cd "$local_repo_dir" || { error "Failed to change directory to $local_repo_dir" exit 1 } # Set up environment variables if ! exeLog "echo \"REACT_APP_VERSION=\$(git rev-parse --short HEAD)\" > .env" "Setting REACT_APP_VERSION in .env"; then exit 1 fi if ! exeLog "echo \"REACT_APP_API_BASE=https://eluxnetworks.net\" >> .env" "Setting REACT_APP_API_BASE in .env"; then exit 1 fi # Install npm dependencies with timeout and better error handling log "Installing npm dependencies (this may take a few minutes)..." if ! timeout 600 npm install --verbose --no-optional --legacy-peer-deps; then warning "npm install failed or timed out, trying with cache clean..." npm cache clean --force if ! timeout 600 npm install --verbose --no-optional --legacy-peer-deps --force; then error "npm install failed after retry" exit 1 fi fi log "Success: npm install completed" # Build the project if ! exeLog "time npm run build --verbose" "npm run build"; then exit 1 fi # Deploy built files to the build subdirectory (mounted by Docker) if ! exeLog "sudo cp -v -rf $local_repo_dir/build/* $deploy_dir/build/." "cp -v -rf $local_repo_dir/build/* $deploy_dir/build/."; then exit 1 fi # Change back to server setup directory cd "$server_setup_dir" || { error "Failed to change directory to $server_setup_dir" exit 1 } # Restart nginx if ! exeLog "docker-compose restart nginx" "docker-compose restart nginx"; then exit 1 fi # Optional log command reminder echo -e "${GREEN}--> <optional>run: wnDockerNginxLog \"$1\"${NC}" log "Well Mob PWA deployment completed successfully!" } # Check if script is being sourced or executed if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then # Script is being executed directly deploy_well_mob "$@" else # Script is being sourced log "Script sourced. Call deploy_well_mob function to run deployment." fi
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: robert/project-issues#12
No description provided.