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
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'
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, laravel web developers! In this article, we'll see how to add toastr notification in livewire laravel 11. Her...
May-31-2024
In this article we will see jquery appendTo() and prependTo example. The appendTo() method inserts HTML elements at...
Dec-13-2021
In this article, we will see laravel 9 user roles and permissions without package. Roles and permissions are an imp...
Apr-14-2022
In today's digital age, where security is paramount, user authentication has become a cornerstone concern for web ap...
Aug-21-2023