parameters:
- name: OverrideBlockOnPossibleDataLoss
  displayName: Add /p:BlockOnPossibleDataLoss=false argument to DACPAC deployment
  type: boolean
  default: false

trigger:
  batch: true
  branches:
    include:
      - "master"

variables:
- name: SolutionBaseName
  value: SFA.DAS.ProviderApprenticeshipsService
- name: DatabaseName
  value: SFA.DAS.ProviderAgreementStatus.Database
- name: BuildConfiguration
  value: release
- name: BuildPlatform
  value: any cpu
- group: Release Management Resources
- group: Release das-providerapprenticeshipsservice
- name: Deploy
  value: $[or(eq(variables['Build.SourceBranch'], 'refs/heads/main'), eq(variables['Build.Reason'], 'Manual'), eq(variables['Build.Reason'], 'PullRequest'))]

resources:
  repositories:
  - repository: self
  - repository: das-platform-building-blocks
    type: github
    name: SkillsFundingAgency/das-platform-building-blocks
    ref: refs/tags/2.1.28
    endpoint: SkillsFundingAgency
  - repository: das-platform-automation
    type: github
    name: SkillsFundingAgency/das-platform-automation
    ref: refs/tags/5.1.8
    endpoint: SkillsFundingAgency
  pipelines:
  - pipeline: das-employer-config
    project: Digital Apprenticeship Service
    source: das-employer-config
    branch: master

stages:
- stage: Build
  jobs:
  - template: pipeline-templates/job/code-build.yml
    parameters:
      BuildConfiguration: $(BuildConfiguration)
      SolutionBaseName: $(SolutionBaseName)
  - template: azure-pipelines-templates/build/job/dacpac-template.yml@das-platform-building-blocks
    parameters:
      SolutionBaseName: $(SolutionBaseName).dacpac
      SqlProjectPath: src/$(DatabaseName)/$(DatabaseName).sqlproj

- stage: Deploy_AT
  dependsOn: Build
  displayName: Deploy to AT
  condition: and(succeeded(), eq(variables.Deploy, 'true'))
  variables:
  - group: DevTest Management Resources
  - group: AT DevTest Shared Resources
  - group: AT das-providerapprenticeshipsservice
  jobs:
  - template: pipeline-templates/job/deploy.yml
    parameters:
      Environment: AT
      SolutionBaseName: $(SolutionBaseName)
      ServiceConnection: SFA-DAS-DevTest-ARM
      OverrideBlockOnPossibleDataLoss: ${{ parameters.OverrideBlockOnPossibleDataLoss  }}
      AppRoleAssignmentsServiceConnection: das-app-role-assignments-CDS
  
- stage: Deploy_TEST
  dependsOn: Build
  displayName: Deploy to TEST
  variables:
  - group: DevTest Management Resources
  - group: TEST DevTest Shared Resources
  - group: TEST das-providerapprenticeshipsservice
  jobs:
  - template: pipeline-templates/job/deploy.yml
    parameters:
      Environment: TEST
      SolutionBaseName: $(SolutionBaseName)
      ServiceConnection: SFA-DAS-DevTest-ARM
      OverrideBlockOnPossibleDataLoss: ${{ parameters.OverrideBlockOnPossibleDataLoss  }}
      AppRoleAssignmentsServiceConnection: das-app-role-assignments-CDS
    
- stage: Deploy_TEST2
  dependsOn: Build
  displayName: Deploy to TEST2
  variables:
  - group: DevTest Management Resources
  - group: TEST2 DevTest Shared Resources
  - group: TEST2 das-providerapprenticeshipsservice
  jobs:
  - template: pipeline-templates/job/deploy.yml
    parameters:
      Environment: TEST2
      SolutionBaseName: $(SolutionBaseName)
      ServiceConnection: SFA-DAS-DevTest-ARM
      OverrideBlockOnPossibleDataLoss: ${{ parameters.OverrideBlockOnPossibleDataLoss  }}
      AppRoleAssignmentsServiceConnection: das-app-role-assignments-CDS

- stage: Deploy_PP
  dependsOn: Build
  displayName: Deploy to PP
  variables:
    - group: PreProd Management Resources
    - group: PreProd Shared Resources
    - group: PREPROD das-providerapprenticeshipsservice
  jobs:
    - template: pipeline-templates/job/deploy.yml
      parameters:
        Environment: PP
        SolutionBaseName: $(SolutionBaseName)
        ServiceConnection: SFA-DIG-PreProd-ARM
        AppRoleAssignmentsServiceConnection: das-app-role-assignments-FCS
        OverrideBlockOnPossibleDataLoss: ${{ parameters.OverrideBlockOnPossibleDataLoss  }}

- stage: Deploy_PROD
  dependsOn: Build
  displayName: Deploy to PROD
  variables:
  - group: PROD Management Resources
  - group: PROD Shared Resources
  - group: PROD das-providerapprenticeshipsservice
  jobs:
  - template: pipeline-templates/job/deploy.yml
    parameters:
      Environment: PROD
      SolutionBaseName: $(SolutionBaseName)
      ServiceConnection: SFA-DIG-Prod-ARM
      AppRoleAssignmentsServiceConnection: das-app-role-assignments-FCS
      OverrideBlockOnPossibleDataLoss: ${{ parameters.OverrideBlockOnPossibleDataLoss  }}

- stage: Deploy_MO
  dependsOn: Build
  displayName: Deploy to MO
  variables:
  - group: MO Management Resources
  - group: MO Shared Resources
  - group: MO das-providerapprenticeshipsservice
  jobs:
  - template: pipeline-templates/job/deploy.yml
    parameters:
      Environment: MO
      SolutionBaseName: $(SolutionBaseName)
      ServiceConnection: SFA-ASM-ModelOffice-ARM
      AppRoleAssignmentsServiceConnection: das-app-role-assignments-FCS
      OverrideBlockOnPossibleDataLoss: ${{ parameters.OverrideBlockOnPossibleDataLoss  }}

- stage: Deploy_DEMO
  dependsOn: Build
  displayName: Deploy to DEMO
  variables:
  - group: DevTest Management Resources
  - group: DEMO DevTest Shared Resources
  - group: DEMO das-providerapprenticeshipsservice
  jobs:
  - template: pipeline-templates/job/deploy.yml
    parameters:
      Environment: DEMO
      SolutionBaseName: $(SolutionBaseName)
      ServiceConnection: SFA-DAS-DevTest-ARM
      OverrideBlockOnPossibleDataLoss: ${{ parameters.OverrideBlockOnPossibleDataLoss  }}
      AppRoleAssignmentsServiceConnection: das-app-role-assignments-CDS