DEV Community

Cover image for Lets Use Bitbucket Pipelines With Laravel/PHP
Jae James
Jae James

Posted on

Lets Use Bitbucket Pipelines With Laravel/PHP

Hello, World!

I am a long time lover of Bitbucket (sorry GitHub fans) and Bitbucket Pipelines is by far one of my favorite CI/CD tools.
Bitbucket Pipelines allows you to automatically build, test and even deploy your code, based on a configuration file in your repository. Oh and I almost forgot to mention, it's based on Docker for all you Docker fans out there ;)


So, Jae, what do I need to do to get started?

Well... That is a very good question! It's actually really simple to setup even if you don't have an in-depth knowledge of how Docker works or what it is (although I do recommend you have a read up on Docker since it is pretty cool). All you need is a Bitbucket account and Bitbucket will give you 50 minutes of free build time a month which should be more than enough for personal projects and side hustles.

Now you have made a Bitbucket account and totally opted-in to Atlassian's marketing, let's get started with the fun stuff!

bitbucket-pipelines.yml

The beating heart for our CI/CD automated joy is our bitbucket-pipelines.yml file. Now, you can generate this in one of two ways; The first way is by manually creating the file (this is what we will do in order for me to break each stage down), the second is by using the online generator which you can do by opening up your repository, clicking Pipelines on the left and it will take you through the visual wizard there to setting up your bitbucket-pipelines.yml file.

So... enough babbling on. Create a file in the route of your project called, you guessed it, bitbucket-pipelines.yml. We'll populate the file with the following for now and I'll break it down.

image: php:7.3

pipelines:
  default:
    - step:
        caches:
          - composer
        script:
          - apt-get update && apt-get install -y unzip
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - cp .env.example .env
          - php artisan key:generate
          - vendor/bin/phpunit
          - echo "Done!"

Let's look at the first line:

image: php7.3

This is the base Docker image that we want Bitbucket Pipelines to use. We can realistically change this to anything we want as long as the image is publicly available on DockerHub. For me however, I've found that the default PHP Docker image works just fine and contains everything I need dependency wise. If you want to use any other PHP version, just change the 7.3 at the end, it really is as simple as that!

pipelines:
  default:
    - step:
        caches:
          - composer
        script:
          - apt-get update && apt-get install -y unzip
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - cp .env.example .env
          - php artisan key:generate
          - vendor/bin/phpunit
          - echo "Done!"

The above is the brains of our pipeline. As you can see, under pipelines we can define different pipelines to go down. This one is the default pipeline. This pipeline will be ran by default, automatically as long as the branch is setup to use pipelines (this is master by default). This is useful if you want automatic code testing.
caches: created a composer cache which means we don't need to run composer install if the composer.json file hasn't had an update since the last build.
Under the script section, this should look oh so familiar. These are just typical Linux commands. We need to install Unzip since the PHP docker image is pretty barebones. Once doing so, we want to install composer and move it into the /usr/local/bin directory so we can access it globally. From then on, we want to copy our .env.example file to be our .env file and generate an application key specifically for Bitbucket Pipelines. Then we just run PHPUnit to run our tests. This means if one of our tests fails, the build will fail and if you have email notifications configured, you'll get a lovely email in your inbox.

The Conclusion

It really is that simple... This will automatically build and test our code. Now we can go one step further in this by adding Deployment steps which will allow us to have access to buttons to deploy our code to various environments. Make sure to follow me as I'll be writing a post on how to deploy to Forge from Bitbucket Pipelines as a continuation to this little guide!

If anyone has any problems at all, leave a comment and I'll be sure to help out!

Top comments (0)