CircleCI yaml for Laravel Jetstream (TALL stack) Application

We often feature guides and techniques we use for rapidly developing with our Laravel based application and TALL stack, and this is number 2 in a series on unit and feature testing.

LoadForge uses CircleCI to run automated tests on our GitHub repo for all pushes (but specifically with pull requests). This allows us to easily verify that all our tests pass before merging new functionality in.

With Laravel you only need a simple yaml configuration file, which is stored in .circleci/config.yml in order to setup your CircleCI configuration.

Note: We also use a .env.circleci environment file to do all our local configurations in CircleCI, which we'll include for assisting you with your setup.

.circleci/config.yml

This installs our TALL stack and runs our unit tests for our Laravel app.

version: 2.1

jobs:
  tests:
    docker:
      - image: circleci/php:7.4-node-browsers
    working_directory: ~/laravel
    steps:
      - restore_cache:
          keys:
            - source-v1-{{ .Branch }}-{{ .Revision }}
            - source-v1-{{ .Branch }}-
            - source-v1-
      - checkout
      - save_cache:
          key: source-v1-{{ .Branch }}-{{ .Revision }}
          paths:
            - ".git"
      - run:
          name: Updating composer
          command: sudo composer self-update
      - restore_cache:
          keys:
            - composer-v1-{{ checksum "composer.lock" }}
            - composer-v1-
      - run:
          name: Composer install
          command: |
            composer install --prefer-dist --no-interaction --no-progress --no-scripts
      - save_cache:
          key: composer-v1-{{ checksum "composer.lock" }}
          paths:
            - vendor
      - restore_cache:
          keys:
            - node-v1-{{ checksum "package.json" }}
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "package-lock.json" }}
      - run:
          name: npm packages
          command: |
            yarn install
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "package-lock.json" }}
          paths:
            - ~/.cache/yarn
      - save_cache:
          key: node-v1-{{ checksum "package.json" }}
          paths:
            - node_modules
      - restore_cache:
          keys:
            - webpack-v1-{{ checksum "webpack.mix.js" }}
            - webpack-v1-
      - run:
          name: npm run dev
          command: |
            yarn run dev
      - save_cache:
          key: webpack-v1-{{ checksum "webpack.mix.js" }}
          paths:
            - public
      - run:
          name: Populate .env for circleci
          command: |
            cp .env.circleci .env.testing
            cp .env.circleci .env
            php artisan key:generate
      - run:
          name: phpunit
          command: |
            sleep 5
            mkdir -p ~/tests/_output/cloud
            XDEBUG_MODE=coverage ./vendor/bin/phpunit -d zend.enable_gc=0 --log-junit ~/tests/_output/cloud/junit.xml tests
          when: always
      - store_test_results:
          path: ~/tests/_output
      - store_artifacts:
          path: ~/tests/_output
      - store_artifacts:
          path: ~/laravel/storage/logs/

workflows:
  version: 2
  test:
    jobs:
      - tests

.env.circleci

This is the custom environment configuration for CircleCI that our yaml uses with various LoadForge specific config and secrets removed. Note that it supports using Redis on CircleCI.

APP_NAME="LoadForge"
APP_ENV=local
APP_KEY=base64:X
APP_DEBUG=true
APP_URL=https://loadforge.com

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=loadforge
DB_USERNAME=loadforge
DB_PASSWORD=secret


CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis
QUEUE_CONNECTION=redis

SESSION_LIFETIME=120

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_SCHEME=tcp

Final words

You can expand these defaults to suit your environment better, and replace some of our naming with your own. We should mention we actually also run in Laravel Vapor which is why we don't have queue servers like horizon.

Ready to run that test?
Start your first test within minutes.