# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

name: Comps jobs
permissions: read-all
on:
  workflow_call:
    inputs:
      service:
        required: true
        type: string
      tag:
        default: "comps"
        required: false
        type: string
      build:
        default: true
        required: false
        type: boolean
      test:
        default: true
        description: "Test comps with docker compose"
        required: false
        type: boolean
      mode:
        default: "CD"
        description: "Whether the test range is CI or CD"
        required: false
        type: string

jobs:
  ####################################################################################################
  # Image Build
  ####################################################################################################
  build-images:
    runs-on: "docker-build-gaudi"
    continue-on-error: true
    outputs:
      file_exists: ${{ steps.get-yaml-path.outputs.file_exists }}
    steps:
      - name: Clean Up Working Directory
        run: sudo rm -rf ${{github.workspace}}/*

      - name: Checkout out Repo
        uses: actions/checkout@v4

      - name: Clone required Repo
        id: get-yaml-path
        run: |
          cd ${{ github.workspace }}/.github/workflows/docker/compose
          # service=$(echo ${{ inputs.service }} | cut -d'_' -f1)
          if [[ "${{ inputs.mode }}" == "CD" ]]; then
            docker_compose_yml=${{ github.workspace }}/.github/workflows/docker/compose/${{ inputs.service }}-compose-cd.yaml
          else
            docker_compose_yml=${{ github.workspace }}/.github/workflows/docker/compose/${{ inputs.service }}-compose.yaml
          fi
          echo "docker_compose_path=${docker_compose_yml}" >> $GITHUB_OUTPUT
          if [ -f "$docker_compose_yml" ]; then
            echo "file_exists=true" >> $GITHUB_OUTPUT
          else
            echo "There is no ${{ inputs.mode }} part of ${{ inputs.service }} that needs to be executed."
            echo "file_exists=false" >> $GITHUB_OUTPUT
          fi

          cd ${{ github.workspace }}
          if [[ $(grep -c "vllm-openvino:" ${docker_compose_yml}) != 0 ]]; then
              git clone https://github.com/vllm-project/vllm.git vllm-openvino
              cd ./vllm-openvino && git checkout v0.6.1 && cd ../
          fi

      - name: Build Image
        if: ${{ fromJSON(inputs.build) && steps.get-yaml-path.outputs.file_exists == 'true' }}
        uses: opea-project/validation/actions/image-build@main
        with:
          work_dir: ${{ github.workspace }}
          docker_compose_path: ${{ steps.get-yaml-path.outputs.docker_compose_path }}
          registry: ${OPEA_IMAGE_REPO}opea
          tag: ${{ inputs.tag }}

  ####################################################################################################
  # Docker Compose Test
  ####################################################################################################
  test-service-compose:
    needs: [build-images]
    if: ${{ fromJSON(inputs.test) }}
    uses: ./.github/workflows/_run-docker-compose.yml
    with:
      tag: ${{ inputs.tag }}
      service: ${{ inputs.service }}
    secrets: inherit
