name: Feature Preview

on:
  workflow_dispatch:
    inputs:
      base_tag_name:
        description: 'Base Tag Name'
        required: false
        default: 'preview'

env:
  TARGET_BRANCH: ${{ github.ref_name }}

jobs:
  branch_build_setup:
    name: Build Setup
    runs-on: ubuntu-latest
    outputs:
      gh_branch_name: ${{ steps.set_env_variables.outputs.TARGET_BRANCH }}
      flat_branch_name: ${{ steps.set_env_variables.outputs.FLAT_BRANCH_NAME }}
      gh_buildx_driver: ${{ steps.set_env_variables.outputs.BUILDX_DRIVER }}
      gh_buildx_version: ${{ steps.set_env_variables.outputs.BUILDX_VERSION }}
      gh_buildx_platforms: ${{ steps.set_env_variables.outputs.BUILDX_PLATFORMS }}
      gh_buildx_endpoint: ${{ steps.set_env_variables.outputs.BUILDX_ENDPOINT }}
      aio_base_tag: ${{ steps.set_env_variables.outputs.AIO_BASE_TAG }}
      do_full_build: ${{ steps.set_env_variables.outputs.DO_FULL_BUILD }}
      do_slim_build: ${{ steps.set_env_variables.outputs.DO_SLIM_BUILD }}

    steps:
      - id: set_env_variables
        name: Set Environment Variables
        run: |
          echo "BUILDX_DRIVER=docker-container" >> $GITHUB_OUTPUT
          echo "BUILDX_VERSION=latest" >> $GITHUB_OUTPUT
          echo "BUILDX_PLATFORMS=linux/amd64" >> $GITHUB_OUTPUT
          echo "BUILDX_ENDPOINT=" >> $GITHUB_OUTPUT

          if [ "${{ github.event.inputs.base_tag_name }}" != "" ]; then
            echo "AIO_BASE_TAG=${{ github.event.inputs.base_tag_name }}" >> $GITHUB_OUTPUT
          else
            echo "AIO_BASE_TAG=develop" >> $GITHUB_OUTPUT
          fi

          echo "TARGET_BRANCH=${{ env.TARGET_BRANCH }}" >> $GITHUB_OUTPUT

          FLAT_BRANCH_NAME=$(echo "${{ env.TARGET_BRANCH }}" | sed 's/[^a-zA-Z0-9]/-/g')
          echo "FLAT_BRANCH_NAME=$FLAT_BRANCH_NAME" >> $GITHUB_OUTPUT

      - id: checkout_files
        name: Checkout Files
        uses: actions/checkout@v6

  full_build_push:
    runs-on: ubuntu-22.04
    needs: [branch_build_setup]
    env:
      BUILD_TYPE: full
      AIO_BASE_TAG: ${{ needs.branch_build_setup.outputs.aio_base_tag }}
      AIO_IMAGE_TAGS: makeplane/plane-aio-feature:${{ needs.branch_build_setup.outputs.flat_branch_name }}
      BUILDX_DRIVER: ${{ needs.branch_build_setup.outputs.gh_buildx_driver }}
      BUILDX_VERSION: ${{ needs.branch_build_setup.outputs.gh_buildx_version }}
      BUILDX_PLATFORMS: ${{ needs.branch_build_setup.outputs.gh_buildx_platforms }}
      BUILDX_ENDPOINT: ${{ needs.branch_build_setup.outputs.gh_buildx_endpoint }}
    steps:
      - name: Login to Docker Hub
        uses: docker/login-action@v4
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4
        with:
          driver: ${{ env.BUILDX_DRIVER }}
          version: ${{ env.BUILDX_VERSION }}
          endpoint: ${{ env.BUILDX_ENDPOINT }}

      - name: Check out the repo
        uses: actions/checkout@v6

      - name: Build and Push to Docker Hub
        uses: docker/build-push-action@v7.0.0
        with:
          context: .
          file: ./aio/Dockerfile-app
          platforms: ${{ env.BUILDX_PLATFORMS }}
          tags: ${{ env.AIO_IMAGE_TAGS }}
          push: true
          build-args: 
            BUILD_TAG=${{ env.AIO_BASE_TAG }}
            BUILD_TYPE=${{env.BUILD_TYPE}}
          # cache-from: type=gha
          # cache-to: type=gha,mode=max
        env:
          DOCKER_BUILDKIT: 1
          DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}
    outputs:
      AIO_IMAGE_TAGS: ${{ env.AIO_IMAGE_TAGS }}

  feature-deploy:
    needs: [branch_build_setup, full_build_push]
    name: Feature Deploy
    runs-on: ubuntu-latest
    env:
      KUBE_CONFIG_FILE: ${{ secrets.FEATURE_PREVIEW_KUBE_CONFIG }}
      DEPLOYMENT_NAME: ${{ needs.branch_build_setup.outputs.flat_branch_name }}
    steps:
      - name: Install AWS cli
        run: |
          sudo apt-get update
          sudo apt-get install -y python3-pip
          pip3 install awscli
      - name: Tailscale
        uses: tailscale/github-action@v4
        with:
          oauth-client-id: ${{ secrets.TAILSCALE_OAUTH_CLIENT_ID }}
          oauth-secret: ${{ secrets.TAILSCALE_OAUTH_SECRET }}
          tags: tag:ci
      - name: Kubectl Setup
        run: |
          curl -LO "https://dl.k8s.io/release/${{ vars.FEATURE_PREVIEW_KUBE_VERSION }}/bin/linux/amd64/kubectl"
          chmod +x kubectl

          mkdir -p ~/.kube
          echo "$KUBE_CONFIG_FILE" > ~/.kube/config
          chmod 600 ~/.kube/config
      - name: HELM Setup
        run: |
          curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
          chmod 700 get_helm.sh
          ./get_helm.sh
      - name: App Deploy
        run: |
          helm --kube-insecure-skip-tls-verify repo add feature-preview ${{ vars.FEATURE_PREVIEW_HELM_CHART_URL }} 

          APP_NAMESPACE="${{ vars.FEATURE_PREVIEW_NAMESPACE }}"

          helm --kube-insecure-skip-tls-verify  uninstall \
            ${{ env.DEPLOYMENT_NAME }} \
            --namespace $APP_NAMESPACE \
            --timeout 10m0s \
            --wait \
            --ignore-not-found

          METADATA=$(helm --kube-insecure-skip-tls-verify upgrade \
              --install=true \
              --namespace $APP_NAMESPACE \
              --set dockerhub.loginid=${{ secrets.DOCKERHUB_USERNAME }} \
              --set dockerhub.password=${{ secrets.DOCKERHUB_TOKEN_RO}} \
              --set config.feature_branch=${{ env.DEPLOYMENT_NAME }} \
              --set ingress.primaryDomain=${{vars.FEATURE_PREVIEW_PRIMARY_DOMAIN || 'feature.plane.tools' }} \
              --set ingress.tls_secret=${{vars.FEATURE_PREVIEW_INGRESS_TLS_SECRET || '' }} \
              --output json \
              --timeout 10m0s \
              --wait \
              ${{ env.DEPLOYMENT_NAME }}  feature-preview/${{ vars.FEATURE_PREVIEW_HELM_CHART_NAME }} )

          APP_NAME=$(echo $METADATA | jq -r '.name')

          INGRESS_HOSTNAME=$(kubectl get ingress -n $APP_NAMESPACE --insecure-skip-tls-verify \
              -o jsonpath='{.items[?(@.metadata.annotations.meta\.helm\.sh\/release-name=="'$APP_NAME'")]}' | \
              jq -r '.spec.rules[0].host')

          echo "****************************************"
          echo "APP NAME ::: $APP_NAME"
          echo "INGRESS HOSTNAME ::: $INGRESS_HOSTNAME"
          echo "****************************************"
