DEV Community

vimuth
vimuth

Posted on

Automate Laravel Deployment on Hostinger with GitHub Actions: A Step-by-Step Guide

Previously I added a blog on deploying a laravel application on Hostinger. This is an extension on this.

This is the previous article. - Effortless Laravel Deployment on Hostinger Using Git: A Step-by-Step Guide

GitHub Actions is a powerful automation tool that allows you to streamline your development workflow. When you push a change to a GitHub repository branch, GitHub can automatically trigger a workflow to deploy that branch to your server. This seamless process ensures continuous integration and deployment without manual intervention.

Creating a Deployment Script for GitHub Actions

Let's create a deploy.sh file and add all the necessary laravel related commands here.

First login via SSH and go to your site location

cd /home/youruser/domains/yourdomain.hostingersite.com
Enter fullscreen mode Exit fullscreen mode

Then create deploy.sh file.

nano deploy.sh
Enter fullscreen mode Exit fullscreen mode

Add this to file

#!/bin/bash

# Define a function to check the status of the last executed command
check_status() {
    if [ $? -eq 0 ]; then
        echo "✅ $1 succeeded."
    else
        echo "❌ $1 failed."
        exit 1
    fi
}

composer2 install --no-interaction
check_status "Composer Install"

npm install
check_status "NPM Install"

npm run build
check_status "NPM Run Build"

# Run migrations
php artisan migrate --force
check_status "PHP Artisan Migrate"

# Run database seeders
php artisan db:seed --force
check_status "PHP Artisan DB Seed"

# Clear caches
php artisan config:clear
check_status "PHP Artisan Config Clear"

php artisan cache:clear
check_status "PHP Artisan Cache Clear"

php artisan view:clear
check_status "PHP Artisan View Clear"

php artisan route:clear
check_status "PHP Artisan Route Clear"

# Rebuild caches
php artisan config:cache
check_status "PHP Artisan Config Cache"

php artisan route:cache
check_status "PHP Artisan Route Cache"

php artisan view:cache
check_status "PHP Artisan View Cache"

echo "✅ Deployment complete."
Enter fullscreen mode Exit fullscreen mode

You will see all the Laravel commands here.

Create the GitHub Actions workflow file

Let's create a GitHub Actions workflow file that automates the deployment of your project to a Hostinger server whenever changes are pushed to a particular branch

Add this file to you github repo .github\workflows\maindeploy.yml. And add this content

name: Deploy to Hostinger Server

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Install sshpass (if using password authentication)
      run: sudo apt-get install -y sshpass

    - name: Deploy to Hostinger via SSH with custom port
      env:
        HOSTINGER_IP: ${{ secrets.HOSTINGER_IP }}
        HOSTINGER_PORT: ${{ secrets.HOSTINGER_PORT }}
        HOSTINGER_USERNAME: ${{ secrets.HOSTINGER_USERNAME }}
        HOSTINGER_PASSWORD: ${{ secrets.HOSTINGER_PASSWORD }}
      run: |
        # Specify custom port with -P for scp
        sshpass -p "$HOSTINGER_PASSWORD" scp -P $HOSTINGER_PORT -o StrictHostKeyChecking=no -r * $HOSTINGER_USERNAME@$HOSTINGER_IP:/home/youruser/domains/yourdomain.hostingersite.com

        # Specify custom port with -p for ssh
        sshpass -p "$HOSTINGER_PASSWORD" ssh -p $HOSTINGER_PORT -o StrictHostKeyChecking=no $HOSTINGER_USERNAME@$HOSTINGER_IP "cd /home/youruser/domains/yourdomain.hostingersite.com && bash deploy.sh"
Enter fullscreen mode Exit fullscreen mode

You can see "/home/youruser/domains/yourdomain.hostingersite.com" in two places. You must change this to your actual location. More about this you can find in first video.

And remember that here you are using default main branch. if you need change the branch here

on:
  push:
    branches:
      - main
Enter fullscreen mode Exit fullscreen mode

Add repository secret values

You can see this section

env:
        HOSTINGER_IP: ${{ secrets.HOSTINGER_IP }}
        HOSTINGER_PORT: ${{ secrets.HOSTINGER_PORT }}
        HOSTINGER_USERNAME: ${{ secrets.HOSTINGER_USERNAME }}
        HOSTINGER_PASSWORD: ${{ secrets.HOSTINGER_PASSWORD }}

Enter fullscreen mode Exit fullscreen mode

You must add these values as github repository secrets. Let me show how. First go to repository and got to Settings -> Secrets and variables -> Actions and you can add these values.

Image description

That's it and when you see the actions tab you can see the action status.

Image description

Thanks and that's all you have to do.

Top comments (0)