trigger:
  branches:
    include:
    - develop
  # paths:
  #   exclude:
  #   - azure-pipelines/*

pool:
  name: Azure Pipelines
  vmImage: 'ubuntu-latest'

resources:
  repositories:
  - repository: templates
    type: git
    name: Data_Science_AI_ML/template-yaml-devops-mlops
    ref: master

variables:
- template: oke_agents_atendimento/variables.yaml@templates
- group: sa-timsa-it-agnt-ai-atendimento-cicd-dev-auth-token
- group: tim-ai-atend-agnt-integ-tia-dev-fqa
- name: imageRepository
  value: '$(ociRegistry)/$(OCI_NAMESPACE)/agnt-ai-atendimento/dev/agnt-ai-atendimento-tia'
- name: imageLivekit
  value: '$(ociRegistry)/$(OCI_NAMESPACE)/agnt-ai-atendimento/dev/agnt-ai-atendimento-tia-livekit'
- name: DNS
  value: agt-ai-atendimento-tia-dev.internal.timbrasil.com.br
- name: REDIS_IP
  value: 10.152.96.51
- name: REDIS_HOST
  value: aaaaehl73aayzoctg2kvzc5xphvuiryrtwelktfaxcc3ill4cs7ml5a-p.redis.sa-saopaulo-1.oci.oraclecloud.com
# Livekit Config
- name: LIVEKIT_REPLICAS
  value: 2
- name: CPU_LIVEKIT_REQ
  value: 1
- name: CPU_LIVEKIT_LIM
  value: 2
- name: MEM_LIVEKIT_REQ
  value: 2Gi
- name: MEM_LIVEKIT_LIM
  value: 4Gi
# TIA Config
- name: TIA_REPLICAS
  value: 2
- name: CPU_TIA_REQ
  value: 1
- name: CPU_TIA_LIM
  value: 2
- name: MEM_TIA_REQ
  value: 4Gi
- name: MEM_TIA_LIM
  value: 6Gi
# TIA Bridge Config
- name: CPU_TIA_BRIDGE_REQ
  value: 1
- name: CPU_TIA_BRIDGE_LIM
  value: 2
- name: MEM_TIA_BRIDGE_REQ
  value: 2Gi
- name: MEM_TIA_BRIDGE_LIM
  value: 4Gi

name: 0.0.$(Rev:r)

stages:
- stage: Build
  jobs:
  - job: Build
    steps:
    - script: |
        echo "$(auth-token)" | docker login $(ociRegistry) -u "$(OCI_NAMESPACE)/$(SA_NAME)" --password-stdin
      displayName: 'Login to OCI Registry'

    - script: |
        set -eo pipefail
        
        docker build -f $(Build.SourcesDirectory)/k8s/tia/Dockerfile -t $(imageRepository):$(Build.BuildNumber) $(Build.SourcesDirectory)

        docker build -f $(Build.SourcesDirectory)/k8s/livekit/Dockerfile -t $(imageLivekit):$(Build.BuildNumber) $(Build.SourcesDirectory)
      displayName: 'Build the image'

    - script: |
        docker push $(imageRepository):$(Build.BuildNumber)
        docker push $(imageLivekit):$(Build.BuildNumber)
      displayName: 'Push image to OCI Registry'

- stage: Deploy
  dependsOn: Build
  condition: succeeded()
  jobs:
  - job: deployb8s
    pool:
      name: oci-kubernetes
    variables:
      K8S_NAMESPACE: 'agnt-ai-atendimento-tia'
      IMAGE_REPOSITORY: '$(imageRepository)'
      IMAGE_REPOSITORY_LIVEKIT: '$(imageLivekit)'
      IMAGE_TAG: '$(Build.BuildNumber)'
      APP_NAME: '$(Build.Repository.Name)'
      OCI_CLI_CONFIG_FILE: '$(Pipeline.Workspace)/oci_config'

    steps:

    - template: oke_agents_atendimento/autenticacao_kubernetes_v2.yaml@templates

    - template: oke_agents_atendimento/apply_env_as_configmap.yaml@templates
  
    - template: oke_agents_atendimento/sa_gcp_secret.yaml@templates

    - script: |
        set -eo pipefail
        envsubst < k8s/secrets.yaml | kubectl apply -n $(K8S_NAMESPACE) -f -
      displayName: "aplicando secrets"
      env:
        AZURE_SPEECH_KEY: $(AZURE_SPEECH_KEY)
        XAI_API_KEY: $(XAI_API_KEY_DEV)

    - script: |
        set -eo pipefail

        # cada pipeline que vamos querer deployar
        mkdir -p helm_build/templates
        envsubst < k8s/livekit/configmap.yaml > helm_build/templates/configmap-livekit.yaml
        envsubst < k8s/livekit/deployment.yaml > helm_build/templates/deployment-livekit.yaml
        envsubst < k8s/tia/deployment.yaml > helm_build/templates/deployment-tia.yaml
        
        envsubst < k8s/helm_deploy.yaml > helm_build/Chart.yaml
        helm upgrade --install $(APP_NAME) ./helm_build \
          --namespace $(K8S_NAMESPACE) \
          --wait \
          --timeout 270s \
          --atomic
      displayName: 'Apply kube custom helm'
