Configure pipeline in Bitbucket for PHP.

Author
By Darío Rivera
Posted On in CI/CD

In our previous post, we saw What is a pipeline and how to enable it in Bitbucket. Today we will see how to configure the bitbucket-pipelines.yml file to run a pipeline in PHP and be able to test our applications as well as perform other routine tasks.

Imagen

The first step to configure a pipeline is to choose the image that will serve us for the compilation, testing, and/or deployment of our application. Remember that you can find images in Docker Hub. In this case, we're going to use an image for PHP 7.4.

image: php:7.4

Pipelines

A pipeline consists of a series of steps to execute on one or multiple branches. A pipeline that will be executed in all branches must be configured as default.

pipelines:
  default:
    - step:
      ...
    - step:
      ...

Otherwise, you can specify the branch where the pipeline will run. The following scaffolding sets up a series of steps to run on the master branch and any feature branch.

pipelines:
  branches:
    master:
      - step:
        ...
    feature/*:
      - step:
        ...

Steps

Each step in the pipeline runs on a separate Docker container. The most important section of this configuration is the script, which essentially are the commands that the container will execute. A good practice is to launch the update command for the relevant distribution and install the necessary packages.

pipelines:
  default:
    - step:
        script:
          - apt-get update && apt-get install -y git
          ...

Common configurations for PHP repositories

In most cases, you will need the same configuration across several PHP repositories. Here are some common configurations depending on the type of repository.

PHP Libraries

image: php:7.4

pipelines:
  default:
    - step:
        script:
          - apt-get update && apt-get install -y git unzip libzip-dev
          - docker-php-ext-install -j$(nproc) zip
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - vendor/bin/phpunit --stop-on-error --stop-on-failure

Laravel Applications

image: php:7.4

pipelines:
  default:
    - step:
        script:
          - apt-get update && apt-get install -y git unzip libzip-dev
          - docker-php-ext-install -j$(nproc) zip
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - cp .env.pipelines .env.testing
          - composer install
          - vendor/bin/phpunit --stop-on-error --stop-on-failure

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.