Command Line Tools
wheels env switch
wheels env switch
Section titled “wheels env switch”Switch to a different environment in your Wheels application.
Synopsis
Section titled “Synopsis”wheels env switch [name] [options]Description
Section titled “Description”The wheels env switch command changes the active environment for your Wheels application by updating the wheels_env variable in the .env file. It validates the environment configuration, optionally creates backups, and can restart the application server.
Arguments
Section titled “Arguments”| Argument | Description | Default |
|---|---|---|
name | Target environment name | Required |
Options
Section titled “Options”| Option | Description | Default |
|---|---|---|
--check | Validate before switching | true |
--restart | Restart application after switch | false |
--backup | Backup current environment | false |
--force | Force switch even with issues | false |
--quiet | Suppress output | false |
Examples
Section titled “Examples”Switch to staging
Section titled “Switch to staging”wheels env switch stagingSwitch with application restart
Section titled “Switch with application restart”wheels env switch production --restartForce switch without validation
Section titled “Force switch without validation”wheels env switch testing --forceSwitch with backup
Section titled “Switch with backup”wheels env switch production --backupQuiet switch for scripts
Section titled “Quiet switch for scripts”wheels env switch development --quietCombine multiple options
Section titled “Combine multiple options”wheels env switch production --backup --restart --checkWhat It Does
Section titled “What It Does”-
Validates Target Environment (if
--checkis enabled):- Checks for
.env.[environment]file - Checks for
config/[environment]/settings.cfmfile - Validates environment configuration
- Checks for
-
Creates Backup (if
--backupis enabled):- Backs up current
.envfile - Backs up
server.jsonif it exists - Creates timestamped backup files
- Backs up current
-
Updates Configuration:
- Updates or creates
wheels_envvariable in.env - Falls back to
environmentvariable ifwheels_envdoesn’t exist - Updates
server.jsonprofile if file exists
- Updates or creates
-
Restarts Application (if
--restartis enabled):- Stops and starts CommandBox server if
server.jsonexists - Falls back to
wheels reloadcommand
- Stops and starts CommandBox server if
Output Example
Section titled “Output Example”================================================== Environment Switch==================================================
Current Environment: developmentTarget Environment: staging
Validating target environment... [OK]Creating backup... [OK] Backup saved: .env.backup-20240115-103045Switching environment... [OK]Updated environment variable... [OK]
[SUCCESS]: Environment switched successfully!
Environment Details--------------------------------------------------Current Environment: productionTarget Environment: stagingDebug Mode: EnabledCache: Partial
[INFO]: IMPORTANT- Restart your application server for changes to take effect- Run 'wheels reload' if using Wheels development server- Or use 'wheels env switch staging --restart' next timeEnvironment File Updates
Section titled “Environment File Updates”.env File
Section titled “.env File”The command updates or creates the wheels_env variable:
Before:
wheels_env=development# orenvironment=developmentAfter:
wheels_env=stagingIf no environment variable exists, it adds:
wheels_env=stagingValidation Process
Section titled “Validation Process”The validation checks for required environment files:
-
Environment Configuration File:
- Checks:
.env.[environment] - Location: Project root
- Checks:
-
Wheels Settings File:
- Checks:
config/[environment]/settings.cfm - Location: Project config directory
- Checks:
Validation Rules:
Section titled “Validation Rules:”- Valid: Both files exist
- Valid with warning: One file exists
- Invalid: Neither file exists (unless
--forceis used)
Options Details
Section titled “Options Details”—check (default: true)
Section titled “—check (default: true)”Validates the target environment before switching:
# With validation (default)wheels env switch production
# Skip validationwheels env switch production --no-check—backup (default: false)
Section titled “—backup (default: false)”Creates timestamped backups before switching:
wheels env switch production --backup# Creates: server.json.backup-20240115-103045 (if exists)—restart (default: false)
Section titled “—restart (default: false)”Automatically restarts the application:
wheels env switch production --restart—force (default: false)
Section titled “—force (default: false)”Bypasses validation and confirmation prompts:
# Force switch even if validation failswheels env switch production --force
# Combine with other optionswheels env switch production --force --no-check—quiet (default: false)
Section titled “—quiet (default: false)”Minimal output for scripting:
wheels env switch production --quiet# Output: Environment switched to productionProduction Safety
Section titled “Production Safety”When switching to production from another environment:
- Displays warning about production implications
- Requires confirmation (unless
--forceor--quiet) - Shows warnings about debug mode, caching, and error handling
Warning message:
WARNING: Switching to PRODUCTION environment This will: - Disable debug mode - Enable full caching - Hide detailed error messages
Are you sure you want to continue? (yes/no):Error Handling
Section titled “Error Handling”If the switch fails:
- Displays error message
- Provides troubleshooting suggestions
- Sets exit code 1 for scripting
Example error output:
[X] Failed to switch environment Error: Environment 'invalid' is not configured
Suggestions:- Check if you have write permissions for .env file- Ensure the environment name is valid- Try running with administrator/sudo privileges if needed- Use --force to bypass validation checksBackup Files
Section titled “Backup Files”The --backup option creates timestamped backup files:
Created Files:
Section titled “Created Files:”.env.backup-YYYYMMDD-HHMMSS- Backup of current .env fileserver.json.backup-YYYYMMDD-HHMMSS- Backup of server.json (if exists)
Manual Restore:
Section titled “Manual Restore:”If you need to restore from a backup:
# Restore .env filecp .env.backup-20240115-103045 .env
# Restore server.jsoncp server.json.backup-20240115-103045 server.json
# Reload applicationwheels reloadService Management
Section titled “Service Management”With —restart Option
Section titled “With —restart Option”When using --restart, the command will:
-
If server.json exists:
- Stop CommandBox server
- Start CommandBox server
-
If server.json doesn’t exist:
- Execute
wheels reloadcommand
- Execute
Manual Restart
Section titled “Manual Restart”If --restart is not used, you need to manually restart:
# CommandBox serverserver restart
# Or Wheels development serverwheels reloadEnvironment-Specific Configuration
Section titled “Environment-Specific Configuration”The command reads additional configuration from .env.[environment] files if they exist:
Supported Variables:
Section titled “Supported Variables:”database- Database connection namedebug- Debug mode (true/false)cache- Cache configuration
Example .env.production:
database=wheels_productiondebug=falsecache=fullIntegration Examples
Section titled “Integration Examples”CI/CD Pipeline
Section titled “CI/CD Pipeline”- name: Switch to staging run: | wheels env switch staging --check wheels test run wheels deploy exec stagingDeployment Scripts
Section titled “Deployment Scripts”#!/bin/bash# Switch environment with backupwheels env switch $1 --backup
# Run migrationswheels dbmigrate latest
# Clear cacheswheels cache clear
# Verify environmentwheels env currentAutomated Testing
Section titled “Automated Testing”# Switch to testing environment quietlywheels env switch testing --quiet --force
# Run testswheels test run
# Switch back to developmentwheels env switch development --quietTroubleshooting
Section titled “Troubleshooting”Switch Failed
Section titled “Switch Failed”- Check validation errors in output
- Verify
.env.[environment]file exists - Verify
config/[environment]/settings.cfmexists - Use
--forceto bypass validation
Application Not Responding After Switch
Section titled “Application Not Responding After Switch”- Ensure server was restarted
- Check
.envfile for correctwheels_envvalue - Review application logs for errors
- Manually restart services if needed
Permission Issues
Section titled “Permission Issues”- Check write permissions for
.envfile - Run with appropriate privileges
- Ensure backup directory is writable
Validation Warnings
Section titled “Validation Warnings”- Warning appears if only one configuration file exists
- Environment may work but might not be fully configured
- Check both
.env.[environment]andconfig/[environment]/settings.cfm
Best Practices
Section titled “Best Practices”- Always validate: Keep
--checkenabled for production switches - Create backups: Use
--backupfor critical environment changes - Test first: Switch in staging before production
- Use —restart: Automatically restart to apply changes immediately
- Document changes: Log environment switches in deployment notes
Security Considerations
Section titled “Security Considerations”- Production switches require explicit confirmation
- Backup files contain sensitive configuration
.envfiles should be in.gitignore- Use
--quietmode carefully in automated scripts - Review environment-specific configurations regularly
- The command modifies the
.envfile in place - Creates
wheels_envvariable if it doesn’t exist - Falls back to updating
environmentvariable if found - Some changes require application restart to take effect
- Database connections may need to be reset after switching
- Cached data should be cleared after environment switch
Exit Codes
Section titled “Exit Codes”0- Success1- Failure (validation error, write error, or user cancellation)
See Also
Section titled “See Also”- wheels env - Environment management overview
- wheels env list - List available environments
- wheels env setup - Setup new environments
- wheels env current - Show current environment
- wheels reload - Reload application