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
Laravel 11 Livewire Toastr Notification
Laravel 11 Livewire Toastr Not...

Hello, laravel web developers! In this article, we'll see how to add toastr notification in livewire laravel 11. Her...

Read More

May-31-2024

Jquery appendTo And prependTo Example
Jquery appendTo And prependTo...

In this article we will see jquery appendTo() and prependTo example. The appendTo() method inserts HTML elements at...

Read More

Dec-13-2021

Laravel 9 User Roles and Permissions Without Package
Laravel 9 User Roles and Permi...

In this article, we will see laravel 9 user roles and permissions without package. Roles and permissions are an imp...

Read More

Apr-14-2022

How To Login With OTP In Laravel 10
How To Login With OTP In Larav...

In today's digital age, where security is paramount, user authentication has become a cornerstone concern for web ap...

Read More

Aug-21-2023