Skip to content
advanced Phase 98 · Cloud

Cloud Hooks Configuration

Cloud hooks configuration including build hooks, deploy hooks, and post-deploy hooks for Adobe Commerce Cloud

45m
0 problems
Topic Progress 0%

Build Hooks

Build Hook Configuration

# .magento.app.yaml
type: php:8.1
build:
    flavor: none

hooks:
    build: |
        set -e
        
        # Composer install
        composer install --prefer-dist --no-interaction
        
        # DI compilation
        php bin/magento setup:di:compile
        
        # Static content deployment
        php bin/magento setup:static-content:deploy -f
        
        # Generate dependency injection code
        php bin/magento setup:di:generate

Build Hook Best Practices

hooks:
    build: |
        set -e
        
        # 1. Install dependencies
        composer install --prefer-dist --no-interaction --no-dev
        
        # 2. Compile code (production)
        if [ "$MAGENTO_ENVIRONMENT" = "production" ]; then
            php bin/magento setup:di:compile
        fi
        
        # 3. Deploy static content
        php bin/magento setup:static-content:deploy -f --theme=Vendor/Theme
        
        # 4. Run tests (optional)
        # vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist

Build Hook Errors

# Common build errors and fixes

# 1. Memory limit
php -d memory_limit=4G bin/magento setup:di:compile

# 2. Missing extensions
php -m | grep -E 'intl|mbstring|gd|redis'

# 3. Composer cache
composer clear-cache
composer install --prefer-dist

Key Takeaway

Build hooks run during the build phase. Use them for composer install, DI compilation, and static content deployment. Include error handling with set -e.

Deploy Hooks

Deploy Hook Configuration

hooks:
    deploy: |
        set -e
        
        # Database setup
        php bin/magento setup:upgrade --keep-generated
        
        # Clear cache
        php bin/magento cache:clean
        php bin/magento cache:flush
        
        # Enable production mode
        php bin/magento deploy:mode:set production

Deploy Hook Best Practices

hooks:
    deploy: |
        set -e
        
        # 1. Maintenance mode (optional)
        php bin/magento maintenance:enable || true
        
        # 2. Database upgrade
        php bin/magento setup:upgrade --keep-generated
        
        # 3. Deploy static content (if not in build)
        # php bin/magento setup:static-content:deploy -f
        
        # 4. Clear cache
        php bin/magento cache:clean
        php bin/magento cache:flush
        
        # 5. Disable maintenance mode
        php bin/magento maintenance:disable || true

Deploy Hook with Notifications

hooks:
    deploy: |
        set -e
        
        # Deploy
        php bin/magento setup:upgrade --keep-generated
        php bin/magento cache:flush
        
        # Notify
        curl -X POST "$SLACK_WEBHOOK" \
            -d '{"text":"Deployment complete for '"$MAGENTO_ENVIRONMENT"'"}'

Key Takeaway

Deploy hooks run during deployment. Use for setup:upgrade, cache clearing, and mode switching. Always include error handling and consider maintenance mode.

Post-Deploy Hooks

Post-Deploy Hook Configuration

hooks:
    post_deploy: |
        set -e
        
        # Health check
        curl -f http://localhost/health || exit 1
        
        # Warm cache
        curl -s -o /dev/null https://www.example.com/
        curl -s -o /dev/null https://www.example.com/catalogsearch/
        
        # Reindex
        php bin/magento indexer:reindex

Post-Deploy Best Practices

hooks:
    post_deploy: |
        set -e
        
        # 1. Health check
        HTTP_STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://localhost/)
        if [ $HTTP_STATUS -ne 200 ]; then
            echo "Health check failed: HTTP $HTTP_STATUS"
            exit 1
        fi
        
        # 2. Reindex (non-blocking)
        php bin/magento indexer:reindex &
        
        # 3. Warm cache
        for url in \
            "https://www.example.com/" \
            "https://www.example.com/about-us" \
            "https://www.example.com/contact";
        do
            curl -s -o /dev/null "$url"
        done

Post-Deploy Monitoring

hooks:
    post_deploy: |
        set -e
        
        # Wait for services
        sleep 10
        
        # Health check
        curl -f http://localhost/health
        
        # Log deployment
        echo "Deployment completed at $(date)" >> /var/log/deployments.log
        
        # Send notification
        curl -X POST "$NOTIFICATION_WEBHOOK" \
            -d '{"status":"success","environment":"'"$MAGENTO_ENVIRONMENT"'"}'

Key Takeaway

Post-deploy hooks run after deployment. Use for health checks, cache warming, reindexing, and notifications. Keep them fast and non-blocking.

Hook Troubleshooting

Hook Execution Order

1. Build hooks (on build node)
   - composer install
   - DI compilation
   - Static content deployment

2. Deploy hooks (on each web node)
   - setup:upgrade
   - cache operations
   - mode switching

3. Post-deploy hooks (on first web node)
   - health checks
   - cache warming
   - notifications

Debugging Hooks

# View hook output
magento-cloud builds:log <build-id>
magento-cloud environment:log

# SSH and test hooks
magento-cloud ssh

# Test build hook manually
set -e
composer install --prefer-dist
php bin/magento setup:di:compile

# Test deploy hook manually
set -e
php bin/magento setup:upgrade --keep-generated
php bin/magento cache:flush

Common Hook Issues

# Issue: Memory limit
# Fix: Increase PHP memory in hook
php -d memory_limit=4G bin/magento setup:di:compile

# Issue: Timeout
# Fix: Optimize commands or split into smaller tasks

# Issue: Missing extensions
# Fix: Add extension installation to build hook
apt-get install -y php8.1-intl php8.1-mbstring

# Issue: Permission errors
# Fix: Ensure proper file ownership
chown -R www-data:www-data pub/ var/ generated/

Hook Optimization

hooks:
    build: |
        set -e
        # Cache composer dependencies
        composer install --prefer-dist --no-interaction --optimize-autoloader
        
        # Parallel static content deployment
        php bin/magento setup:static-content:deploy -f -j4
    deploy: |
        set -e
        # Skip DI compile if already done in build
        php bin/magento setup:upgrade --keep-generated
        php bin/magento cache:flush
    post_deploy: |
        set -e
        # Quick health check only
        curl -f http://localhost/health

Key Takeaway

Understand hook execution order. Debug with logs and SSH. Common issues: memory, timeouts, permissions. Optimize by caching and parallelizing.

Quiz

1. What runs in build hooks?

Question 1 options

2. What is the deploy hook used for?

Question 2 options

3. When do post-deploy hooks run?

Question 3 options

4. What does set -e do in hooks?

Question 4 options

5. How to debug hook issues?

Question 5 options

Flashcards

Question

What do build hooks do?

Answer

Composer install, DI compilation, static content deployment during build phase

Question

What do deploy hooks do?

Answer

Database setup, cache operations, mode switching during deployment

Question

What do post-deploy hooks do?

Answer

Health checks, cache warming, notifications after deployment

Question

What does set -e do?

Answer

Exit immediately on first error to prevent partial deployments

Question

How to view hook output?

Answer

magento-cloud builds:log and environment:log

Question

Hook execution order?

Answer

Build -> Deploy -> Post-deploy

Revision Notes

Key Takeaways

  • 1. Build hooks: composer install, DI compilation, static content
  • 2. Deploy hooks: setup:upgrade, cache operations, mode switching
  • 3. Post-deploy hooks: health checks, cache warming, notifications
  • 4. Use set -e for error handling
  • 5. Debug with logs and SSH access

Interview Tips

  • Explain hook execution order and purpose
  • Discuss build hook optimization
  • Describe deploy hook best practices
  • Explain post-deploy verification

Cheat Sheet

Cloud Hooks

Build Hooks:

  • composer install
  • setup:di:compile
  • setup:static-content:deploy

Deploy Hooks:

  • setup:upgrade
  • cache:clean/flush
  • deploy:mode:set

Post-Deploy Hooks:

  • Health check
  • Cache warming
  • Notifications

Debug:
magento-cloud builds:log
magento-cloud environment:log
magento-cloud ssh