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
- SSH into your server:
ssh root@your_server_ip
- Generate SSH keys:
ssh-keygen
(Press enter for default options)
- Copy the public key:
cat ~/.ssh/id_rsa.pub
- Add the public key to the server’s authorized keys:
sudo vim ~/.ssh/authorized_keys
2๏ธโฃ Adding SSH Keys and Secrets to GitHub
- Go to GitHub โ Settings โ SSH and GPG keys โ New SSH key
- Title: Your Server
- Key: Paste the public key
- 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
- Create workflow folders:
mkdir -p .github/workflows
- 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
withnpm 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! ๐ปโจ