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.

Google AI Education track image

Work through these 3 parts to earn the exclusive Google AI Studio Builder badge!

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more →

Top comments (0)

Sentry image

👀 Watch Us Break (and Fix) a Laravel App with Sentry

In this on-demand workshop, we push a Laravel + React app to the edge—then show exactly how to debug it using Sentry. From error tracking to Session Replays and full-stack performance monitoring, it's all here. Bonus: watch AI bots pitch in to clean up the mess.

🎥 Watch now + follow along

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay