October, 17 2023

How To Set Up Auto Deploy For Laravel Applications On CPanel Using GitHub Action

Lately I have been experimenting on how to improve the CPanel experience for Laravel developers and luckly I have been successful with my attemps. This article is one of my many cPanel experiments.

I already wrote an article on how to set up auto deploy but this time we will learn how to do it using GitHub Actions.

How To Connect CPanel To GitHub

First, We need to ensure that our cPanel has SSH enabled and connected to our GitHub account. This will enable us to clone our private repositories.

Read more: How To Connect Cpanel To GitHub, Gitlab, And Bitbucket And Deploy Private Repositories

Initial Laravel Deployment On CPanel

Next, we need to set up our Laravel application on Cpanel. We also have to set up the application database, smtp and other credentials for our production environment and ensure our CPanel is serving the Laravel public directory.

Read more: How To Deploy A Laravel Project On Linux Shared Hosting In 5 Minutes

Setup Deployment Bash Script

  • Create a .github/workflows directory in the application base directory if this directory does not already exist.
  • Create a deploy.sh bash script inside the .github/workflow directory.
  • Copy the following BASH contents into the deploy.sh:

Replace /path/to/project with the path your Laravel application on CPanel.

#!/bin/sh

# Change to the project directory. 
cd ~/path/to/project

# Pull the latest changes from the git repository
git pull origin main

# Install/update composer dependencies
composer install --no-interaction

# Run database migrations
php artisan migrate --force

# Clear caches
php artisan cache:clear

# Clear and cache routes
php artisan route:cache

# Clear and cache config
php artisan config:cache

# Clear and cache views
php artisan view:cache

This script above contains all the necessary commands that runs everytime a new code is deployed. We can always customize this script based on our project need.

Setup GitHub Action Workflow

  • Create a .github/workflows directory in the app repository on GitHub if this directory does not already exist.
  • Create a file named deploy.yml inside the .github/workflows directory.
  • Copy the following YAML contents into the deploy.yml:

Replace cpanel_username with your CPanel username and project_dir with your deployed application directory on CPanel.

name: Deploy
on:
  push:
  pull_request:
jobs:
  tests:
    runs-on: ubuntu-latest
    name: Build & Deploy Assets
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup SSH
        run: |
          mkdir -p ~/.ssh/
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
          echo "${{ secrets.SSH_PUBLIC_KEY }}" > ~/.ssh/id_rsa.pub
          chmod 600 ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa.pub
          ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts

      - name: Deploy
        run: |
          ssh syncflux@${{ secrets.SERVER_IP }} 'bash -s' < ${{ github.workspace }}/.github/workflows/deploy.sh

The Setup SSH section of our workflow connects our GitHub Action instance to our cPanel.

The Deploy section of our workflow executes our deployment bash script on CPanel server via the SSH.

Create GitHub Action Secrets

  • Private and public keys can be found in the ~/.ssh directory of the CPanel server.
  • On GitHub.com, navigate to the settings page of the app repository.
  • In the "Security" section of the sidebar, select Secrets and variables, then click Actions.
  • Click New repository secret and create SSH_PRIVATE_KEY, SSH_PUBLIC_KEY and SERVER_IP secrets respectively.

GitHub Secrets

With this setup, our Laravel project will be deployed on CPanel whenever our GitHub actions runs successfully.

GitHub Action

Bonus

If you also have frontend assets that needs to be compiled and deployed on cPanel, you can do that also with GitHub Actions.

Read more: Asset Bundling For Laravel Applications On CPanel Using GitHub Action

Join my inner circle newsletter

Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here

Hey, have you tried Litehost lately ?

Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now