Zaher Ghaibeh
PHP Backend developer
I've experience in a few PHP Frameworks, such as Laravel, Lumen and Slim (The last two are used for building Microservices/API services).
Manually run your Bitbucket Pipelines
Published at Thursday, July 20, 2017 , Categorized under: Code, docker, PHP

In the earlier  post, we have talked about how to test your Laravel  application with Bitbucket Pipelines, but sometimes you may need to run the tests on specific commit and not after you push it, luckily for us Bitbucket Pipelines team has implemented the custom section in the ymal file, which helps us to do so.

It does not differ that much from what we already have, so adding the following to our file which we created in the earlier  post will enable us to manually run the pipeline:

  custom:
    manual-run:
      - step:
        caches:
          - composer
          - vendor-directory
        image: zaherg/php-7.1-xdebug-alpine:latest
        script:
          - composer install --no-progress --no-suggest --prefer-dist
          - cp .env.testing .env
          - sed -i "s/DB_DATABASE=.*/DB_DATABASE=mytest_database/" .env
          - sed -i "s/DB_USERNAME=.*/DB_USERNAME=root/" .env
          - sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=test_user_password/" .env
          - php artisan key:generate
          - php artisan migrate
          - php vendor/bin/phpunit .
        services:
          - mysql

First of all, we start the section by the word custom which defines that this is the things that I'll need to execute when I run the manual execution, the second line defines the name of the custom run, and this is so important other wise the pipelines will not recognise it. Here I've called it manual-run but you can name it what ever you want, and the rest is something you already familiar with.

After pushing your modification, you can go to that commit and find that now you can run the custom pipeline like the following:

Now if you have more than one custom section, you will see them all listed here and you can choose which one you want to run. So our full file should be something like the following

image: zaherg/php-7.1-xdebug-alpine:latest

pipelines:
  default:
    - step:
        caches:
          - composer
          - vendor-directory
        script:
            - composer install --no-progress --no-suggest --prefer-dist
            - cp .env.testing .env
            - sed -i "s/DB_DATABASE=.*/DB_DATABASE=mytest_database/" .env
            - sed -i "s/DB_USERNAME=.*/DB_USERNAME=root/" .env
            - sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=test_user_password/" .env
            - php artisan key:generate
            - php artisan migrate
            - php vendor/bin/phpunit .
        services:
          - mysql

  branches:
    master:
      - step:
          caches:
            - composer
            - vendor-directory
          script:
            - composer install --no-progress --no-suggest --prefer-dist
            - cp .env.testing .env
            - sed -i "s/DB_DATABASE=.*/DB_DATABASE=mytest_database/" .env
            - sed -i "s/DB_USERNAME=.*/DB_USERNAME=root/" .env
            - sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=test_user_password/" .env
            - php artisan key:generate
            - php artisan migrate
            - php vendor/bin/phpunit .
          services:
            - mysql

## We can use this section to define the test which we want to trigger manually:
  custom:
    manual-run:
      - step:
          caches:
            - composer
            - vendor-directory
          script:
            - composer install --no-progress --no-suggest --prefer-dist
            - cp .env.testing .env
            - sed -i "s/DB_DATABASE=.*/DB_DATABASE=mytest_database/" .env
            - sed -i "s/DB_USERNAME=.*/DB_USERNAME=root/" .env
            - sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=test_user_password/" .env
            - php artisan key:generate
            - php artisan migrate
            - php vendor/bin/phpunit .
          services:
            - mysql

## This is the definitions section where we define the caches and the services we depend on:
definitions:
  caches:
    vendor-directory: vendor

  services:
    mysql:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: 'mytest_database'
        MYSQL_ROOT_PASSWORD: 'test_user_password'

And that's all, but you should always refer to Pipelines documentation as they always add new features and fix bugs.