# Deploy to Azure Kubernetes Service
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
  tags:
    include:
    - 'p-*'
  branches:
    exclude:
    - '*'

resources:
- repo: self

variables:

  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'aosprodacr'
  imageRepository: 'extranetui'
  containerRegistry: 'aosprodacr.azurecr.io'
  dockerfilePath: './prod.Dockerfile'
  tag: 'prod-$(Build.BuildId)'
  imagePullSecret: 'acr-pull'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'


stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)

    - upload: kustomize/manifests/prod
      artifact: kustomize/manifests/prod

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build

  jobs:
  - deployment: Deploy
    displayName: Deploy
    pool:
      vmImage: $(vmImageName)
    environment: 'aos-prod-aks'
    strategy:
      runOnce:
        deploy:
          steps:
          - task: KubernetesManifest@0
            displayName: Create imagePullSecret
            inputs:
              connectionType: 'Kubernetes Service Connection'
              kubernetesServiceConnection: 'aos-prod-aks'
              namespace: 'prod'
              action: createSecret
              secretName: $(imagePullSecret)
              dockerRegistryEndpoint: $(dockerRegistryServiceConnection)

          - task: KubernetesManifest@0
            displayName: Deploy to Kubernetes cluster
            inputs:
              connectionType: 'Kubernetes Service Connection'
              kubernetesServiceConnection: 'aos-prod-aks'
              namespace: 'prod'
              action: deploy
              manifests: |
                $(Pipeline.Workspace)/kustomize/manifests/prod/prod.yaml
              imagePullSecrets: |
                $(imagePullSecret)
              containers: |
                $(containerRegistry)/$(imageRepository):$(tag)

