name: Frontend Dev Deployment
on: workflow_dispatch
# Disable previous workflows for this action.
concurrency:
  group: ${{ github.workflow }} #-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Cache node modules
        uses: actions/cache@v1
        with:
          path: node_modules
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-build-
            ${{ runner.OS }}-

      - uses: ./.github/composite-actions/update-keys
        with:
          domain_name: ${{ secrets.DEV_DOMAIN_NAME }}
          license_key: ${{ secrets.DEV_LICENSE_KEY }}
          jwt_secret: ${{ secrets.DEV_JWT_SECRET }}
          minio_access_key: ${{ secrets.DEV_MINIO_ACCESS_KEY }}
          minio_secret_key: ${{ secrets.DEV_MINIO_SECRET_KEY }}
          pg_password: ${{ secrets.DEV_PG_PASSWORD }}
          registry_url: ${{ secrets.OSS_REGISTRY_URL }}
        name: Update Keys

      - name: Docker login
        run: |
          docker login ${{ secrets.OSS_REGISTRY_URL }} -u ${{ secrets.OSS_DOCKER_USERNAME }} -p "${{ secrets.OSS_REGISTRY_TOKEN }}"

      - uses: azure/k8s-set-context@v1
        with:
          method: kubeconfig
          kubeconfig: ${{ secrets.DEV_KUBECONFIG }} # Use content of kubeconfig in secret.
        id: setcontext

      - name: Building and Pushing frontend image
        id: build-image
        env:
          DOCKER_REPO: ${{ secrets.OSS_REGISTRY_URL }}
          IMAGE_TAG: ${{ github.ref_name }}_${{ github.sha }}
          ENVIRONMENT: staging
        run: |
          set -x
          cd frontend
          mv .env.sample .env
          docker run --rm -v /etc/passwd:/etc/passwd -u `id -u`:`id -g` -v $(pwd):/home/${USER} -w /home/${USER} --name node_build node:20-slim /bin/bash -c "yarn && yarn build"
          # https://github.com/docker/cli/issues/1134#issuecomment-613516912
          DOCKER_BUILDKIT=1 docker build --target=cicd -t $DOCKER_REPO/frontend:${IMAGE_TAG} .
          docker tag $DOCKER_REPO/frontend:${IMAGE_TAG} $DOCKER_REPO/frontend:${IMAGE_TAG}-ee
          docker push $DOCKER_REPO/frontend:${IMAGE_TAG}
          docker push $DOCKER_REPO/frontend:${IMAGE_TAG}-ee

      - name: Deploy to kubernetes foss
        run: |
          cd scripts/helmcharts/

          set -x
          cat <<EOF>>/tmp/image_override.yaml
          frontend:
            image:
              tag: ${IMAGE_TAG}
          EOF

          # Update changed image tag
          sed -i "/frontend/{n;n;s/.*/    tag: ${IMAGE_TAG}/}" /tmp/image_override.yaml

          cat /tmp/image_override.yaml
          # Deploy command
          mkdir -p /tmp/charts
          mv openreplay/charts/{ingress-nginx,frontend,quickwit,connector} /tmp/charts/
          rm -rf  openreplay/charts/*
          mv /tmp/charts/* openreplay/charts/
          helm template openreplay -n app openreplay -f vars.yaml -f /tmp/image_override.yaml --set ingress-nginx.enabled=false --set skipMigration=true --no-hooks | kubectl apply -n app -f -
        env:
          DOCKER_REPO: ${{ secrets.OSS_REGISTRY_URL }}
          iMAGE_TAG: ${{ github.ref_name }}_${{ github.sha }}
