Laravel Continuous Deployment with Github Actions

Automatically deploy your laravel application on Cpanel.

GitHub Actions makes it easy to automate all your software workflows, Build, test, and deploy your code right from GitHub. Yiis, no need to use FTP or manually using SSH to git pull on server.

Think about github actions like cron jobs on your server. Cron jobs are scripts that run on your server on scheduled time, However github actions run when there is a new action in your github repository eg: new commit, merge etc.

Here’s what we’ll achieve if you just follow along this tutorial. When you commit a new change on your master branch, All changed files will be uploaded to your site (EXAMPLE.COM)

All github action workflow lives in /.github/workflows/main.yml (Folders needs to be named exactly like this.)

Let’s look at an example workflow file:

name: Build and Deploy
on:
  push:
    branches:
      -   master

jobs:
  build:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Call deploy script
        uses: garygrossgarten/github-action-ssh@release
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          privateKey: ${{ secrets.SSH_KEY }}
          command: cd /var/www/staging.example.com && ./server_deploy.sh

Explainer: Action explainer

You will find community built Action Workflows for many common task.

Each action comes with a few options that you can modify. You can pass options to the workflow by with key.

For our example we are passing SSH HOST, KEY, COMMAND to our workflow so it can SSH to our application serever.

You may wonder what are these ${{ secrets.SSH_HOST}}?

These are Encrypted Secrets. You definitely don’t want to save your server’s password in a plain text in your github repository. That’s why you save any sensitive information as github secret.

Voila. We’re done.

Now you just have to create server_deploy.sh on your project directory. Which will actually pull & deploy the application.

#!/bin/sh
set -e

echo "Deploying application ..."

# Enter maintenance mode
(php artisan down --message 'The app is being (quickly!) updated. Please try again in a minute.') || true

    git pull origin master

    composer install --no-interaction --prefer-dist --optimize-autoloader

    php artisan migrate --force

    php artisan optimize

# Exit maintenance mode
php artisan up

echo "Application deployed!"

Now whenever you push to master branch of your repository, Github actions will SSH to your server & try to run server_deploy.sh script.