In this guide, I'll walk you through setting up a multi-stage CI/CD deployment pipeline for your Laravel 11 project using GitHub Actions. We'll automate testing, building, and deploying to both staging and production environments, ensuring a smooth workflow.
I'll also cover rollback strategies to quickly revert in case something goes wrong. By the end, you'll have a robust pipeline that makes your Laravel app deployments more efficient and reliable.
Multi-Stage Deployment Pipeline in Laravel 11 with GitHub Actions
Before setting up GitHub Actions, make sure your Laravel 11 project is ready for deployment:
workflow
Directory in Your RepositoryInside your Laravel project's root, create a .github/workflows
directory. This is where all your GitHub Actions workflows will live.
mkdir -p .github/workflows
deploy.yml
file inside the .github/workflows
directory.
name: Laravel Multi-Stage Deployment Pipeline
on:
push:
branches:
- main
- staging
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run tests
run: php artisan test
build:
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Compile assets
run: npm ci && npm run production
deploy-staging:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/staging'
steps:
- name: Deploy to Staging
run: |
ssh ${{ secrets.STAGING_SERVER }} 'cd /path/to/your/staging && git pull origin staging && composer install && php artisan migrate --force'
deploy-production:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to Production
run: |
ssh ${{ secrets.PRODUCTION_SERVER }} 'cd /path/to/your/production && git pull origin main && composer install && php artisan migrate --force'
rollback:
runs-on: ubuntu-latest
if: failure()
steps:
- name: Rollback on failure
run: |
ssh ${{ secrets.PRODUCTION_SERVER }} 'cd /path/to/your/production && git reset --hard HEAD~1'
main
or staging
branch, it runs the tests in your Laravel project using php artisan test
.staging
branch, it will automatically deploy to the staging server using SSH.main
branch, the deployment to the production server happens.git reset --hard
.
To securely connect to your servers, store sensitive information like SSH keys in GitHub Secrets:
STAGING_SERVER
and PRODUCTION_SERVER
secrets containing your SSH login details.
In your Laravel config/deploy.php
, you can customize the deployment script to handle environment-specific configurations for both staging and production.
Push changes to the staging
branch and see if they trigger the CI/CD pipeline. You can also push to main
to check the production deployment flow.
You might also like:
Hello, web developers! In this article, I will show you how to install and use Trix Editor in a Laravel 11 application,...
Aug-28-2024
In this article, we will see the laravel 9 and laravel 10 multiple where condition query example. We will learn&nbs...
Sep-30-2022
Welcome to this tutorial where I'll guide you through the process of integrating Bootstrap 5 into your Angular 17 ap...
Mar-25-2024
Have you ever seen those pop-up boxes on websites? They're called modal forms, and they make it easier to do things...
Jan-04-2023