trigger:
  branches:
    include:
    - fqa

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

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

name: 0.1.$(Rev:r)

variables:
- template: oke_agents_atendimento/variables.yaml@templates
- group: tim-ai-contas-agnt-dev-fqa
- group: sa-timsa-it-agnt-ai-atendimento-cicd-fqa-auth-token
- name: imageRepository
  value: '$(ociRegistry)/$(OCI_NAMESPACE)/agnt-ai-atendimento/fqa/agnt-ai-atendimento-contas'
- name: K8S_NAMESPACE
  value: 'agnt-ai-atendimento-contas'
- name: DNS
  value: agt-ai-atendimento-contas-fqa.internal.timbrasil.com.br
- name: IP_ADB
  value: "10.152.101.173"
- name: HOST_ADB_1
  value: "vkmt14dd.adb.sa-saopaulo-1.oraclecloud.com"
- name: HOST_ADB_2
  value: "vkmt14dd.adb.sa-saopaulo-1.oraclecloudapps.com"

stages:
- stage: FqaBuild
  displayName: 'Build & Push (FQA)'
  jobs:
  - job: Build
    steps:
    - template: oke_agents_atendimento/certs_add.yaml@templates
    - script: |
        echo "$(auth-token)" | docker login $(ociRegistry) -u "$(OCI_NAMESPACE)/$(SA_NAME)" --password-stdin
      displayName: 'Login to OCI Registry'

    - task: Docker@2
      displayName: 'Build the image'
      inputs:
        command: 'build'
        repository: $(imageRepository)
        dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
        arguments: '--build-arg UV_INDEX_TIMBRASIL_USERNAME=XGH --build-arg UV_INDEX_TIMBRASIL_PASSWORD=$(System.AccessToken)'
        tags: |
          $(Build.BuildNumber)

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

- stage: FqaDeploy
  dependsOn: FqaBuild
  condition: succeeded()
  displayName: 'Deploy to OKE (FQA)'
  jobs:
  - job: deployb8s
    pool:
      name: oci-kubernetes
    variables:
      IMAGE_REPOSITORY: '$(imageRepository)'
      IMAGE_TAG: '$(Build.BuildNumber)'
      APP_NAME: '$(Build.Repository.Name)'
      OCI_CLI_CONFIG_FILE: '$(Pipeline.Workspace)/oci_config'
      IP_CLUSTER_AI: '10.152.97.126'
      HOSTNAME_CLUSTER_AI: 'pegruagntaiatenddev.pe.inference.generativeai.sa-saopaulo-1.oci.oraclecloud.com'

    steps:
    - task: DownloadSecureFile@1
      name: appOciPemKey
      displayName: 'Download OCI PEM Key for App Container'
      inputs:
        secureFile: 'sa-agnt-ai-atendimento-contas-fqa-key'

    - template: oke_agents_atendimento/adicionando_secret_ai.yaml@templates
      parameters:
        configFilePath: 'k8s/artefatos_k8s/oracle_config_ai_fqa'

    - template: oke_agents_atendimento/autenticacao_kubernetes_v2.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: 'Apply application secrets'
      env:
        TIM_BLOQUEIO_AUTH: $(TIM_BLOQUEIO_AUTH)
        TIM_CANCELAMENTO_AUTH: $(TIM_CANCELAMENTO_AUTH)
        TIM_COMPLETE_INVOICES_AUTH: $(TIM_COMPLETE_INVOICES_AUTH)
        TIM_CONSULTA_AUTH: $(TIM_CONSULTA_AUTH)
        TIM_CONTRATO_AUTH: $(TIM_CONTRATO_AUTH)
        TIM_PROFILE_BILL_AUTH: $(TIM_PROFILE_BILL_AUTH)
        TIM_PROFILE_FULL_AUTH: $(TIM_PROFILE_FULL_AUTH)
        TIM_PROTOCOL_AUTH: $(TIM_PROTOCOL_AUTH)
        TIM_SERVICE_REQUEST_STATUS_AUTH: $(TIM_SERVICE_REQUEST_STATUS_AUTH)
        TIM_LLM_GATEWAY_LANGFUSE_SECRET_KEY: $(TIM_LLM_GATEWAY_LANGFUSE_SECRET_KEY_FQA)
        TIM_TRACKING_ACTIVITIES_AUTH: $(TIM_TRACKING_ACTIVITIES_AUTH)
        TIM_SMS_AUTH: $(TIM_SMS_AUTH)
        TIM_INVOICE_RECOVER_AUTH: $(TIM_INVOICE_RECOVER_AUTH)
        TIM_VAS_HISTORY_AUTH: $(TIM_VAS_HISTORY_AUTH)
        TIM_OBSERVABILITY_ENCRYPTION_KEY: $(TIM_OBSERVABILITY_ENCRYPTION_KEY)

    - script: |
        set -eo pipefail

        kubectl create secret generic ${APP_NAME}-api-secrets-extras \
          --from-literal=TIM_RAG_DB_PASSWORD="${TIM_RAG_DB_PASSWORD}" \
          -n $(K8S_NAMESPACE) \
          --dry-run=client -o yaml | kubectl apply -f -
      displayName: 'Secrets with special characters'
      env:
        TIM_RAG_DB_PASSWORD: $(TIM_RAG_DB_PASSWORD)

    - template: oke_agents_atendimento/apply_env_as_configmap.yaml@templates
      parameters:
        envFile: '.env.kube.fqa'

    - template: oke_agents_atendimento/helm_deploy.yaml@templates
      parameters:
        timeout: '180s'
