This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

Using Bitbucket Pipelines in a PHP App

Updated on December 8, 2021

5 Min Read

Bitbucket Pipelines is an integrated CI/CD (continuous integration and continuous delivery) service built into Bitbucket. It allow user to automatically build, test, and send your code based on a setup record in your store. Basically, we make holders within the cloud for you. Inside these containers, you can run commands but with all the focal points of a new system, customized and configured for your needs.

Bitbucket Pipelines is an exceptionally polished but constrained experience. It can be an extraordinary tool for quickly getting a small group into the CI/CD world, but in the event that you wish more progressed functionality, you may rapidly hit the limits of the platform.

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.

Nothing as Easy as Deploying PHP Apps on Cloud

With Cloudways, you can have your PHP apps up and running on managed cloud servers in just a few minutes.

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.

Read more: Enhanced Cloudways Staging Environment Is Now Available for All Users

The Sample PHP App

Static Analysis

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

image: phpunit/phpunit:5.0.3

pipelines:

 default:

   - step:

    script: # Modify the commands below to build your repository.

        - composer install

         - php -l index.php

        - ./vendor/bin/phpcs index.php

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:

<?php

class Sum

{

/**

* This function will add N numbers and return the result.

*

* Given n arguments, this function will return the sum.

*

* @return int

*/

public function getSum()

{

    $args = func_get_args();

    $sum = 0;

    if (empty($args)) {

        return $sum;

    }

    foreach ($args as $arg) {

        $sum += $arg;

    }

    return $sum;

}

}

?>

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:

<?php
namespace myproject\Test;
require __DIR__ . "/../Controllers/Sum.php";
class SumTest extends \PHPUnit_Framework_TestCase
{
    public function testPositiveNumbers()
    {
        $s = new \Controllers\Sum();
        $this->assertEquals(6, $s->getSum(5, 1));
        $this->assertEquals(102, $s->getSum(100, 2));
    }
    public function testMultipleNumbers()
    {
        $s = new \Controllers\Sum();
        $this->assertEquals(0, $s->getSum());
        $this->assertEquals(6, $s->getSum(5, 1));
        $this->assertEquals(9, $s->getSum(5, 1, 3));
        $this->assertEquals(120, $s->getSum(100, 2, 8, 10));
    }
    public function testNegativeNumbers()
    {
        $s = new \Controllers\Sum();
        $this->assertEquals(4, $s->getSum(5, -1));
        $this->assertEquals(7, $s->getSum(5, -1, 3));
    }
}

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:

image: phpunit/phpunit:5.0.3

pipelines:

 default:

     - step:

          script: # Modify the commands below to build your repository.

       - composer install

       - php -l myproject/Controllers/Sum.php

       - ./vendor/bin/phpcs myproject/Controllers/Sum.php

       - ./vendor/bin/phpunit

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.

Supercharged Managed PHP Hosting – Improve Your PHP App Speed by 300%

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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Fahad Saleh

Fahad Saleh is a DevOps Engineer at Cloudways

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now