How to Set Up CI/CD in Laravel: Step by Step Guide

Hey there! If you’re a Laravel developer like me, you’ve probably spent hours manually testing and deploying your code. It’s tedious, right? That’s where CI/CD (Continuous Integration and Continuous Deployment) comes in.

It automates your testing and deployment process, saving you time and reducing errors. In this article, I’ll share how I set up a CI/CD pipeline for my Laravel projects using GitHub Actions.

Step-by-Step Guide to Set Up CI/CD in Laravel

How to Set Up CI/CD in Laravel: Step by Step Guide

Here’s how I set up a CI/CD pipeline for my Laravel project. I’m using GitHub Actions because it’s free, easy to use, and integrates well with GitHub, where most developers host their code.

Step 1: Set Up Your Laravel Project

First, make sure your Laravel project is ready. I assume you already have a Laravel app set up and pushed to a GitHub repository. If not, create a new Laravel project using Composer:

composer create-project laravel/laravel my-laravel-app
cd my-laravel-app
git init
git add .
git commit -m "Initial commit"
git remote add origin <your-github-repo-url>
git push -u origin main

Your project should have unit tests (you can create them using php artisan make:test) to ensure CI/CD can run them automatically.

Step 2: Create a GitHub Repository

If you haven’t already, create a GitHub repository for your project. Go to GitHub, click “New Repository,” name it (e.g., my-laravel-app), and push your code. This is where GitHub Actions will run your CI/CD pipeline.

Step 3: Set Up GitHub Actions

GitHub Actions is a powerful tool for automating workflows. To set it up:

  1. In your Laravel project’s root directory, create a folder named .github/workflows.
  2. Inside it, create a file called ci-cd.yml. This file defines your CI/CD pipeline.

Here’s a simple GitHub Actions workflow I use for Laravel:

name: Laravel CI/CD

on:
  push:
    branches:
      - main

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.1'
          extensions: mbstring, xml, ctype, json, tokenizer, pdo_mysql

      - name: Install Composer dependencies
        run: composer install --prefer-dist --no-progress

      - name: Copy .env file
        run: cp .env.example .env

      - name: Generate application key
        run: php artisan key:generate

      - name: Run tests
        run: php artisan test

  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'

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

      - name: Deploy to server
        env:
          DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
        run: |
          echo "$DEPLOY_SSH_KEY" > deploy_key
          chmod 600 deploy_key
          ssh -i deploy_key -o StrictHostKeyChecking=no user@your-server-ip "cd /var/www/your-app && git pull origin main && composer install --no-dev && php artisan migrate --force && php artisan optimize"

Step 4: Configure Your Server

You need a server to deploy your Laravel app. I use a VPS (like DigitalOcean or AWS). Make sure your server has PHP, Composer, and your database (e.g., MySQL) installed. Here’s what I do:

  1. Set up SSH: Generate an SSH key pair on your local machine (ssh-keygen -t rsa), add the public key to your server’s ~/.ssh/authorized_keys, and keep the private key secure.
  2. Add SSH key to GitHub: Go to your GitHub repository > Settings > Secrets and variables > Actions > New repository secret. Name it DEPLOY_SSH_KEY and paste your private SSH key.
  3. Prepare your server: On your server, clone your repository to /var/www/your-app and set up your env file with database credentials.

Step 5: Test the Pipeline

Push a change to your main branch:

git add .
git commit -m "Test CI/CD pipeline"
git push origin main

Go to your GitHub repository > Actions tab. You’ll see your workflow running. The test job runs your Laravel tests, and if they pass, the deploy job pushes your code to the server and runs necessary commands.

Step 6: Monitor and Debug

If your pipeline fails, check the logs in the Actions tab. Common issues include missing dependencies or incorrect SSH keys. I always double-check my env file and server permissions if something goes wrong.

Conclusion

Setting up CI/CD for my Laravel project was a game-changer. It saves me hours of manual work and ensures my code is tested and deployed reliably. By using GitHub Actions, I automated my testing and deployment process with just a few steps. Now, every time I push code to my main branch, my app is tested and deployed automatically. Give it a try, and you’ll love how much time it saves!

Frequently Asked Questions

Q: What is CI/CD in Laravel?
A: CI/CD stands for Continuous Integration and Continuous Deployment. In Laravel, it automates testing your code (CI) and deploying it to a server (CD) whenever you push changes.

Q: Why use GitHub Actions for Laravel CI/CD?
A: GitHub Actions is free, integrates with GitHub, and is easy to configure for Laravel projects. It supports PHP and Composer, making it a great choice.

Q: Do I need a server for CI/CD?
A: Yes, for deployment, you need a server (like a VPS) where your Laravel app will run. For testing, GitHub Actions handles everything in the cloud.

Q: What if my tests fail in the pipeline?
A: Check the error logs in the GitHub Actions tab. Common issues include missing PHP extensions, incorrect env settings, or failing tests. Fix the issue and push again.

Q: Can I use other CI/CD tools with Laravel?
A: Yes! Tools like Jenkins, CircleCI, or GitLab CI/CD also work with Laravel, but GitHub Actions is beginner-friendly and widely used.


You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS