trigger:
- master

variables:

  vmImage: 'ubuntu-latest'
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: 'pvs-docker-registry' ## Review Service Connections
  containerRegistry: 'omsapvsregistry.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  repository: 'pvs-is-fe'

  tag: 'v42'

  kubernetesServiceConnection: 'kubernetes-nonProduction'
  k8sNamespace: 'pvs-uat'
  impagePullSecret: 'acrpull'
  imageName: '$(containerRegistry)/$(repository)'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:  
  - job: Build
    displayName: Build job
    pool:
      vmImage: $(vmImage)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        repository: $(repository)
        tags: $(tag)
    
    - task: PublishPipelineArtifact@1
      inputs:
        artifactName: 'manifests'
        path: 'manifests'

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  jobs:
  - deployment: Deploy
    displayName: Deploy job
    pool: 
      vmImage: $(vmImage)
    environment: pvs-is-fe
    strategy:
      runOnce:
        deploy:
          steps:
          - task: DownloadPipelineArtifact@2
            inputs:
              artifactName: 'manifests'
              downloadPath: '$(Sytem.ArtifactDirectory)/manifests'
              
          - task: KubernetesManifest@0
            displayName: Create imagePullSecret
            inputs:
              kubernetesServiceConnection: $(kubernetesServiceConnection)
              action: createSecret
              secretName: $(impagePullSecret)
              namespace: $(k8sNamespace)
              dockerRegistryEndpoint: $(dockerRegistryServiceConnection)
          
          - task: KubernetesManifest@0
            displayName: Deploy to Kubernetes Cluster
            inputs:
              action: deploy
              kubernetesServiceConnection: $(kubernetesServiceConnection)
              namespace: $(k8sNamespace)
              manifests: |
                $(Sytem.ArtifactDirectory)/manifests/deployment.yml
              imagePullSecrets: |
                $(impagePullSecret)
              containers: |
                $(imageName):$(tag)