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

name: Example jobs
permissions: read-all
on:
  workflow_call:
    inputs:
      node:
        required: true
        type: string
      example:
        required: true
        type: string
      services:
        default: ""
        required: false
        type: string
      tag:
        default: "latest"
        required: false
        type: string
      build:
        default: true
        required: false
        type: boolean
      test_compose:
        default: false
        required: false
        type: boolean
      test_k8s:
        default: false
        required: false
        type: boolean
      test_gmc:
        default: false
        required: false
        type: boolean
      opea_branch:
        default: "main"
        required: false
        type: string
      inject_commit:
        default: false
        required: false
        type: string

jobs:
####################################################################################################
# Image Build
####################################################################################################
  build-images:
    runs-on: "docker-build-${{ inputs.node }}"
    steps:
      - name: Clean Up Working Directory
        run: sudo rm -rf ${{github.workspace}}/*

      - name: Get Checkout Ref
        run: |
          if [ "${{ github.event_name }}" == "pull_request" ] || [ "${{ github.event_name }}" == "pull_request_target" ]; then
            echo "CHECKOUT_REF=refs/pull/${{ github.event.number }}/merge" >> $GITHUB_ENV
          else
            echo "CHECKOUT_REF=${{ github.ref }}" >> $GITHUB_ENV
          fi

      - name: Checkout out GenAIExamples
        uses: actions/checkout@v4
        with:
          ref: ${{ env.CHECKOUT_REF }}
          fetch-depth: 0

      - name: Clone Required Repo
        run: |
          cd ${{ github.workspace }}/${{ inputs.example }}/docker_image_build
          docker_compose_path=${{ github.workspace }}/${{ inputs.example }}/docker_image_build/build.yaml
          if [[ $(grep -c "vllm:" ${docker_compose_path}) != 0 ]]; then
              git clone https://github.com/vllm-project/vllm.git
              cd vllm && git rev-parse HEAD && cd ../
          fi
          if [[ $(grep -c "vllm-gaudi:" ${docker_compose_path}) != 0 ]]; then
               git clone https://github.com/HabanaAI/vllm-fork.git
               cd vllm-fork && git checkout 3c39626 && cd ../
          fi
          git clone https://github.com/opea-project/GenAIComps.git
          cd GenAIComps && git checkout ${{ inputs.opea_branch }} && git rev-parse HEAD && cd ../

      - name: Build Image
        if: ${{ fromJSON(inputs.build) }}
        uses: opea-project/validation/actions/image-build@main
        with:
          work_dir: ${{ github.workspace }}/${{ inputs.example }}/docker_image_build
          docker_compose_path: ${{ github.workspace }}/${{ inputs.example }}/docker_image_build/build.yaml
          service_list: ${{ inputs.services }}
          registry: ${OPEA_IMAGE_REPO}opea
          inject_commit: ${{ inputs.inject_commit }}
          tag: ${{ inputs.tag }}

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


####################################################################################################
# K8S Test
####################################################################################################
  test-k8s-manifest:
    needs: [build-images]
    if: ${{ fromJSON(inputs.test_k8s) }}
    uses: ./.github/workflows/_manifest-e2e.yml
    with:
      example: ${{ inputs.example }}
      hardware: ${{ inputs.node }}
      tag: ${{ inputs.tag }}
    secrets: inherit

####################################################################################################
# GMC Test
####################################################################################################
  test-gmc-pipeline:
    needs: [build-images]
    if: ${{ fromJSON(inputs.test_gmc) }}
    uses: ./.github/workflows/_gmc-e2e.yml
    with:
      example: ${{ inputs.example }}
      hardware: ${{ inputs.node }}
    secrets: inherit
