name: push

on:
  push:
    branches:
      - main
      - develop
    tags:
      - v*

jobs:

  image-build-and-push-query-service:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup golang
        uses: actions/setup-go@v4
        with:
          go-version: "1.21"
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          version: latest
      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - uses: benjlevesque/short-sha@v2.2
        id: short-sha
      - name: Get branch name
        id: branch-name
        uses: tj-actions/branch-names@v7.0.7
      - name: Set docker tag environment
        run: |
          if [ '${{ steps.branch-name.outputs.is_tag }}' == 'true' ]; then
            tag="${{ steps.branch-name.outputs.tag }}"
            tag="${tag:1}"
            echo "DOCKER_TAG=${tag}-oss" >> $GITHUB_ENV
          elif [ '${{ steps.branch-name.outputs.current_branch }}' == 'main' ]; then
            echo "DOCKER_TAG=latest-oss" >> $GITHUB_ENV
          else
            echo "DOCKER_TAG=${{ steps.branch-name.outputs.current_branch }}-oss" >> $GITHUB_ENV
          fi
      - name: Install cross-compilation tools
        run: |
          set -ex
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu musl-tools
      - name: Build and push docker image
        run: make build-push-query-service

  image-build-and-push-ee-query-service:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup golang
        uses: actions/setup-go@v4
        with:
          go-version: "1.21"
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          version: latest
      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - uses: benjlevesque/short-sha@v2.2
        id: short-sha
      - name: Get branch name
        id: branch-name
        uses: tj-actions/branch-names@v7.0.7
      - name: Set docker tag environment
        run: |
          if [ '${{ steps.branch-name.outputs.is_tag }}' == 'true' ]; then
            tag="${{ steps.branch-name.outputs.tag }}"
            tag="${tag:1}"
            echo "DOCKER_TAG=$tag" >> $GITHUB_ENV
          elif [ '${{ steps.branch-name.outputs.current_branch }}' == 'main' ]; then
            echo "DOCKER_TAG=latest" >> $GITHUB_ENV
          else
            echo "DOCKER_TAG=${{ steps.branch-name.outputs.current_branch }}" >> $GITHUB_ENV
          fi
      - name: Install cross-compilation tools
        run: |
          set -ex
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu musl-tools
      - name: Build and push docker image
        run: make build-push-ee-query-service

  image-build-and-push-frontend:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install dependencies
        working-directory: frontend
        run: yarn install
      - name: Run Prettier
        working-directory: frontend
        run: npm run prettify
        continue-on-error: true
      - name: Run ESLint
        working-directory: frontend
        run: npm run lint
        continue-on-error: true
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          version: latest
      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - uses: benjlevesque/short-sha@v2.2
        id: short-sha
      - name: Get branch name
        id: branch-name
        uses: tj-actions/branch-names@v7.0.7
      - name: Set docker tag environment
        run: |
          if [ '${{ steps.branch-name.outputs.is_tag }}' == 'true' ]; then
            tag="${{ steps.branch-name.outputs.tag }}"
            tag="${tag:1}"
            echo "DOCKER_TAG=$tag" >> $GITHUB_ENV
          elif [ '${{ steps.branch-name.outputs.current_branch }}' == 'main' ]; then
            echo "DOCKER_TAG=latest" >> $GITHUB_ENV
          else
            echo "DOCKER_TAG=${{ steps.branch-name.outputs.current_branch }}" >> $GITHUB_ENV
          fi
      - name: Build and push docker image
        run: make build-push-frontend

  image-build-and-push-frontend-ee:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Create .env file
        run: |
          echo 'INTERCOM_APP_ID="${{ secrets.INTERCOM_APP_ID }}"' > frontend/.env
          echo 'SEGMENT_ID="${{ secrets.SEGMENT_ID }}"' >> frontend/.env
          echo 'CLARITY_PROJECT_ID="${{ secrets.CLARITY_PROJECT_ID }}"' >> frontend/.env
          echo 'SENTRY_AUTH_TOKEN="${{ secrets.SENTRY_AUTH_TOKEN }}"' >> frontend/.env
          echo 'SENTRY_ORG="${{ secrets.SENTRY_ORG }}"' >> frontend/.env
          echo 'SENTRY_PROJECT_ID="${{ secrets.SENTRY_PROJECT_ID }}"' >> frontend/.env
          echo 'SENTRY_DSN="${{ secrets.SENTRY_DSN }}"' >> frontend/.env
          echo 'TUNNEL_URL="${{ secrets.TUNNEL_URL }}"' >> frontend/.env
          echo 'TUNNEL_DOMAIN="${{ secrets.TUNNEL_DOMAIN }}"' >> frontend/.env
          echo 'POSTHOG_KEY="${{ secrets.POSTHOG_KEY }}"' >> frontend/.env
      - name: Install dependencies
        working-directory: frontend
        run: yarn install
      - name: Run Prettier
        working-directory: frontend
        run: npm run prettify
        continue-on-error: true
      - name: Run ESLint
        working-directory: frontend
        run: npm run lint
        continue-on-error: true
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          version: latest
      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - uses: benjlevesque/short-sha@v2.2
        id: short-sha
      - name: Get branch name
        id: branch-name
        uses: tj-actions/branch-names@v7.0.7
      - name: Set docker tag environment
        run: |
          if [ '${{ steps.branch-name.outputs.is_tag }}' == 'true' ]; then
            tag="${{ steps.branch-name.outputs.tag }}"
            tag="${tag:1}"
            echo "DOCKER_TAG=${tag}-ee" >> $GITHUB_ENV
          elif [ '${{ steps.branch-name.outputs.current_branch }}' == 'main' ]; then
            echo "DOCKER_TAG=latest-ee" >> $GITHUB_ENV
          else
            echo "DOCKER_TAG=${{ steps.branch-name.outputs.current_branch }}-ee" >> $GITHUB_ENV
          fi
      - name: Build and push docker image
        run: make build-push-frontend
