name: push-pipeline
on:
  push:
    branches:
      - master
      - ^v[0-9]*.[0-9]*.x$
    tags:
      - "*"
defaults:
  run:
    working-directory: chaoscenter
    shell: bash

jobs:
  backend-checks:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: "1.22" # By default, the go version is v1.15 in runner.
      - run: |
          make backend-services-checks

  frontend-checks:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 16
      - run: |
          make frontend-services-checks

  get-envs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - run: |
          img_tag=""
          array=(`echo ${GITHUB_REF} | sed 's/\//\n/g'`)
          if [ ${array[1]} == "tags" ]
          then
              echo "tag build"
              img_tag=${GITHUB_REF#refs/*/}
          else
              echo "non tag build"
              img_tag="ci"
          fi
          # This is a condition where image tag looks like "pull/<pullrequest-name>" during pull request build
          NEW_IMG_TAG=`echo $img_tag | sed "s/\//-/g"`
          echo $NEW_IMG_TAG
          echo export IMG_TAG=$NEW_IMG_TAG >> env-vars
          echo export GRAPHQL_SERVER_IMAGE="litmusportal-server" >> env-vars
          echo export AUTHENTICATION_SERVER_IMAGE="litmusportal-auth-server" >> env-vars
          echo export FRONTEND_IMAGE="litmusportal-frontend" >> env-vars
          echo export SUBSCRIBER_IMAGE="litmusportal-subscriber" >> env-vars
          echo export EVENT_TRACKER="litmusportal-event-tracker" >> env-vars
          # echo export UPGRADE_AGENT_CP="upgrade-agent-cp" >> env-vars
          # echo export DEX_SERVER="litmusportal-dex-server" >> env-vars

      - name: Uploading envs
        uses: actions/upload-artifact@v4
        with:
          name: env_artifact
          path: chaoscenter/env-vars

  docker-build-and-push-graphql-server:
    runs-on: ubuntu-latest
    needs:
      - get-envs
      - backend-checks
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Downloading image artficate
        uses: actions/download-artifact@v4
        with:
          name: env_artifact
          path: chaoscenter

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
        with:
          version: latest

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push graphql server docker image
        env:
          IMAGE_NAME: ${GRAPHQL_SERVER_IMAGE}
          IMG_TAG: ${IMG_TAG}
          PLATFORMS: ${{ secrets.PLATFORMS }}
          REPONAME: ${{ secrets.REPONAME }}
          DIRECTORY: "graphql/server"
        run: |
          source env-vars
          make push-portal-component

  docker-build-and-push-authentication-server:
    runs-on: ubuntu-latest
    needs:
      - get-envs
      - backend-checks
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Downloading image artficate
        uses: actions/download-artifact@v4
        with:
          name: env_artifact
          path: chaoscenter

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
        with:
          version: latest

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push auth server docker image
        env:
          IMAGE_NAME: ${AUTHENTICATION_SERVER_IMAGE}
          IMG_TAG: ${IMG_TAG}
          PLATFORMS: ${{ secrets.PLATFORMS }}
          REPONAME: ${{ secrets.REPONAME }}
          DIRECTORY: "authentication"
        run: |
          source env-vars
          make push-portal-component

  docker-build-and-push-subscriber:
    runs-on: ubuntu-latest
    needs:
      - get-envs
      - backend-checks
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Downloading image artficate
        uses: actions/download-artifact@v4
        with:
          name: env_artifact
          path: chaoscenter

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
        with:
          version: latest

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push subscriber docker image
        env:
          IMAGE_NAME: ${SUBSCRIBER_IMAGE}
          IMG_TAG: ${IMG_TAG}
          PLATFORMS: ${{ secrets.PLATFORMS }}
          REPONAME: ${{ secrets.REPONAME }}
          DIRECTORY: "subscriber"
        run: |
          source env-vars
          make push-portal-component

  docker-build-and-push-event-tracker:
    runs-on: ubuntu-latest
    needs:
      - get-envs
      - backend-checks
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Downloading image artficate
        uses: actions/download-artifact@v4
        with:
          name: env_artifact
          path: chaoscenter

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
        with:
          version: latest

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push event tracker docker image
        env:
          IMAGE_NAME: ${EVENT_TRACKER}
          IMG_TAG: ${IMG_TAG}
          PLATFORMS: ${{ secrets.PLATFORMS }}
          REPONAME: ${{ secrets.REPONAME }}
          DIRECTORY: "event-tracker"
        run: |
          source env-vars
          make push-portal-component

  ## TODO: the build time for multiarch frontend image is higher so it is used in the release
  ## builds only need to optimize the build duration and also add it with ci tag
  ## Build and push / base path
  docker-build-and-push-frontend:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        frontend:
          [
            { "image_name": "litmusportal-frontend", "path": "" },
            {
              "image_name": "litmusportal-frontend-path-litmuschaos",
              "path": "/litmuschaos",
            },
          ]
    needs:
      - get-envs
      - frontend-checks
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Downloading image artficate
        uses: actions/download-artifact@v4
        with:
          name: env_artifact
          path: chaoscenter

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
        with:
          version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: 16

      - name: yarn build
        run: |
          cd web && yarn && yarn build

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push frontend docker image ( ${{ matrix.frontend.image_name }} )
        env:
          IMG_TAG: ${IMG_TAG}
          PLATFORMS: ${{ secrets.PLATFORMS }}
          REPONAME: ${{ secrets.REPONAME }}
          timestamp: ${timestamp}
          PUBLIC_URL: ${{ matrix.frontend.path }}
        run: |
          source env-vars
          FRONTEND_IMAGE=${{ matrix.frontend.image_name }}
          timestamp=`date "+%s"`
          make push-frontend
