image: atlassian/default-image:3

definitions:
  steps:
    - step: &github-trigger
        name: Trigger GitHub Automation Tests
        script:
          - set -e
            - echo "Push detected in Bitbucket"
            - >
            curl -s -X POST
            -H "Authorization: token ${GITHUB_PAT}"
              -H "Accept: application/vnd.github+json"
            https://api.github.com/repos/NextLevelValueBV/usedapp-cypress/actions/workflows/bitbucket-sync.yml/dispatches
            -d '{"ref":"main"}'

    - step: &deploy-dev
        name: Deployment to Development Server
          services:
            - docker
          script:
            - set -e
            - apt-get update -qy
            - apt-get install -y sshpass
            - >
              sshpass -p "${SSH_PASS_DEV}" ssh
              -o StrictHostKeyChecking=no
              -p ${SSH_PORT}
              ${SSH_USER_DEV}@${SSH_DOMAIN}
              "
                cd ${DEV_PATH} &&
                git stash &&
                git checkout dev2 &&
                git pull https://${GIT_USER}:${GIT_PASS}@bitbucket.org/${REPO_PATH} dev2 &&
                npm run build
              "

    - step: &deploy-mock
        name: Deployment to Mock Server
          services:
            - docker
          script:
            - set -e
            - apt-get update -qy
            - apt-get install -y sshpass
            - >
              sshpass -p "${SSH_PASS_DEV}" ssh
              -o StrictHostKeyChecking=no
              -p ${SSH_PORT}
              ${SSH_USER_DEV}@${SSH_DOMAIN}
              "
                cd ${MOCK_PATH} &&
                git stash &&
                git checkout mock &&
                git pull https://${GIT_USER}:${GIT_PASS}@bitbucket.org/${REPO_PATH} mock &&
                npm run build
              "

pipelines:
  branches:
    mock:
      - step: *deploy-mock
      - step: *github-trigger

    dev2:
      - step: *deploy-dev