Level Up Your Laravel Code Quality with CI/CD

Abu Sayed
4 min readFeb 25, 2024

--

Want to automate your Laravel development workflow? Learn how continuous integration (CI) and continuous delivery (CD) can help you ship code changes faster and with more stability. This guide covers CI/CD concepts, tools like GitHub Actions and Envoyer, and implementing automated build, test, and release pipelines.

A Guide to Continuous Integration and Delivery (CI/CD) in Laravel

Shipping high quality software quickly is vital for any successful web app. But how do you make sure each change you deploy doesn’t break things? Enter CI/CD…

CI/CD (continuous integration/continuous delivery) is a devops practice for automating builds, tests, and releases. It’s like having an army of robotic helpers monitoring your code!

Now I won’t pretend CI/CD is the most exciting topic. There’s a reason they left it out of computer science courses! But implemented right, it can transform your development workflow.

Let me break down the key concepts in plain English with some real-world Laravel examples…

Continuous Integration

Continuous integration is about automatically verifying changes via your test suite and other checks.

Here’s a typical CI workflow:

  1. Developer commits code changes to a shared repo like GitHub.
  2. The CI server detects the new commit and kicks off a build.
  3. The app is installed and tests are run automatically.
  4. If tests fail, the developer is notified to fix the issues.
  5. If tests pass, the code is considered ready to be released.

This automated verification gives you confidence each change you make isn’t breaking the app. No more worrying if you remembered to run the tests locally!

Common CI Tools

There are tons of great CI tools out there:

  • GitHub Actions — baked into GitHub, easy to set up
  • Travis CI — popular open source option
  • CircleCI — powerful with great Docker support

And many more! The syntax varies, but conceptually they all work similarly.

Let’s look at a GitHub Actions workflow example:

name: CI
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Dependencies
run: composer install
- name: Execute tests
run: ./vendor/bin/phpunit

This will trigger the workflow to run automatically whenever code is pushed. It checks out the repo, installs Composer dependencies, and runs your PHPUnit tests.

Slick! You can even extend it to run coding standard checks, security scanners, and more.

Continuous Delivery

CI verifies your changes. Continuous delivery takes it a step further by automating releases.

A CD pipeline picks up where CI ends:

  1. CI tests complete successfully.
  2. CD tool automatically builds and packages the app.
  3. The package is deployed to staging for final validation.
  4. With approval, the package is promoted to production.

So you go from code change to live software without any manual steps. CD does all the heavy lifting!

Laravel and CI/CD
Laravel and CI/CD

Laravel and Envoyer

Laravel Envoyer makes continuous delivery simple. Just define the steps to build and deploy your app:

@servers(['web' => '123.45.6.78'])
@setup
$repository = 'https://github.com/user/repo';
$releases_dir = '/home/forge/myapp';
@story('deploy')
clone_repository
composer_install
npm_install
php_artisan_migrate
npm_production
reload_php
@task('clone_repository')
[ $[ clone_repository ] ]
@task('reload_php')
sudo service php-fpm reload

Now when you push changes, Envoyer will deploy them to production automatically. Add approval workflows, rollbacks, and slack notifications to make it even more robust.

Envoyer integrates nicely with Laravel Forge for managing servers. But you can use any hosting provider.

Putting It All Together

Let’s walk through a full example CI/CD pipeline:

  1. Developer commits changes, triggering CI build.
  2. CI server checks out code, installs dependencies, and runs tests.
  3. Assuming tests pass, CI job completes successfully.
  4. CD tool picks up new passing commit and deploys to staging.
  5. Tests are run against staging environment, sanity checks look good.
  6. CD tool tags commit and deploys package to production.
  7. Developers are notified of the new production release.

Boom! Fully automated path from code to production. You can see how CI and CD complement each other perfectly.

Key Benefits

Here are some of the awesome benefits you get from implementing CI/CD:

Catch Issues Rapidly

Bugs and breaks are caught immediately with automated testing, instead of manually days later.

Reduce Risk

Small, incremental code changes are less risky than big bang deployments.

Improve Quality

You’ll write more tests and follow best practices knowing CI/CD is running.

Simplify Deployments

No need for complex manual steps and procedures. It just happens automatically!

Standardize Environments

CI/CD results in dev, staging, and prod environments that closely match.

Empower Developers

Devs can focus on writing code rather than builds and IT ops.

Getting Started

If you’re sold on CI/CD, here are some tips for getting started:

  • Add CI first using GitHub Actions or similar. Get tests running automatically.
  • Gradually expand CI with coding standards, security checks, etc.
  • Once CI is robust, layer on CD for automated deployments.
  • Start with lower risk staging deploys before moving to production.
  • Get buy-in from stakeholders on the release process up front.
  • Monitor, tweak, and extend your pipelines iteratively.
  • Use Slack or similar to keep team notified of builds/deployments.

It takes some work to get right, but putting in place CI/CD will supercharge your code quality and deployment workflow!

Let’s Recap

CI/CD may sound complex at first, but it boils down to:

  • CI — Automatically verify changes via tests, checks, etc
  • CD — Automate build, test and release processes

Popular tools like GitHub Actions, Travis CI, Envoyer, and CircleCI make implementation smooth.

The benefits are huge in terms of catching issues quickly, reducing risk, and simplifying deployments.

It’s a DevOps best practice for a reason — CI/CD lets you ship better software faster!

Hopefully this overview gives you a good foundation to get started. Let me know if you have any other questions as you being your CI/CD journey!

--

--

Abu Sayed
Abu Sayed

Written by Abu Sayed

Bangladeshi Full Stack Web Dev, Sys Admin & DevOps Engineer. Skills: Data analysis, SQL, Unity, C#. Python, PHP & Laravel. Me on: bd.linkedin.com/in/imabusayed

Responses (1)