kind: pipeline
type: docker
name: default

steps:
    ##############################
    # Build and deploy docs
    ##############################
    - name: build docs
      image: node:18
      pull: if-not-exists
      environment:
          FORCE_COLOR: "true"
      commands:
          - cd docs
          - yarn install
          - yarn docpress:build
      when:
          branch: [master]
          event: [push]

    - name: deploy docs
      image: instrumentisto/rsync-ssh
      pull: if-not-exists
      environment:
          RSYNC_HOST: ftp1.webmakers.de
          RSYNC_PORT: 22222
          RSYNC_USER: webmakers_tibi_docs_rsync_master
          RSYNC_PASS:
              from_secret: rsync_master
      commands:
          - cd docs
          - >
              rsync -rlcgD --perms -i -u -v --stats --progress
              --delete
              -e "sshpass -p $${RSYNC_PASS} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p $${RSYNC_PORT}"
              _docpress/
              $${RSYNC_USER}@$${RSYNC_HOST}:./
      when:
          branch: [master]
          event: [push]

    ##############################
    # Demo project
    ##############################
    - name: yarn install
      image: node:18
      pull: if-not-exists
      environment:
          FORCE_COLOR: "true"
      commands:
          - yarn install

    - name: modify config
      image: alpine/git
      commands:
          - sed -i 's#\(sentryEnvironment.*\)".*"#\1"${DRONE_BRANCH}"#g' frontend/src/config.ts
          - sed -i 's#//\( sentry\\.init.*\)#\1#g' frontend/src/config.ts
          - export $(cat .env | xargs)
          - echo "PROJECT_RELEASE=$${RELEASE_PROJECT_SLUG}.r`git rev-list HEAD --count`-`git describe --all --long | sed 's+/+-+'`" >> .env
          - export $(cat .env | xargs)
          - cat .env
          - sed -i 's#\(const release = \).*#\1"'$${PROJECT_RELEASE}'"#g' api/hooks/config-client.js

    - name: build
      image: node:18
      commands:
          - yarn build

    - name: build ssr
      image: node:18
      commands:
          - yarn build:server

    - name: build legacy
      image: node:18
      commands:
          - yarn build:legacy

    - name: modify html
      image: bash
      commands:
          - bash scripts/preload-meta.sh frontend/spa.html
          - bash scripts/preload-meta.sh frontend/spa.html > frontend/_spa.html
          - cp frontend/_spa.html frontend/spa.html
          - export stamp=`date +%s`
          - echo $$stamp
          - sed -i s/__TIMESTAMP__/$$stamp/g frontend/spa.html
          #- sed -i s/__TIMESTAMP__/$$stamp/g frontend/serviceworker.js
          #- cat frontend/serviceworker.js
          - cp frontend/spa.html api/templates/spa.html
          - cat frontend/spa.html

    # staging
    - name: copy api config to staging
      image: instrumentisto/rsync-ssh
      volumes:
          - name: data
            path: /data
      commands:
          - rsync -av api /data/
          - mkdir -p /data/frontend/dist
          - rsync -av frontend/dist/ /data/frontend/dist/
      when:
          branch: [dev]
          event: [push]

    - name: review in staging
      image: docker/compose:1.22.0
      commands:
          - docker-compose -p ${DRONE_BRANCH}-${DRONE_REPO_NAME}-${DRONE_REPO_OWNER} up -d --build --remove-orphans
      volumes:
          - name: docker
            path: /var/run/docker.sock
      when:
          branch: [dev]
          event: [push]

    # live
    - name: deploy master
      image: instrumentisto/rsync-ssh
      environment:
          RSYNC_USER: ""
          RSYNC_PASS:
              from_secret: rsync_master
      # remove if user and pass is set
      failure: ignore
      commands:
          - apk add --no-cache sshpass curl
          - scripts/deploy.sh ftp1.webmakers.de $${RSYNC_USER} $${RSYNC_PASS}
          # - curl -X POST "https://www....de/api/ssr?token=TowendQhi&clear=1"
      when:
          branch: [master]
          event: [push]

    # - name: upload sourcemaps for glitchtip
    #   image: node
    #   environment:
    #       GLITCHTIP_TOKEN:
    #           from_secret: glitchtip_token
    #   commands:
    #       - yarn upload:sourcemaps

########
volumes:
    - name: data
      host:
          path: /data/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/${DRONE_BRANCH}
    - name: docker
      host:
          path: /var/run/docker.sock
