Using Bitbucket Pipelines in a PHP App

by Fahad Saleh  March 2, 2017

If you use Bitbucket, you might have noticed a Pipelines (New) entry in the menu of Bitbucket repositories. While not exactly new (it has been there for some time now), it is something that is not used very often. This is a shame because Pipelines are a very powerful, yet easy to use feature. Pipelines can be used for static syntax analysis, unit testing, building apps and much more.

bitbucket pipelines php

Bitbucket Pipelines

Bitbucket Pipelines

Bitbucket has made sure that the feature is very easy to use. Everything is configured in a file called bitbucket-pipelines.yml. This file holds all the instructions for the process. It is committed in the repository. On the right is the general structure of the file.

Bitbucket fires up a Docker container as a build environment for all your needs. The first thing you need to define is the image of this Docker container. If you skip this step, it will use the default Bitbucket image.

Ideally, the image in question should contain the resources you need to build your project: Python for a Python project, PHP for a PHP project, Ruby for a Ruby project, and etc.

Bitbucket has yaml file templates ready for most of the popular images. You can select the most appropriate one right after you have enabled Pipelines.

pipelines

In the file structure, notice that I am able to define pipeline instructions for specific branches, tags, or even bookmarks (Mercurial). The default task section runs for all branches which do not have specific configurations for other sections. Here are the complete bitbucket-pipelines.yml file configuration options.

Like all popular functionalities, this is free to use but comes with several limitations. To recap, the limits per build are:

  • 4GB of memory (RAM)
  • 2 hours execution time

There is a also monthly limit of 500 build minutes per user.

The Sample PHP App

Static Analysis

For the sample app, I will use PHP. Here is the required bitbucket-pipelines.yml file:

For this sample app, the default Docker image from the template list will suffice.

I will use PHPCodeSniffer for static analysis. For this, this will be listed as a dependency in the composer.json. For unit testing, I will use PHPUnit for unit testing. All the missing dependencies will be installed by the composer install command.

From this point onwards, as soon as I push code into the repository, the build process will automatically start and run the tasks listed above.

In the project, I will run static analysis against the Sum class, which returns the results of N integers. The code for the class is:

During the first push, I got a whole bunch of PHPCodeSniffer errors:

PHPCodeSniffer errors

After fixing the errors (that were mainly related to missing documentation), the build process is successful.

Pipelines Successful

PHPUnit Testing

Next, I will create some test cases using PHPUnit test for the Sum class. The code of the test class is:

This requires restructuring of the directory. In addition, another task had to be added in the bitbucket-pipelines.yml file to execute the tests. The updated version of the yaml file is:

The build is successful as expected.

Logs

This is the list of the builds along the way:

All branches

At this point, the basic pipeline is done. Now I could add build automation (for example: using Gradle for Android), or even deployment. You have several option open.

If you need further information about Bitbucket Pipelines or any help with the code, do leave a comment and I will do a quick follow-up.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Fahad Saleh

Fahad Saleh is a DevOps Engineer at Cloudways

Stay Connected:

You Might Also Like...