image: php:8.2

definitions:
  steps:
    - step: &php-build-test
        name: Build PHP
        caches:
          - composer
        services:
         - mysql
         - mysql-claims-history
        script:
          - apt-get update && apt-get install -y unzip libzip-dev libpng-dev
          - docker-php-ext-install pdo_mysql zip gd
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install --ignore-platform-req=ext-gd
          - cp .env.example .env
          - php artisan key:generate
          - php artisan vendor:publish --provider="PHPOpenSourceSaver\JWTAuth\Providers\LaravelServiceProvider"
          - php artisan migrate
          - php artisan jwt:secret
          - php artisan db:seed
          - php artisan test
        artifacts:
          - vendor/**
          - test-reports/**

    - step: &php-build
        name: Build PHP
        caches:
          - composer
        services:
         - mysql
         - mysql-claims-history
        script:
          - apt-get update && apt-get install -y unzip libzip-dev libpng-dev
          - docker-php-ext-install pdo_mysql zip gd
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install --ignore-platform-req=ext-gd
          - cp .env.example .env
          - php artisan key:generate
          - php artisan vendor:publish --provider="PHPOpenSourceSaver\JWTAuth\Providers\LaravelServiceProvider"
          - php artisan jwt:secret
        artifacts:
          - vendor/**
          - test-reports/**

    - step: &deploy-production
        name: Deploy (Production)
        deployment: production
        caches:
          - composer
        script:
          - apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer global require "laravel/envoy"
          - ~/.composer/vendor/bin/envoy run deploy --host=$PROD_DEPLOY_HOST --user=$PROD_DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR --new_release=true --port=$PROD_PORT --folder=$PROD_CURRENT_FOLDER

    - step: &deploy-test
        name: Deploy (Test)
        deployment: test
        caches:
          - composer
        script:
          - apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer global require "laravel/envoy"
          - ~/.composer/vendor/bin/envoy run deploy --host=$DEPLOY_HOST --user=$DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR --new_release=false --port=$PORT --folder=$CURRENT_FOLDER

    - step: &deploy-staging
        name: Deploy (Staging)
        deployment: staging
        caches:
          - composer
        script:
          - apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer global require "laravel/envoy"
          - ~/.composer/vendor/bin/envoy run deploy --host=$DEPLOY_HOST --user=$DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR --port=$PORT --folder=$CURRENT_FOLDER

    - step: &deploy-aces-staging
        name: Deploy (Aces Staging)
        deployment: aces-staging
        caches:
          - composer
        script:
          - apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer global require "laravel/envoy"
          - ~/.composer/vendor/bin/envoy run deploy --host=$DEPLOY_HOST --user=$DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR --port=$PORT --folder=$CURRENT_FOLDER

    - step: &deploy-aces-test
        name: Deploy (Aces Test)
        deployment: aces-test
        caches:
          - composer
        script:
          - apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer global require "laravel/envoy"
          - ~/.composer/vendor/bin/envoy run deploy --host=$DEPLOY_HOST --user=$DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR --new_release=false --port=$PORT --folder=$CURRENT_FOLDER

    - step: &deploy-aces-production
        name: Deploy (Aces Production)
        deployment: aces-production
        caches:
          - composer
        script:
          - apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer global require "laravel/envoy"
          - ~/.composer/vendor/bin/envoy run deploy --host=$ACES_PROD_DEPLOY_HOST --user=$ACES_PROD_DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR --new_release=false --port=$ACES_PROD_PORT --folder=$PROD_CURRENT_FOLDER

    - step: &deploy-qc
        name: Deploy (QC)
        deployment: qc
        caches:
          - composer
        script:
          - apt-get update && apt-get install -qy unzip git curl libmcrypt-dev rsync
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer global require "laravel/envoy"
          - ~/.composer/vendor/bin/envoy run deploy --host=$DEPLOY_HOST --user=$DEPLOY_USER --path=$DEPLOY_PATH --build=$BITBUCKET_BUILD_NUMBER --commit=$BITBUCKET_COMMIT --branch=$BITBUCKET_BRANCH --php=php --dir=$BITBUCKET_CLONE_DIR --new_release=false --port=$PORT --folder=$CURRENT_FOLDER


  services:
    mysql:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: 'pipeline'
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        MYSQL_USER: 'username'
        MYSQL_PASSWORD: 'password'
        MYSQL_TCP_PORT: 3306
      ports:
      - "3306:3306"
      expose:
      - 3306

    mysql-claims-history:
      image: mysql:5.7
      environment:
        MYSQL_DATABASE: 'pipeline_claims_history'
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        MYSQL_USER: 'username'
        MYSQL_PASSWORD: 'password'
        MYSQL_TCP_PORT: 3307
      ports:
      - "3307:3307"
      expose:
      - 3307

pipelines:
  default:
    - step: *php-build-test
  branches:
    production:
      - step: *php-build
      - step: *deploy-production
    staging:
      - step: *php-build-test
      - step: *deploy-staging
    development:
      - step: *php-build
      - step: *deploy-test
    qc:
      - step: *php-build
      - step: *deploy-qc
    aces-staging:
      - step: *php-build-test
      - step: *deploy-aces-staging
    aces-development:
      - step: *php-build
      - step: *deploy-aces-test
    aces-production:
      - step: *php-build
      - step: *deploy-aces-production
    feature/*:
      - step: *php-build-test
