image: node:10.15.3

common: &common
  caches:
    - yarn

clone:
  depth: 10

validate: &validate
  <<: *common
  name: Setup & validate
  script:
    - node -v
    - yarn -v
    - npx @bahmutov/print-env BITBUCKET
    - yarn install --frozen-lockfile
    - yarn speedlo generate
    - yarn speedlo validate -tld
    - yarn speedlo localize --compile
    - yarn test --verbose=true

pipelines:
  pull-requests:
    '**':
      - step:
          <<: *validate
          name: 'Validate PR'
  branches:
    master:
      - step:
          <<: *validate
          name: 'Validate [DEV]'
      - step:
          <<: *common
          trigger: manual
          name: Build & deploy [DEV]
          script:
            - npx @bahmutov/print-env DEPLOY
            - yarn install --frozen-lockfile
            - yarn build:dev
            - yarn speedlo release -s dev
            - tar -czvf $BITBUCKET_COMMIT.tar.gz build
            - curl -X POST http://$DEPLOY_USERNAME_DEV:$DEPLOY_PASSWORD_DEV@$DEPLOY_URL_DEV/projects/$DEPLOY_PROJECT/revision -F "file=@./$BITBUCKET_COMMIT.tar.gz"
          artifacts:
            - $BITBUCKET_COMMIT.tar.gz
    production*:
      - step: *validate
      - step:
          <<: *common
          name: Build and release [PROD]
          trigger: manual
          script:
            - yarn install --frozen-lockfile
            - npx @bahmutov/print-env DEPLOY
            - yarn build:prod
            - yarn speedlo release -s prod
            - tar -czvf $BITBUCKET_COMMIT.tar.gz build
            - curl -X POST http://$DEPLOY_USERNAME_PROD:$DEPLOY_PASSWORD_PROD@$DEPLOY_URL_PROD/projects/$DEPLOY_PROJECT/revision -F "file=@./$BITBUCKET_COMMIT.tar.gz"
          artifacts:
            - $BITBUCKET_COMMIT.tar.gz

definitions:
  caches:
    yarn: /usr/local/share/.cache/yarn/v4
