name: deploy to production

on: "push"
jobs:
    deploy:
        name: deploy
        runs-on: ubuntu-latest
        container:
            image: gitbase.de/actions/ubuntu:latest
            volumes:
                - /data:/data
        steps:
            - uses: actions/checkout@v3
              with:
                  fetch-depth: 0
                  lfs: true
                  submodules: true

            - run: |
                  git fetch --force --tags

            # setup node 20
            - name: setup node 20
              uses: actions/setup-node@v3
              with:
                  node-version: 20

            - name: install dependencies
              run: |
                  npm install -g yarn
                  yarn install

            - name: modify config
              run: |
                  sed -i 's#\(sentryEnvironment.*\)".*"#\1"main"#g' frontend/src/config.ts
                  sed -i 's#//[[:space:]]*\(sentry\.init.*\)#\1#g' frontend/src/config.ts
                  cat frontend/src/config.ts

                  set -o allexport
                  . ./.env
                  echo "PROJECT_RELEASE=${SENTRY_PROJECT}.r`git rev-list HEAD --count`-`git describe --all --long | sed 's+/+-+'`" >> .env
                  . ./.env
                  set +o allexport
                  echo ______ .env ______
                  cat .env
                  echo
                  sed -i 's#\(const release = \).*#\1"'${PROJECT_RELEASE}'"#g' api/hooks/config-client.js
                  sed -i 's#\(const originURL = \).*#\1"'${LIVE_URL}'"#g' api/hooks/config-client.js

                  export stamp=`date +%s`
                  sed -i s/__TIMESTAMP__/$stamp/g frontend/spa.html
                  rm api/templates/spa.html
                  cp frontend/spa.html api/templates/spa.html
                  echo ______ frontend/spa.html ______
                  cat frontend/spa.html

                  echo ______ api/config.yml.env ______
                  cat api/config.yml.env

            - name: build
              env:
                  FORCE_COLOR: "true"
              run: |
                  yarn build
            - name: upload Sourcemaps for Sentry
              env:
                  SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
              run: |
                  yarn upload:sourcemaps

            - name: build admin
              env:
                  FORCE_COLOR: "true"
              run: |
                  yarn build:admin

            - name: build ssr
              env:
                  FORCE_COLOR: "true"
              run: |
                  yarn build:server
            #TODO activate before final deploy
            #    - name: build legacy
            #      env:
            #          FORCE_COLOR: "true"
            #      run: |
            #          yarn build:legacy

            - name: deploy
              # only if branch is master
              if: github.ref == 'refs/heads/main'
              env:
                  RSYNC_PASS: ${{ secrets.rsync_master }}
                  BRANCH: "main"
              run: |
                  # read .env
                  set -o allexport
                  . ./.env
                  set +o allexport

                  scripts/deploy.sh

                  # clear ssr cache
                  ssrUrl=${LIVE_URL}/api/ssr
                  curl -X POST -H "Content-Type: application/json" -d '{}' "${ssrUrl}?clear=1"
