image:
  name: markoshust/magento-php:8.1-fpm-1
definitions:
  steps:
    - step: &build
        name: Build Magento
        caches:
          - composer
        artifacts:
          - build.tgz
        script:
          # fail on error
          - set -e
          # install composer dependencies
          - composer install
          # run ece-tools generate scenario
          - php ./vendor/bin/ece-tools run scenario/build/generate.xml
          # prepare release artifact for deployment
          - php ./vendor/bin/ece-tools run ./vendor/overdose/deploy-tools/scenario/build/prepare-release.xml
    - step: &deploy
        name: Deploy
        image: atlassian/default-image:3
        services:
          - docker
        caches:
          - docker
        script:
          - set -e
          # scp tgz artifact file to server
          - pipe: atlassian/scp-deploy:1.3.0
            variables:
              USER: ${SSH_USER}
              SERVER: ${SSH_HOST}
              REMOTE_PATH: '${PROJECT_ROOT}/build.tgz'
              LOCAL_PATH: 'build.tgz'
              EXTRA_ARGS: [ "-P", $SSH_PORT, "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null" ]
          # expand tgz file, create symlinks, run deploy scenario, run post-deploy-scenario
          - pipe: atlassian/ssh-run:0.5.0
            variables:
              SSH_USER: ${SSH_USER}
              SERVER: ${SSH_HOST}
              PORT: ${SSH_PORT}
              DEBUG: 'true'
              COMMAND: 'rm -rf ${PROJECT_ROOT}/build
                         && mkdir -p ${PROJECT_ROOT}/build
                         && tar -zxf ${PROJECT_ROOT}/build.tgz -C ${PROJECT_ROOT}/build
                         && php ${PROJECT_ROOT}/build/vendor/bin/ece-tools run ./vendor/overdose/deploy-tools/scenario/deploy/predeploy.xml
                         && php ${PROJECT_ROOT}/www/vendor/bin/ece-tools run ./vendor/overdose/deploy-tools/scenario/deploy/deploy.xml
                         && php ${PROJECT_ROOT}/www/vendor/bin/ece-tools run scenario/post-deploy.xml'
              ENV_VARS: >-
                PROJECT_ROOT=${PROJECT_ROOT}
                MAGENTO_CLOUD_RELATIONSHIPS=${MAGENTO_CLOUD_RELATIONSHIPS}
                MAGENTO_CLOUD_BRANCH=${BITBUCKET_BRANCH}
                MAGENTO_CLOUD_ENVIRONMENT_TYPE=${BITBUCKET_BRANCH}
                MAGENTO_CLOUD_ENVIRONMENT=${BITBUCKET_BRANCH}
pipelines:
  branches:
    staging:
      - step: *build
      - step:
          <<: *deploy
          deployment: staging
          name: Deploy Staging
    master:
      - step: *build
      - step:
          <<: *deploy
          deployment: production
          name: Deploy Production