Lets Use Bitbucket Pipelines With Laravel/PHP
Tags: php, laravel, devops, bitbucket
Published: 2019-05-11 20:47:21

---
title: Lets Use Bitbucket Pipelines With Laravel/PHP
published: true
description: A simple guide to starting a really nice CI/CD setup
tags: php, laravel, devops, bitbucket
cover_image: https://thepracticaldev.s3.amazonaws.com/i/2gdds0ffm633h6t3kiip.png
---

<h2>Hello, World!</h2>
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 ;)

<quote>
So, Jae, what do I need to do to get started?
</quote>
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!

<h3>bitbucket-pipelines.yml</h3>
The beating heart for our CI/CD automated joy is our <i>bitbucket-pipelines.yml</i> 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 <i>bitbucket-pipelines.yml</i> file.

So... enough babbling on. Create a file in the route of your project called, you guessed it, <i>bitbucket-pipelines.yml</i>. We'll populate the file with the following for now and I'll break it down.
```yaml
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:
```yaml
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 <i>7.3</i> at the end, it really is as simple as that!

```yaml
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.
<i>caches:</i> 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 <i>/usr/local/bin</i> directory so we can access it globally. From then on, we want to copy our <i>.env.example</i> file to be our <i>.env</i> 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.

<h2>The Conclusion</h2>
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!

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