Files
portal-api/.github/workflows
2025-07-28 15:34:15 +07:00
..
2025-07-28 15:34:15 +07:00
2025-07-28 15:34:15 +07:00
2025-07-28 15:34:15 +07:00

GitHub Actions Deployment Workflows

Các workflow tự động deploy được tạo cho project Laravel Portal.

Workflows Available

1. deploy.yml - Deployment thông thường

  • Trigger: Push vào branches main, master, develop hoặc manual trigger
  • Mục đích: Deploy cho staging/development environment
  • Actions: Git pull, npm run production, Laravel optimization

2. deploy-minified.yml - Production Deployment với minification

  • Trigger: Push vào branches main, master hoặc manual trigger
  • Mục đích: Deploy cho production environment
  • Actions: Git pull, npm run minify-all, database migration, backup, cleanup

Setup Requirements

1. GitHub Secrets Configuration

Cần setup các secrets sau trong GitHub repository:

For deploy.yml:

HOST                - IP address hoặc domain của server
USERNAME            - SSH username
SSH_KEY             - SSH private key content
PORT                - SSH port (optional, default: 22)
PROJECT_PATH        - Đường dẫn tới project (optional, default: /var/www/portal)

For deploy-minified.yml:

PROD_HOST           - Production server IP/domain
PROD_USERNAME       - Production SSH username  
PROD_SSH_KEY        - Production SSH private key
PROD_PORT           - Production SSH port (optional, default: 22)
PROD_PROJECT_PATH   - Production project path (optional, default: /var/www/portal)

2. Server Requirements

Server cần có sẵn:

  • Git
  • PHP >= 8.0
  • Composer
  • Node.js & NPM
  • Web server (nginx/apache) với quyền www-data

Project directory structure:

/var/www/portal/     # Main project
/var/www/portal-backup-*  # Backup directories (auto-created)

3. SSH Key Setup

  1. Generate SSH key pair:

    ssh-keygen -t rsa -b 4096 -C "github-actions@yourdomain.com"
    
  2. Add public key to server:

    # Copy public key to server
    cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
    
  3. Add private key to GitHub Secrets:

    • Copy nội dung của private key file
    • Paste vào GitHub Secrets với tên SSH_KEY hoặc PROD_SSH_KEY

Manual Deployment

Có thể trigger deployment manually:

  1. Vào GitHub repository
  2. Click tab "Actions"
  3. Chọn workflow muốn chạy
  4. Click "Run workflow"
  5. Chọn branch và click "Run workflow"

Workflow Features

Security Features:

  • SSH key authentication
  • Separate production secrets
  • Backup before deployment

Performance Features:

  • Asset minification cho production
  • Laravel optimization commands
  • Cache clearing/rebuilding

Maintenance Features:

  • Automatic backup rotation (keep last 5)
  • Queue restart
  • Permission fixing
  • Database migration

Troubleshooting

Common Issues:

  1. SSH Connection Failed:

    • Kiểm tra HOST, USERNAME, SSH_KEY secrets
    • Verify SSH key format (complete private key including headers)
  2. Permission Denied:

    • Kiểm tra SSH user có quyền access project directory
    • Verify www-data ownership
  3. Build Failed:

    • Kiểm tra Node.js version trên server
    • Verify npm dependencies
  4. Laravel Commands Failed:

    • Kiểm tra PHP version compatibility
    • Verify .env file exists và correct

Debug Commands:

Để debug, có thể thêm các commands sau vào workflow:

# Add to workflow script section
echo "Current user: $(whoami)"
echo "Current directory: $(pwd)"
echo "PHP version: $(php -v)"
echo "Node version: $(node -v)"
echo "NPM version: $(npm -v)"

Environment-specific Customization

Có thể customize workflows theo environment:

Development:

  • Sử dụng npm run dev thay vì production
  • Skip database migrations
  • Enable debug mode

Staging:

  • Sử dụng npm run production
  • Include database migrations
  • Moderate optimization

Production:

  • Sử dụng npm run minify-all
  • Full optimization
  • Backup và cleanup
  • Queue restart