How to implement CI/CD in Laravel: A practical guide

Updated: January 16, 2024 By: Guest Contributor Post a comment

Overview

In the world of software development, CI/CD (Continuous Integration/Continuous Deployment) is essential for constant iteration and improvement of code. For Laravel, one of the most popular PHP frameworks, implementing CI/CD can significantly streamline the development process, ensuring all new code commits result in the automatic and reliable rollout of changes. In this guide, we will walk through the steps to set up a CI/CD pipeline for a Laravel application from scratch.

Why CI/CD?

CI/CD bridges the gaps between development and operations teams by automating the building, testing, and deployment processes. Good practices in CI/CD can dramatically reduce the time required to integrate changes for a release and can allow for releases to occur more frequently.

In this tutorial, we will delve into:

  • Setting up a version control system with Git and GitHub.
  • Creating a testing environment with PHPUnit.
  • Configuring a continuous integration service using GitHub Actions.
  • Automating deployment to a live server.

Prerequisites

  • A Laravel application
  • A GitHub account
  • Server access for deployment (e.g., DigitalOcean droplet)

Setting Up A Repository

Firstly, you need to store your Laravel application in a Git repository. If you haven’t already initialized git in your Laravel project run these commands in your project directory:

git init
 git add .
 git commit -m "Initial commit"

Next, create a new repository on GitHub and link your local repository to this remote one with:

git remote add origin your-repository-url
 git push -u origin master

Automated Testing with PHPUnit

Laravel comes with built-in support for testing with PHPUnit. Automated tests are essential for a good CI/CD pipeline, ensuring that every change passes necessary quality checks. The default Laravel setup includes an example test that you can run:

php artisan test

Existing tests are found under the tests directory. You should create your tests for your application functionality. Here’s a simple example:

public function testBasicTest()
{
    $response = $this->get('/');
    $response->assertStatus(200);
}

Once you’ve added your tests, commit them to your repository:

git add .
 git commit -m 'Added tests'
 git push origin master

Setting up Continuous Integration with GitHub Actions

The next step is to configure GitHub Actions, which offers CI/CD functionality directly within the GitHub platform. You’ll need to create a .github/workflows folder in your repository and add a new YAML file to define your workflow, say laravel.yml:

name: Laravel CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Install Dependencies
      run: composer install --no-progress
    - name: Run Tests
      run: php artisan test

With this basic setup, each push to the repository will now trigger a workflow execution that will clone the repo, install dependencies, and run tests. If any step fails, the workflow will halt, and GitHub will show the push as unsuccessful.

Automating Laravel Deployments

Now that CI is in place, you can add steps to your workflow to handle CD. Deployments can be as simple or complex as your server configuration demands. A basic deployment step could involve SSHing into your server and pulling the latest changes from Git.

Add the following to your workflow file after the test step:

- name: Deploy to Production
  if: github.ref == 'refs/heads/master'
  uses: appleboy/ssh-action@master
  with:
    host: ${{ secrets.DEPLOY_SERVER }}
    username: ${{ secrets.DEPLOY_USER }}
    password: ${{ secrets.DEPLOY_PASSWORD }}
    script: | 
      cd path-to-your-laravel-app
      git pull origin master
      composer install --no-dev --optimize-autoloader
      php artisan migrate --force

For this action to work securely, you’ll want to store server connection details as secrets in your GitHub repository settings rather than in the workflow file itself.

Maintenance and Rollbacks

Good CI/CD practice isn’t just about pushing deployments. It’s also important to have strategies in place for application maintenance and rollbacks. Handling database migrations, backups, and storage of assets are all crucial considerations. It’s beyond the scope of this tutorial, but be aware that a stable deployment should make provisions for these aspects as well.

There are also Laravel-specific deployment services, like Envoyer, which can handle zero-downtime deployments and common task automation that may complement your CI/CD pipeline.

Conclusion

Setting up a CI/CD pipeline is an investment that pays substantial dividends. It promotes confidence in your release procedures, encourages testing, and accelerates development cycles. Following the steps outlined in this tutorial will set you onto a path of more efficient, reliable, and professional Laravel development workflows.