Deployment commands allow you to automate tasks before and after file transfers during your deployments. This article provides practical examples of common deployment commands for various scenarios.

For detailed information on how to configure SSH commands, see the [SSH Commands](Article: #57) article.

## Understanding Deployment Command Timing

Deployment commands can be executed at two key points during the deployment process:

- **Pre-deployment commands** - Run before files are transferred to your server. Useful for preparing the environment, backing up data, or putting the application into maintenance mode.
- **Post-deployment commands** - Run after files are transferred to your server. Used for cache clearing, running migrations, restarting services, or taking the application out of maintenance mode.

## Common Pre-Deployment Command Examples

### Enable Maintenance Mode

Put your application into maintenance mode before deployment to prevent users from accessing a partially deployed application:

```bash
cd /var/www/myapp && \
php artisan down --message="Deployment in progress" --retry=60
```

### Create Database Backup

Back up your database before deploying changes that include migrations:

```bash
cd /var/www/myapp && \
mysqldump -u dbuser -p'password' database_name > /home/user/backups/db_backup_$(date +%Y%m%d_%H%M%S).sql
```

### Stop Application Services

Stop services that may lock files during deployment:

```bash
cd /var/www/myapp && \
sudo systemctl stop myapp-worker && \
sudo systemctl stop myapp-scheduler
```

### Clear Temporary Files

Remove temporary files or caches before deployment:

```bash
cd /var/www/myapp && \
rm -rf storage/framework/cache/data/* && \
rm -rf storage/framework/views/*
```

## Common Post-Deployment Command Examples

### Install Dependencies

Install or update application dependencies after new code is deployed:

**For PHP/Composer:**
```bash
cd /var/www/myapp && \
composer install --no-dev --optimize-autoloader
```

**For Node.js/NPM:**
```bash
cd /var/www/myapp && \
npm ci --production
```

**For Python/pip:**
```bash
cd /var/www/myapp && \
pip install -r requirements.txt
```

### Run Database Migrations

Execute database migrations after deploying schema changes:

**For Laravel:**
```bash
cd /var/www/myapp && \
php artisan migrate --force
```

**For Django:**
```bash
cd /var/www/myapp && \
python manage.py migrate --noinput
```

**For Rails:**
```bash
cd /var/www/myapp && \
bundle exec rake db:migrate RAILS_ENV=production
```

### Clear Application Cache

Clear application and framework caches after deployment:

**For Laravel:**
```bash
cd /var/www/myapp && \
php artisan cache:clear && \
php artisan config:clear && \
php artisan route:clear && \
php artisan view:clear
```

**For Symfony:**
```bash
cd /var/www/myapp && \
php bin/console cache:clear --env=prod --no-debug
```

### Restart Application Services

Restart application workers, queues, or web servers:

```bash
cd /var/www/myapp && \
sudo systemctl restart php8.1-fpm && \
sudo systemctl restart myapp-worker && \
sudo systemctl restart myapp-scheduler
```

### Build Frontend Assets

Compile frontend assets after deploying new code:

```bash
cd /var/www/myapp && \
npm run build
```

### Disable Maintenance Mode

Take your application out of maintenance mode after successful deployment:

```bash
cd /var/www/myapp && \
php artisan up
```

### Warm Up Application Cache

Pre-populate caches for better performance after deployment:

**For Laravel:**
```bash
cd /var/www/myapp && \
php artisan config:cache && \
php artisan route:cache && \
php artisan view:cache
```

## Combined Deployment Workflow Example

Here's a complete example showing a typical deployment workflow with both pre and post-deployment commands:

### Pre-Deployment Commands

1. **Enable maintenance mode:**
   ```bash
   cd /var/www/myapp && php artisan down
   ```

2. **Backup database:**
   ```bash
   cd /var/www/myapp && mysqldump -u dbuser -p'password' mydb > /home/user/backups/db_$(date +%Y%m%d_%H%M%S).sql
   ```

### Post-Deployment Commands

1. **Install dependencies:**
   ```bash
   cd /var/www/myapp && composer install --no-dev --optimize-autoloader
   ```

2. **Run migrations:**
   ```bash
   cd /var/www/myapp && php artisan migrate --force
   ```

3. **Clear and warm cache:**
   ```bash
   cd /var/www/myapp && php artisan cache:clear && php artisan config:cache && php artisan route:cache
   ```

4. **Restart services:**
   ```bash
   sudo systemctl restart php8.1-fpm && sudo systemctl restart myapp-worker
   ```

5. **Disable maintenance mode:**
   ```bash
   cd /var/www/myapp && php artisan up
   ```

## Important Considerations

### Working Directory

Remember that SSH commands are executed from the user's home directory by default. Always use `cd` to change to your application directory at the start of each command:

```bash
cd /var/www/myapp && your-command-here
```

### Multi-Line Commands

When running multiple commands, use `&&` to chain them and ensure each command completes successfully before proceeding:

```bash
cd /var/www/myapp && \
composer install && \
php artisan migrate
```

### Error Handling

Enable the "Stop deployment if this command fails" option for critical commands. This is especially important for:
- Pre-deployment database backups
- Service shutdown commands
- Post-deployment migration commands

### Command Timeout

Configure appropriate timeouts for commands that may take longer to execute:
- Database migrations: 10-30 minutes depending on data volume
- Dependency installation: 5-15 minutes
- Asset compilation: 5-10 minutes
- Cache warming: 2-5 minutes

### Sudo Commands

If you need to run commands with elevated privileges, ensure your deployment user has the necessary sudo permissions. See the [Running Sudo SSH Commands](Article: #525) article for more information.

### Testing Commands

Always test your commands manually on the server before adding them to DeployHQ to ensure they work as expected and don't cause deployment failures.

## Framework-Specific Resources

For more framework-specific deployment command examples and best practices, consult your framework's deployment documentation:

- **Laravel:** [Laravel Deployment Documentation](https://laravel.com/docs/deployment)
- **Symfony:** [Symfony Deployment Documentation](https://symfony.com/doc/current/deployment.html)
- **Django:** [Django Deployment Checklist](https://docs.djangoproject.com/en/stable/howto/deployment/checklist/)
- **Rails:** [Rails Deployment Guide](https://guides.rubyonrails.org/deployment.html)

## Additional Resources

- [SSH Commands](Article: #57) - Detailed guide on configuring SSH commands
- [Running Sudo SSH Commands](Article: #525) - How to run commands with elevated privileges
- [Starting a Deployment](Article: #43) - Overview of the deployment process
