name: deploy to production

on: "push"
jobs:
    deploy:
        name: deploy
        runs-on: ubuntu-latest
        container:
            image: gitbase.de/actions/ubuntu:latest
            volumes:
                - /data:/data

        services:
            mongo:
                image: mongo:4.2
                ports:
                    - 27017:27017
                options: --health-cmd='mongo --eval "db.adminCommand(\"ping\")"' --health-interval=10s --health-timeout=5s --health-retries=3

            maildev:
                image: gitbase.de/robin/maildev:latest
                ports:
                    - 80:80
                    - 25:25
                options: --health-cmd='curl -f http://localhost:80' --health-interval=10s --health-timeout=5s --health-retries=3

            tibi-server:
                image: gitbase.de/cms/tibi-server
                ports:
                    - 8080:8080
                env:
                    DB_DIAL: mongodb://mongo
                    API_PORT: 8080
                    MAIL_HOST: maildev:25
                    SECURITY_ALLOWABSOLUTEPATHS: "true"
                    SECURITY_ALLOWUPPERPATHS: "true"
                    SECURITY_ALLOWRELATIVEPATHS: "true"
                options: --health-cmd='curl -f http://localhost:8080' --health-interval=10s --health-timeout=5s --health-retries=3

            live-server:
                image: gitbase.de/robin/live-server:latest
                ports:
                    - 8081:8081
                options: --health-cmd='curl -f http://localhost:8081' --health-interval=10s --health-timeout=5s --health-retries=3

        steps:
            - uses: actions/checkout@v3
              with:
                  fetch-depth: 0

            - name: setup node 18
              uses: actions/setup-node@v3

            - name: install dependencies
              env:
                  FORCE_COLOR: "true"
              run: |
                  npm install -g yarn
                  yarn install

            - name: modify config
              run: |
                  sed -i 's#\(sentryEnvironment.*\)".*"#\1"${GITHUB_REF_NAME}"#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)
                  echo ______ .env ______
                  cat .env
                  echo
                  sed -i 's#\(const release = \).*#\1"'${PROJECT_RELEASE}'"#g' api/hooks/config-client.js

                  # 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`
                  sed -i s/__TIMESTAMP__/$stamp/g frontend/spa.html
                  # sed -i s/__TIMESTAMP__/$stamp/g frontend/serviceworker.js
                  # cat frontend/serviceworker.js
                  # rm api/templates/spa.html
                  # cp frontend/spa.html api/templates/spa.html
                  echo ______ frontend/spa.html ______
                  cat frontend/spa.html

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

            - name: build ssr
              env:
                  FORCE_COLOR: "true"
              run: |
                  yarn build:server

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

            #- name: Wait for Live Server
            #  run: |
            #      attempts=0
            #      max_attempts=2
            #      while ! curl --output /dev/null --silent --head --fail http://live-server:8081; do
            #     if [ $attempts -eq $max_attempts ]; then
            #                echo "Live server not ready after $max_attempts attempts"
            #                echo "${{ toJson(job) }}"
            #                curl -v http://live-server:8081
            #          exit 1
            #      fi
            #      attempts=$((attempts+1))
            #      echo "Waiting for live-server to be ready... attempt $attempts"
            #      sleep 5
            #      done

            #- name: Test HTTP Request
            #  run: |
            #      echo "Live server not ready after $max_attempts attempts"
            #      echo "${{ toJson(job) }}"
            #      echo "${{ job.services.live-server.id }}"
            #      echo "${{ job.services.tibi-server.id }}"
            ##      echo "${{ job.services.mongo.id }}"

            #      docker logs "${{ job.services.tibi-server.id }}"
            #      docker logs "${{ job.services.live-server.id }}"
            #      curl -v http://live-server:8081

            #- name: Install Chrome
            #  run: |
            #      wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
            #     sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
            #
            #      sudo apt-get update
            #      sudo apt-get install -y google-chrome-stable

            # Lighthouse Analysis Step
            #- name: Lighthouse Analysis
            #  run: |
            #     yarn add lighthouse
            #      npx lighthouse http://127.0.0.1:8081 --output json --output-path /tmp/lighthouse-report.json --chrome-flags="--headless --no-sandbox --disable-dev-shm-usage"

            # Notify-Lighthouse Step
            #- name: Notify Lighthouse
            #  run: |
            #      docker run --rm \
            #      -e PLUGIN_FROM=noreply@gitbase.de \
            #      -e PLUGIN_HOST=smtp.basehosts.de \
            #      -e PLUGIN_RECIPIENT=recipient@example.com \
            #      -e PLUGIN_SUBJECT="Lighthouse Report" \
            #      -v ${{ github.workspace }}/tmp:/lighthouse-reports \
            #      drillster/drone-email /tmp/lighthouse-report.json

            - name: deploy
              if: github.ref == 'refs/heads/master'
              env:
                  RSYNC_USER: "allkids_rsync_master"
                  RSYNC_PASS: ${{ secrets.rsync_master }}
              run: |
                  scripts/deploy.sh ftp1.webmakers.de $RSYNC_USER $RSYNC_PASS