Setting up a simple Bitbucket pipeline for a Laravel application

If you’re using Bitbucket, then using their Pipelines feature as your CI/CD tool is a natural choice. Here’s a pipeline script that I use as a starting point for my new projects.

For every pull request created, the pipeline will perform the following tasks:

  • Running your test suite.
  • Ensuring code standards are adhered to.
  • Running static analysis against your codebase.

The pipeline

Bitbucket Pipelines uses a Docker container under the hood, and is configured by placing a bitbucket-pipelines.yml file in the root folder of your repository.

I’ve got a sample configuration file below, that works for a relatively standard Laravel application.

image: php:7.4-fpm
pipelines:
  pull-requests:
    '**':
      - step:
          name: Run tests
          services:
            - docker
          caches:
            - composer
            - node
          script:
            - apt-get update
            - apt-get install -y unzip libzip-dev libpng-dev zip git
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - docker-php-ext-configure zip
            - docker-php-ext-install zip bcmath pcntl gd
            - php -r "file_exists('.env') || copy('.env.example', '.env');"
            - composer install --no-progress --no-suggest --prefer-dist --no-interaction
            - vendor/bin/phpcs --standard=PSR2 -n app
            - apt-get update && apt-get install -y curl gnupg gnupg2 && curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get install -y nodejs
            - php artisan key:generate
            - npm install
            - npm run dev
            - php artisan test --parallel
            - vendor/bin/phpstan analyse --memory-limit=512M

Lessons learned

  • The earlier you can get these steps automated, the better. Pro tip: do it BEFORE you hire your first developer!
  • Bitbucket Pipelines is a pain to configure. But it’s really cheap if you’re already using Bitbucket.

End

What do you think about my pipeline setup? Would you do anything differently?