# This is a sample build configuration for JavaScript.
# Check our guides at https://confluence.atlassian.com/x/14UWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: node:14.15.3

options:
  docker: true

definitions: 
  steps:
    - step: &build
        name: Build
        runs-on: 
          - self.hosted
          - demo.pdf
          - linux
        caches:
          - node
        script:
          - npm install
    - step: &test
        name: Test
        runs-on: 
          - 'self.hosted'
          - demo.pdf
          - linux
        caches:
          - node
        script:
          - npm run test

pipelines:
  default:
    - step: *build
    - step: *test
  branches:
    master:
    - step: *build
    - step: *test
    - step:
        name: Build&Push Docker Image
        runs-on: 
          - 'self.hosted'
        image: atlassian/pipelines-awscli:latest
        script:
          # aws login
          - eval $(aws ecr get-login --region ${AWS_DEFAULT_REGION} --no-include-email)
          # docker
          - docker build -t ${AWS_REGISTRY_URL}:pdf_server_latest .
          - docker push ${AWS_REGISTRY_URL}:pdf_server_latest
    - step:
        name: Deploy To dev.meshid.app
        script:
          # - ssh-keyscan -p 2222 -t rsa demo.singletruth.io >> ~/.ssh/known_hosts
          - cat ./deploy-demo.sh | ssh ubuntu@dev.meshid.app -p 2222
          - echo "Deploy to dev.meshid.app finished"

  tags:
    '*':
      - step: *build
      - step: *test
      - step:
          name: Build&Push Docker Image
          runs-on: 
            - self.hosted
            - demo.pdf
            - linux
          image: atlassian/pipelines-awscli:latest
          script:
            # aws login
            - eval $(aws ecr get-login --region ${AWS_DEFAULT_REGION} --no-include-email)
            # docker
            - docker build -t ${AWS_REGISTRY_URL}:pdf_server_${BITBUCKET_TAG} .
            - docker push ${AWS_REGISTRY_URL}:pdf_server_${BITBUCKET_TAG}