image: stelianandrei/pipelines-php:7.4

definitions:
  caches:
    node-modules: web/app/themes/b2bnext/node_modules
  steps:
    - step: &install
        name: Install and build
        caches:
          - composer
          - node
          - node-modules
        script:
          - composer install
          - cd web/app/themes/b2bnext
          - composer install
          - nvm use 12
          - npm install
          - npm run build:production
        artifacts:
          - vendor/**
          - web/wp/**
          - web/app/plugins/**
          - web/app/themes/b2bnext/vendor/**
          - web/app/themes/b2bnext/public/**
    - step: &deploy
        script:
          - composer dumpautoload
          - rsync -rltv /opt/atlassian/pipelines/agent/build/ $SSH_USER@$SSH_HOST:$SSH_PATH --exclude=.env --exclude=web/.htaccess --exclude=.git --exclude=web/.well-known --exclude=web/app/uploads --exclude=web/app/cache --exclude=web/app/themes/b2bnext/node_modules --delete
          - ssh $SSH_USER@$SSH_HOST "find $SSH_PATH/web -name "cache" -type d -exec chmod 777 {} \;"
          - ssh $SSH_USER@$SSH_HOST "find $SSH_PATH/web -name "storage" -type d -exec chmod 777 {} \;"

pipelines:
  branches:
    staging:
      - step: *install
      - step:
          <<: *deploy
          deployment: staging
          name: Deploying to STAGING
    master:
      - step: *install
      - step:
          <<: *deploy
          deployment: production
          name: Deploying to LIVE