DEV Community

JOHN MWACHARO
JOHN MWACHARO

Posted on

๐Ÿš€ Automating Laravel Deployment to DigitalOcean with GitHub Actions

When building with Laravel, deploying changes from your local machine to your production server shouldnโ€™t be a chore. In this post, Iโ€™ll show you how to automate Laravel deployments to a DigitalOcean VPS using GitHub Actions โ€” so that every push to GitHub updates your live app. Fast, clean, and fully hands-free.

โœ… Why Use GitHub Actions for Deployment?
๐Ÿ” Automated: Every push triggers a deployment.

๐Ÿง  Smart: Only deploys on specific branches.

๐Ÿ” Secure: Uses SSH with no manual login.

๐Ÿšซ No third-party CI/CD platforms needed.

๐Ÿ”ง Prerequisites
Before diving in, ensure:

You have a Laravel project hosted on GitHub.

Your production server (DigitalOcean) is running Ubuntu, PHP, Composer, and Laravel dependencies.

SSH access to the server (weโ€™ll use this for GitHub Actions).

1๏ธโƒฃ Generate SSH Key for GitHub
From your local machine:

ssh-keygen -t ed25519 -C "github_actions_deploy"
When prompted for a filename, use:

/home/engineer/.ssh/solssa_github_ci
This creates:

solssa_github_ci โ†’ Private key

solssa_github_ci.pub โ†’ Public key

2๏ธโƒฃ Add the Public Key to Your Server
Upload the public key to your DigitalOcean server:

ssh-copy-id -i ~/.ssh/solssa_github_ci.pub root@159.89.41.188
Test the connection:

ssh -i ~/.ssh/solssa_github_ci root@159.89.41.188
You should log in without a password.

3๏ธโƒฃ Add Private Key to GitHub Secrets
On your GitHub repo (SolssaTemplate), go to:

Settings > Secrets and variables > Actions
Create these 3 secrets:

Name Value
DO_SSH_KEY Contents of solssa_github_ci
DO_HOST 159.89.41.188
DO_USER root

4๏ธโƒฃ Create the GitHub Actions Workflow
In your Laravel project, create:

.github/workflows/deploy.yml
Paste this:

yaml
Copy
Edit
name: Deploy to DigitalOcean

on:
push:
branches:
- main # or 'master'

jobs:
deploy:
runs-on: ubuntu-latest

steps:
  - name: Checkout code
    uses: actions/checkout@v2

  - name: Deploy to DigitalOcean via SSH
    uses: appleboy/ssh-action@v1.0.0
    with:
      host: ${{ secrets.DO_HOST }}
      username: ${{ secrets.DO_USER }}
      key: ${{ secrets.DO_SSH_KEY }}
      script: |
        cd /var/www/solssa
        git pull origin main
        composer install --no-dev --optimize-autoloader
        php artisan migrate --force
        php artisan config:cache
        php artisan route:cache
        php artisan view:cache
Enter fullscreen mode Exit fullscreen mode

โ˜๏ธ Update /var/www/solssa to your actual Laravel app directory on the server.

5๏ธโƒฃ Trigger Your First Deploy
Push any change to your GitHub main branch:

git add .
git commit -m "Automated deploy test"
git push origin main
Then visit the Actions tab on your repo to watch the magic happen. ๐ŸŽ‰

โœจ Conclusion
By setting up this simple GitHub Actions workflow, youโ€™ve turned deployment into a single push operation โ€” no FTP, no SSH copy-pasting, just clean DevOps right inside GitHub.

This setup is perfect for small to mid-size Laravel apps and gives you a solid foundation to scale with more advanced CI/CD in the future.

Top comments (0)