Multi-Stage Deployment Pipeline in Laravel 11 with GitHub Actions

Websolutionstuff | Oct-18-2024 | Categories : Laravel

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

Multi-Stage Deployment Pipeline in Laravel 11 with GitHub Actions

 

Step 1: Prepare Your Laravel 11 Project

Before setting up GitHub Actions, make sure your Laravel 11 project is ready for deployment:

  • Ensure .env files for staging and production environments are set up.
  • Your database configurations for both environments should be in place

 

Step 2: Add workflow Directory in Your Repository

Inside your Laravel project's root, create a .github/workflows directory. This is where all your GitHub Actions workflows will live.

mkdir -p .github/workflows

 

Step 3: Create a GitHub Actions Workflow File

Create a 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'

 

Step 4: Explanation of Workflow Job
  • Testing: On each push to the main or staging branch, it runs the tests in your Laravel project using php artisan test.
  • Build: After testing, it installs the dependencies and compiles assets.
  • Deploy to Staging: If the code is pushed to the staging branch, it will automatically deploy to the staging server using SSH.
  • Deploy to Production: If the code is pushed to the main branch, the deployment to the production server happens.
  • Rollback: If the deployment fails, this step will trigger and rollback the code to the previous state using git reset --hard.

 

Step 5: Setting Up GitHub Secrets

To securely connect to your servers, store sensitive information like SSH keys in GitHub Secrets:

  • Go to your GitHub repository settings.
  • Add STAGING_SERVER and PRODUCTION_SERVER secrets containing your SSH login details.

 

Step 6: Configure Environment-Specific Settings

In your Laravel config/deploy.php, you can customize the deployment script to handle environment-specific configurations for both staging and production.

 

Step 7: Testing the Pipeline

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:

Recommended Post
Featured Post
Install and Use Trix Editor in Laravel 11
Install and Use Trix Editor in...

Hello, web developers! In this article, I will show you how to install and use Trix Editor in a Laravel 11 application,...

Read More

Aug-28-2024

Laravel 9 Multiple Where Condition Query Example
Laravel 9 Multiple Where Condi...

In this article, we will see the laravel 9 and laravel 10 multiple where condition query example. We will learn&nbs...

Read More

Sep-30-2022

How to Add Bootstrap 5 in Angular 17 Application
How to Add Bootstrap 5 in Angu...

Welcome to this tutorial where I'll guide you through the process of integrating Bootstrap 5 into your Angular 17 ap...

Read More

Mar-25-2024

How To Reset Modal Form In jQuery
How To Reset Modal Form In jQu...

Have you ever seen those pop-up boxes on websites? They're called modal forms, and they make it easier to do things...

Read More

Jan-04-2023