image: atlassian/default-image:3

options:
  runtime:
    cloud:
      atlassian-ip-ranges: true
pipelines:
  branches:
    master:
      - step:
          name: 'Build & Deploy production to server..'
          caches:
            - node
            - nextcache
          deployment: production
          runs-on:
            - 'self.hosted'
            - 'minipc'
          script:
            - npm install --legacy-peer-deps
            - npm run build
            - apt-get update && apt-get -qq install rsync
            - mkdir live-temp
            - cp -a .next ./live-temp
            - cp package.json ./live-temp
            - cp -a node_modules ./live-temp
            - cp -a public ./live-temp
            - rsync --inplace -arz --delete $BITBUCKET_CLONE_DIR/live-temp/ $SERVER_NAME@$SERVER_HOST:public_html/inkxtore-next/live/deploy
            - ssh $SERVER_NAME@$SERVER_HOST "cd ~/public_html/inkxtore-next/live/deploy && chmod +x .next"
            - ssh $SERVER_NAME@$SERVER_HOST "rsync --remove-source-files -zhr ~/public_html/inkxtore-next/live/deploy/.next ~/public_html/inkxtore-next/live/"
            - ssh $SERVER_NAME@$SERVER_HOST "rsync --remove-source-files -zhr -progress ~/public_html/inkxtore-next/live/deploy/** ~/public_html/inkxtore-next/live/"

#      - step:
#          name: "PM2 Restart"
#          runs-on:
#            - 'self.hosted'
#            - 'minipc'
#          artifacts:
#            download: false
#          script:
#            - ssh $SERVER_NAME@$SERVER_HOST "pm2 restart inkxtore-admin-next-live"
#            - ssh $SERVER_NAME@$SERVER_HOST "rm -rf ~/public_html/inkxtore-next/live/deploy"

    staging:
      - step:
          name: 'Build & Deploying to server'
          deployment: staging
          runs-on:
            - 'self.hosted'
            - 'minipc'
          script:
            - npm install
            - npm run build
            - apt-get update && apt-get -qq install rsync
            - mkdir staging-temp
            - cp -a .next ./staging-temp
            - cp -a public ./staging-temp
            - cp package.json ./staging-temp
            - cp -a node_modules ./staging-temp
            - rsync --inplace -arz --delete $BITBUCKET_CLONE_DIR/staging-temp/ $SERVER_NAME@$SERVER_HOST:public_html/inkxtore-next/staging/deploy
            - ssh $SERVER_NAME@$SERVER_HOST "cd ~/public_html/inkxtore-next/staging/deploy && chmod +x .next"
            - ssh $SERVER_NAME@$SERVER_HOST "rsync --remove-source-files -zhr ~/public_html/inkxtore-next/staging/deploy/.next ~/public_html/inkxtore-next/staging/"
            - ssh $SERVER_NAME@$SERVER_HOST "rsync --remove-source-files -zhr -progress ~/public_html/inkxtore-next/staging/deploy/** ~/public_html/inkxtore-next/staging/"

#      - step:
#          name: "PM2 Restart"
#          runs-on:
#            - 'self.hosted'
#            - 'minipc'
#          artifacts:
#            download: false
#          script:
#            - ssh $SERVER_NAME@$SERVER_HOST "pm2 restart inkxtore-admin-next-staging"
#            - ssh $SERVER_NAME@$SERVER_HOST "rm -rf ~/public_html/inkxtore-next/staging/deploy"

    dev:
      - step:
          name: 'Build & Deploying to server'
          deployment: development
          runs-on:
            - 'self.hosted'
            - 'minipc'
          script:
            - npm install
            - npm run build
            - apt-get update && apt-get -qq install rsync
            - mkdir dev-temp
            - cp -a .next ./dev-temp
            - cp -a public ./dev-temp
            - cp package.json ./dev-temp
            - cp -a node_modules ./dev-temp
            - rsync --inplace -arz --delete $BITBUCKET_CLONE_DIR/dev-temp/ $SERVER_NAME@$SERVER_HOST:public_html/inkxtore-next/dev/deploy
            - ssh $SERVER_NAME@$SERVER_HOST "cd ~/public_html/inkxtore-next/dev/deploy && chmod +x .next"
            - ssh $SERVER_NAME@$SERVER_HOST "rsync --remove-source-files -zhr ~/public_html/inkxtore-next/dev/deploy/.next ~/public_html/inkxtore-next/dev/"
            - ssh $SERVER_NAME@$SERVER_HOST "rsync --remove-source-files -zhr -progress ~/public_html/inkxtore-next/dev/deploy/** ~/public_html/inkxtore-next/dev/"

#      - step:
#          name: "PM2 Restart"
#          runs-on:
#            - 'self.hosted'
#            - 'minipc'
#          artifacts:
#            download: false
#          script:
#            - ssh $SERVER_NAME@$SERVER_HOST "pm2 restart inkxtore-admin-next-dev"
#            - ssh $SERVER_NAME@$SERVER_HOST "rm -rf ~/public_html/inkxtore-next/dev/deploy"

definitions:
  caches:
    nextcache: .next/cache
