wheels env switch
Switch to a different environment in your Wheels application.
Synopsis
wheels env switch [name] [options]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
name
Target environment name
Required
Options
--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
Switch to staging
wheels env switch stagingSwitch with application restart
wheels env switch production --restartForce switch without validation
wheels env switch testing --forceSwitch with backup
wheels env switch production --backupQuiet switch for scripts
wheels env switch development --quietCombine multiple options
wheels env switch production --backup --restart --checkWhat It Does
Validates Target Environment (if
--checkis enabled):Checks for
.env.[environment]fileChecks for
config/[environment]/settings.cfmfileValidates environment configuration
Creates Backup (if
--backupis enabled):Backs up current
.envfileBacks up
server.jsonif it existsCreates timestamped backup files
Updates Configuration:
Updates or creates
wheels_envvariable in.envFalls back to
environmentvariable ifwheels_envdoesn't existUpdates
server.jsonprofile if file exists
Restarts Application (if
--restartis enabled):Stops and starts CommandBox server if
server.jsonexistsFalls back to
wheels reloadcommand
Output Example
Environment Switch
==================================================
Current Environment: development
Target Environment: staging
Validating target environment... [OK]
Creating backup... [OK]
Backup saved: .env.backup-20240115-103045
Switching environment... [OK]
Updated environment variable... [OK]
==================================================
[SUCCESS] Environment switched successfully!
Environment Details:
- Environment: staging
- Database: wheels_staging
- Debug Mode: Enabled
- Cache: Partial
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
.env File
The command updates or creates the wheels_env variable:
Before:
wheels_env=development
# or
environment=developmentAfter:
wheels_env=stagingIf no environment variable exists, it adds:
wheels_env=stagingValidation Process
The validation checks for required environment files:
Environment Configuration File:
Checks:
.env.[environment]Location: Project root
Wheels Settings File:
Checks:
config/[environment]/settings.cfmLocation: Project config directory
Validation Rules:
Valid: Both files exist
Valid with warning: One file exists
Invalid: Neither file exists (unless
--forceis used)
Options Details
--check (default: true)
Validates the target environment before switching:
# With validation (default)
wheels env switch production
# Skip validation
wheels env switch production --no-check--backup (default: false)
Creates timestamped backups before switching:
wheels env switch production --backup
# Creates: .env.backup-20240115-103045
# Creates: server.json.backup-20240115-103045 (if exists)--restart (default: false)
Automatically restarts the application:
wheels env switch production --restart--force (default: false)
Bypasses validation and confirmation prompts:
# Force switch even if validation fails
wheels env switch production --force
# Combine with other options
wheels env switch production --force --no-check--quiet (default: false)
Minimal output for scripting:
wheels env switch production --quiet
# Output: Environment switched to productionProduction 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
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
The --backup option creates timestamped backup files:
Created Files:
.env.backup-YYYYMMDD-HHMMSS- Backup of current .env fileserver.json.backup-YYYYMMDD-HHMMSS- Backup of server.json (if exists)
Manual Restore:
If you need to restore from a backup:
# Restore .env file
cp .env.backup-20240115-103045 .env
# Restore server.json
cp server.json.backup-20240115-103045 server.json
# Reload application
wheels reloadService Management
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
Manual Restart
If --restart is not used, you need to manually restart:
# CommandBox server
server restart
# Or Wheels development server
wheels reloadEnvironment-Specific Configuration
The command reads additional configuration from .env.[environment] files if they exist:
Supported Variables:
database- Database connection namedebug- Debug mode (true/false)cache- Cache configuration
Example .env.production:
database=wheels_production
debug=false
cache=fullIntegration Examples
CI/CD Pipeline
- name: Switch to staging
run: |
wheels env switch staging --check
wheels test run
wheels deploy exec stagingDeployment Scripts
#!/bin/bash
# deploy.sh
# Switch environment with backup
wheels env switch $1 --backup
# Run migrations
wheels dbmigrate latest
# Clear caches
wheels cache clear
# Verify environment
wheels env currentAutomated Testing
# Switch to testing environment quietly
wheels env switch testing --quiet --force
# Run tests
wheels test run
# Switch back to development
wheels env switch development --quietTroubleshooting
Switch Failed
Check validation errors in output
Verify
.env.[environment]file existsVerify
config/[environment]/settings.cfmexistsUse
--forceto bypass validation
Application Not Responding After Switch
Ensure server was restarted
Check
.envfile for correctwheels_envvalueReview application logs for errors
Manually restart services if needed
Permission Issues
Check write permissions for
.envfileRun with appropriate privileges
Ensure backup directory is writable
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
Always validate: Keep
--checkenabled for production switchesCreate backups: Use
--backupfor critical environment changesTest first: Switch in staging before production
Use --restart: Automatically restart to apply changes immediately
Document changes: Log environment switches in deployment notes
Security Considerations
Production switches require explicit confirmation
Backup files contain sensitive configuration
.envfiles should be in.gitignoreUse
--quietmode carefully in automated scriptsReview environment-specific configurations regularly
Notes
The command modifies the
.envfile in placeCreates
wheels_envvariable if it doesn't existFalls back to updating
environmentvariable if foundSome 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
0- Success1- Failure (validation error, write error, or user cancellation)
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
Last updated
Was this helpful?

