Github Actions | Automatic Git Push Deployment with cPanel | Laravel Pipeline

You are currently viewing Github Actions | Automatic Git Push Deployment with cPanel | Laravel Pipeline

This guide walks you through setting up a CI/CD pipeline using GitHub Actions for a Laravel project, enabling automatic deployment to a cPanel server.


๐Ÿš€ What is CI/CD?

โœ… Continuous Integration (CI)

Continuous Integration automates the process of integrating code changes into a shared repository. It builds, tests, and validates the code, allowing developers to detect bugs early, improve code quality, and speed up development cycles.

โœ… Continuous Deployment (CD)

Continuous Deployment automates the release process. After successful CI validation, the code is automatically deployed to production or staging environments. This reduces manual steps, minimizes human error, and ensures faster delivery of updates.

โœ… CI/CD Pipeline

A CI/CD pipeline consists of:

  • Triggers (e.g., code push, pull request)
  • Actions (e.g., testing, building, deploying)

Common tools for CI/CD include GitHub Actions, GitLab CI/CD, and Jenkins. In this guide, we’ll focus on GitHub Actions.


๐Ÿ› ๏ธ Setting Up CI/CD for Laravel with GitHub Actions

1๏ธโƒฃ Generating SSH Keys for GitHub and cPanel

  1. SSH into your server:
    ssh root@your_server_ip
    
  2. Generate SSH keys:
    ssh-keygen
    

    (Press enter for default options)

  3. Copy the public key:
    cat ~/.ssh/id_rsa.pub
    
  4. Add the public key to the server’s authorized keys:
    sudo vim ~/.ssh/authorized_keys
    

2๏ธโƒฃ Adding SSH Keys and Secrets to GitHub

  1. Go to GitHub โ†’ Settings โ†’ SSH and GPG keys โ†’ New SSH key
    • Title: Your Server
    • Key: Paste the public key
  2. Add repository secrets:
    • SSH_HOST โ†’ Your server IP
    • SSH_USERNAME โ†’ Your SSH username
    • SSH_KEY โ†’ Your private SSH key (cat ~/.ssh/id_rsa)

โšก Configuring the GitHub Actions Workflow

  1. Create workflow folders:
    mkdir -p .github/workflows
    
  2. Create a deploy.yml file:
name: Laravel CI/CD

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.3'

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Install Composer Dependencies
        run: composer install --no-dev --optimize-autoloader

      - name: Install Node Dependencies
        run: npm ci

      - name: Build Assets
        run: npm run build

      - name: Run Laravel Commands
        run: |
          cp .env.example .env
          php artisan key:generate
          php artisan config:cache
          php artisan migrate --force

      - name: Set Directory Permissions
        run: chmod -R 755 storage bootstrap/cache

      - name: Run Unit Tests
        run: php artisan test

      - name: Deploy to Server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            cd /path/to/your/project
            git pull origin main
            composer install --no-dev --optimize-autoloader
            npm ci
            npm run build
            php artisan migrate --force

๐Ÿงพ Explanation of Workflow Steps

  • Trigger: Runs on push to the main branch.
  • Setup: PHP 8.3, Node.js 16, and installs dependencies.
  • Build & Cache: Compiles assets, clears caches, and generates keys.
  • Tests: Runs Laravel unit tests.
  • Deploy: SSHs into the server, pulls code, installs dependencies, and runs migrations.

๐Ÿ’ก Tip: If you’re using Laravel Mix instead of Vite, replace npm run build with npm run prod.


๐ŸŽ‰ Conclusion

With this CI/CD pipeline, your Laravel project will automatically deploy to your cPanel server whenever changes are pushed to the main branch. This ensures faster, more reliable deployments and allows you to focus on building great features instead of managing deployments manually.

Happy Coding! ๐Ÿ’ปโœจ

Leave a Reply