upskill-event-manager/docs/deploy-plugin-safe-script.md
bengizmo cade20aa2b feat(testing): Implement HVAC Role Manager component
- Added HVAC_Role_Manager class with role/permission management
- Implemented test cases in HVAC_Role_Manager_Test.php
- Created API documentation in docs/role-manager-api.md
- Updated testing improvement plan with progress
- Added design decisions to memory-bank/decisionLog.md

Includes:
- Role creation/deletion methods
- Permission management system
- Role conflict detection
- Permission inheritance logic
- Comprehensive test coverage
2025-04-14 19:02:22 -03:00

4.4 KiB

Enhanced Deploy Plugin Script

This document contains a safer version of the deploy-plugin.sh script with added safeguards to prevent WordPress core file corruption.

#!/bin/bash

# Deploy WordPress plugin to staging server with safety measures
# Enhanced version with path validation and dry-run option

# Load configuration file
if [ -z "$1" ]; then
  echo "Usage: $0 --config <config_file> [--dry-run]"
  exit 1
fi

DRY_RUN=false

while [ "$1" != "" ]; do
  case $1 in
    --config )
      shift
      if [ -z "$1" ]; then
        echo "Error: --config requires a value"
        exit 1
      fi
      CONFIG_FILE="$1"
      shift
      ;;
    --dry-run )
      DRY_RUN=true
      shift
      ;;
    * )
      echo "Error: Invalid argument: $1"
      exit 1
  esac
done

if [ ! -f "$CONFIG_FILE" ]; then
  echo "Error: Configuration file not found: $CONFIG_FILE"
  exit 1
fi

source "$CONFIG_FILE"

# Check required variables
if [ -z "$REMOTE_HOST" ] || [ -z "$REMOTE_USER" ] || [ -z "$REMOTE_PATH_BASE" ] || [ -z "$PLUGIN_SLUG" ] || [ -z "$REMOTE_PLUGIN_PATH" ] || [ -z "$LOCAL_PLUGIN_PATH" ]; then
  echo "Error: Missing required variables in configuration file."
  exit 1
fi

# Validate paths to ensure we're only modifying plugin directory
if [[ "$REMOTE_PLUGIN_PATH" != *"/wp-content/plugins/$PLUGIN_SLUG"* ]]; then
  echo "Error: Remote plugin path does not appear to be within the WordPress plugins directory."
  echo "Expected path pattern: */wp-content/plugins/$PLUGIN_SLUG*"
  echo "Actual path: $REMOTE_PLUGIN_PATH"
  exit 1
fi

if [[ "$LOCAL_PLUGIN_PATH" != *"/wp-content/plugins/$PLUGIN_SLUG"* ]]; then
  echo "Error: Local plugin path does not appear to be within the WordPress plugins directory."
  echo "Expected path pattern: */wp-content/plugins/$PLUGIN_SLUG*"
  echo "Actual path: $LOCAL_PLUGIN_PATH"
  exit 1
fi

# Create backup of plugin directory on staging server
echo "Creating backup of plugin directory on staging server..."
if [ "$DRY_RUN" = false ]; then
  ssh "$REMOTE_USER@$REMOTE_HOST" "if [ -d \"$REMOTE_PLUGIN_PATH\" ]; then cp -r \"$REMOTE_PLUGIN_PATH\" \"${REMOTE_PLUGIN_PATH}_backup_$(date +%Y%m%d%H%M%S)\"; fi"
  if [ $? -ne 0 ]; then
    echo "Warning: Failed to create backup on staging server."
  fi
else
  echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"if [ -d \\\"$REMOTE_PLUGIN_PATH\\\" ]; then cp -r \\\"$REMOTE_PLUGIN_PATH\\\" \\\"${REMOTE_PLUGIN_PATH}_backup_$(date +%Y%m%d%H%M%S)\\\"; fi\""
fi

# Rsync the plugin files
echo "Deploying plugin $PLUGIN_SLUG to staging server..."
RSYNC_CMD="rsync -avz --delete \
  --exclude '.git' \
  --exclude 'node_modules' \
  --include 'tests/' \
  --include 'tests/unit/' \
  --include 'tests/unit/*.php' \
  --include 'tests/test-doubles.php' \
  --include 'tests/bootstrap.php' \
  \"$LOCAL_PLUGIN_PATH\" \
  \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_PLUGIN_PATH\""

if [ "$DRY_RUN" = true ]; then
  echo "[DRY RUN] Would execute: $RSYNC_CMD"
else
  eval $RSYNC_CMD
  if [ $? -ne 0 ]; then
    echo "Error: rsync failed."
    exit 1
  fi
fi

echo "Plugin deployment completed successfully."

# Optional: Install Composer dependencies on staging
echo "Installing Composer dependencies on staging server..."
if [ "$DRY_RUN" = true ]; then
  echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"cd $REMOTE_PLUGIN_PATH && composer install\""
else
  ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PLUGIN_PATH && composer install"
  if [ $? -ne 0 ]; then
    echo "Warning: Composer installation failed."
  fi
fi

# Verify deployment
echo "Verifying deployment..."
if [ "$DRY_RUN" = true ]; then
  echo "[DRY RUN] Would execute: ssh \"$REMOTE_USER@$REMOTE_HOST\" \"ls -la $REMOTE_PLUGIN_PATH\""
else
  ssh "$REMOTE_USER@$REMOTE_HOST" "ls -la $REMOTE_PLUGIN_PATH"
  if [ $? -ne 0 ]; then
    echo "Warning: Verification failed."
  fi
fi

exit 0

Key Improvements

  1. Path Validation: Ensures that both local and remote paths contain the expected plugin directory structure
  2. Dry Run Option: Allows previewing the commands that would be executed without making actual changes
  3. Backup Creation: Creates a timestamped backup of the plugin directory before deployment
  4. Explicit Test Files: Specifically includes test-doubles.php and bootstrap.php in the rsync command
  5. Composer Installation: Automatically installs Composer dependencies on the staging server
  6. Deployment Verification: Lists the deployed files to verify successful deployment