trigger:
  branches:
    include: [develop, release/*]
  paths:
    exclude:
      - deployment/*
    include:
      - "*"
pr:
  branches:
    include: ["*"]
  paths:
    exclude:
      - deployment/*
    include:
      - "*"

resources:
  containers:
    - container: node
      image: packagerepository.sagaws.net:8082/safelite/node-build:15
    - container: awscli
      image: packagerepository.sagaws.net:8082/safelite/awscli2-build:3.9
    - container: prettier-node
      image: packagerepository.sagaws.net:8082/safelite/prettier-node-build:18
  repositories:
    - repository: AzureDevOps
      type: github
      name: Safelite/AzureDevOps
      endpoint: Safelite
      ref: refs/tags/t5.5.40

variables:
  - group: Digital-Infrastructure
  - group: FixMyGlass-BuildBranches

stages:
  # PR's
  - ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
    - stage: TestFormat
      displayName: Test Code Format
      jobs:
        - job: checkFormatting
          displayName: Check formatting
          container: prettier-node
          steps:
            - bash: prettier --check "$(Build.SourcesDirectory)/src/**/*.(js|vue)"
              displayName: Run Prettier check
            
    - stage: TestPr
      displayName: Run Unit Tests For PullRequest
      jobs:
        - template: templates/digital/vue-jest-run-unit-tests.yml@AzureDevOps
          parameters:
            nodeContainer: node
            npmLocation: $(Build.SourcesDirectory)
            testResultsFile: junit.xml
            summaryFileLocation: $(Build.SourcesDirectory)/coverage/cobertura-coverage.xml
  - ${{ else }}:
      # Dev Build/Deploy
      - stage: Dev
        condition: eq(variables['Build.SourceBranch'], variables['dev-branch'] )
        variables:
          - group: FixMyGlassDev
        jobs:
          - deployment: devBuildDeployment
            displayName: Build and Deploy FMG - Dev
            environment: digitalCloud-dev
            container: node
            workspace:
              clean: all
            strategy:
              runOnce:
                deploy:
                  steps:
                    - checkout: self
                      clean: true
                    - template: templates/digital/step-build-vue.yml@AzureDevOps
                      parameters:
                        buildOutputDir: dist
                        environment: Dev
                    - template: templates/digital/step-deploy-vue.yml@AzureDevOps
                      parameters:
                        artifactName: vueDistDev
                        awsProfile: $(devDeploymentProfile)
                        outputPath: /fmg/
                        deployBuckets:
                          safelite-dev-fmg-us-east-1:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-1
                          safelite-dev-fmg-us-east-2:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-2
                        appDeployVariables:
                          __VUE_APP_CONSUMER_CF_DISTRO__: $(__VUE_APP_CONSUMER_CF_DISTRO__)
                          __VUE_APP_GOOGLE_PLACES_API_KEY__: $(__VUE_APP_GOOGLE_PLACES_API_KEY__)
                          __VUE_APP_HERITAGE_FUNNEL__: $(__VUE_APP_HERITAGE_FUNNEL__)
                          __VUE_APP_CURRENT_ENVIRONMENT__: $(__VUE_APP_CURRENT_ENVIRONMENT__)
                        indexDeployVariables:
                          __VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__: $(__VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__)
                          __VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__: $(__VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters:
                        awsCliContainer: awscli
                        distributionId: $(apiCfDistributionId)
                        paths: /* 
                        awsProfile: $(devDeploymentProfile)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters: 
                        awsCliContainer: awscli
                        distributionId: $(cfDistributionId)
                        paths: /fmg/*
                        awsProfile: $(devDeploymentProfile)

      # Test Build/Deploy                      
      - stage: Test
        condition: eq(variables['Build.SourceBranch'], variables['test-branch'] )
        variables:
          - group: FixMyGlassTest
        jobs:
          - deployment: testBuildDeployment
            displayName: Build and Deploy FMG - Test
            environment: NoApproval-All
            container: node
            workspace:
              clean: all
            strategy:
              runOnce:
                deploy:
                  steps:
                    - checkout: self
                      clean: true
                    - template: templates/digital/step-build-vue.yml@AzureDevOps
                      parameters:
                        buildOutputDir: dist
                        environment: Test
                    - template: templates/digital/step-deploy-vue.yml@AzureDevOps
                      parameters:
                        artifactName: vueDistTest
                        awsProfile: $(sysDeploymentProfile)
                        outputPath: /fmg/
                        deployBuckets:
                          safelite-sys-fmg-us-east-1:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-1
                          safelite-sys-fmg-us-east-2:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-2
                        appDeployVariables:
                          __VUE_APP_CONSUMER_CF_DISTRO__: $(__VUE_APP_CONSUMER_CF_DISTRO__)
                          __VUE_APP_GOOGLE_PLACES_API_KEY__: $(__VUE_APP_GOOGLE_PLACES_API_KEY__)
                          __VUE_APP_HERITAGE_FUNNEL__: $(__VUE_APP_HERITAGE_FUNNEL__)
                          __VUE_APP_CURRENT_ENVIRONMENT__: $(__VUE_APP_CURRENT_ENVIRONMENT__)
                        indexDeployVariables:
                          __VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__: $(__VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__)
                          __VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__: $(__VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters:
                        awsCliContainer: awscli
                        distributionId: $(apiCfDistributionId)
                        paths: /* 
                        awsProfile: $(sysDeploymentProfile)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters: 
                        awsCliContainer: awscli
                        distributionId: $(cfDistributionId)
                        paths: /fmg/*
                        awsProfile: $(sysDeploymentProfile)
                
      # QA Build/Deploy
      - stage: Qa
        condition: eq(variables['Build.SourceBranch'], variables['qa-branch'] )
        variables:
          - group: FixMyGlassQa
        jobs:
          - deployment: qaBuildDeployment
            displayName: Build and Deploy FMG - QA
            environment: NoApproval-All
            container: node
            workspace:
              clean: all
            strategy:
              runOnce:
                deploy:
                  steps:
                    - checkout: self
                      clean: true
                    - template: templates/digital/step-build-vue.yml@AzureDevOps
                      parameters:
                        buildOutputDir: dist
                        environment: Qa
                    - template: templates/digital/step-deploy-vue.yml@AzureDevOps
                      parameters:
                        artifactName: vueDistQa
                        awsProfile: $(qaDeploymentProfile)
                        outputPath: /fmg/
                        deployBuckets:
                          safelite-qa-fmg-us-east-1:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-1
                          safelite-qa-fmg-us-east-2:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-2
                        appDeployVariables:
                          __VUE_APP_CONSUMER_CF_DISTRO__: $(__VUE_APP_CONSUMER_CF_DISTRO__)
                          __VUE_APP_GOOGLE_PLACES_API_KEY__: $(__VUE_APP_GOOGLE_PLACES_API_KEY__)
                          __VUE_APP_HERITAGE_FUNNEL__: $(__VUE_APP_HERITAGE_FUNNEL__)
                          __VUE_APP_CURRENT_ENVIRONMENT__: $(__VUE_APP_CURRENT_ENVIRONMENT__)
                        indexDeployVariables:
                          __VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__: $(__VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__)
                          __VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__: $(__VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters:
                        awsCliContainer: awscli
                        distributionId: $(apiCfDistributionId)
                        paths: /* 
                        awsProfile: $(qaDeploymentProfile)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters: 
                        awsCliContainer: awscli
                        distributionId: $(cfDistributionId)
                        paths: /fmg/*
                        awsProfile: $(qaDeploymentProfile)

      # Prod Build/Deploy
      - stage: Prod
        condition: succeeded('Qa')
        variables:
          - group: FixMyGlassProd
        jobs:
          - deployment: prodBuildDeployment
            displayName: Build and Deploy FMG - Prod
            environment: digitalCloud-prod
            container: node
            workspace:
              clean: all
            strategy:
              runOnce:
                deploy:
                  steps:
                    - checkout: self
                      clean: true
                    - template: templates/digital/step-build-vue.yml@AzureDevOps
                      parameters:
                        buildOutputDir: dist
                        environment: Prod
                    - template: templates/digital/step-deploy-vue.yml@AzureDevOps
                      parameters:
                        artifactName: vueDistProd
                        awsProfile: $(prodDeploymentProfile)
                        outputPath: /fmg/
                        deployBuckets:
                          safelite-prod-fmg-us-east-1:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-1
                          safelite-prod-fmg-us-east-2:
                            clearFolder: true
                            deployFolder: ""
                            region: us-east-2
                        appDeployVariables:
                          __VUE_APP_CONSUMER_CF_DISTRO__: $(__VUE_APP_CONSUMER_CF_DISTRO__)
                          __VUE_APP_GOOGLE_PLACES_API_KEY__: $(__VUE_APP_GOOGLE_PLACES_API_KEY__)
                          __VUE_APP_HERITAGE_FUNNEL__: $(__VUE_APP_HERITAGE_FUNNEL__)
                          __VUE_APP_CURRENT_ENVIRONMENT__: $(__VUE_APP_CURRENT_ENVIRONMENT__)
                        indexDeployVariables:
                          __VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__: $(__VUE_APP_GOOGLE_TAG_MANAGER_SCRIPT_BODY__)
                          __VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__: $(__VUE_APP_GOOGLE_TAG_MANAGER_NOSCRIPT_FRAME_SRC__)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters:
                        awsCliContainer: awscli
                        distributionId: $(apiCfDistributionId)
                        paths: /* 
                        awsProfile: $(prodDeploymentProfile)
                    - template: templates/digital/invalidate-cloudfront-cache.yml@AzureDevOps
                      parameters: 
                        awsCliContainer: awscli
                        distributionId: $(cfDistributionId)
                        paths: /fmg/*
                        awsProfile: $(prodDeploymentProfile)
          - template: templates/digital/auto-tag.yml@AzureDevOps
            parameters:
              dependsOn: prodBuildDeployment
              userName: SafeliteAzureDevops
              userEmail: githubazuredevops@safelite.com
