trigger:
 branches:
    include:
    - sprint
 paths:
    include:
    - futurenhs.content.data/*

jobs:

- job: BuildDatabase
  displayName: 'Build and deploy artifact for database'
  pool :
    vmImage: 'windows-latest'

  steps:
  - task: VSBuild@1
    inputs:
      solution: '$(Build.SourcesDirectory)/futurenhs.content.data/FutureNHS.Content.Data.sln'
    displayName: 'Build dacpac solution'
  - task: SqlAzureDacpacDeployment@1
    inputs:
      azureSubscription: '$(AZURESUBSCRIPTIONNAME)'
      AuthenticationType: 'server'
      ServerName: '$(DBSERVERNAME)'
      DatabaseName: '$(DATABASE)'
      SqlUsername: '$(DATABASEUSERNAME)'
      SqlPassword: '$(DATABASEPASSWORD)'
      deployType: 'DacpacTask'
      DeploymentAction: 'DeployReport'
      DacpacFile: '$(Build.SourcesDirectory)/futurenhs.content.data/FutureNHS.Content.Data/bin/Debug/FutureNHS.Content.Data.Dacpac'
      PublishProfile: '$(Build.SourcesDirectory)/futurenhs.content.data/FutureNHS.Content.Data/FutureNHS.Content.Data.publish.xml'
      IpDetectionMethod: 'AutoDetect'

  - task: CopyFiles@2
    displayName: 'Stage build report'
    inputs:
      SourceFolder: '$(Build.SourcesDirectory)/GeneratedOutputFiles'
      Contents: '**'
      TargetFolder: '$(Build.ArtifactStagingDirectory)'
  - task: SqlAzureDacpacDeployment@1
    inputs:
      azureSubscription: '$(AZURESUBSCRIPTIONNAME)'
      AuthenticationType: 'server'
      ServerName: '$(DBSERVERNAME)'
      DatabaseName: '$(DATABASE)'
      SqlUsername: '$(DATABASEUSERNAME)'
      SqlPassword: '$(DATABASEPASSWORD)'
      deployType: 'DacpacTask'
      DeploymentAction: 'Script'
      DacpacFile: '$(Build.SourcesDirectory)/futurenhs.content.data/FutureNHS.Content.Data/bin/Debug/FutureNHS.Content.Data.Dacpac'
      PublishProfile: '$(Build.SourcesDirectory)/futurenhs.content.data/FutureNHS.Content.Data/FutureNHS.Content.Data.publish.xml'
      IpDetectionMethod: 'AutoDetect'

  - task: CopyFiles@2
    displayName: 'Stage build script'
    inputs:
      SourceFolder: '$(Build.SourcesDirectory)/GeneratedOutputFiles'
      Contents: '**'
      TargetFolder: '$(Build.ArtifactStagingDirectory)'
  - task: CopyFiles@2
    displayName: 'Stage build artifacts'
    inputs:
      SourceFolder: '$(Build.SourcesDirectory)/futurenhs.content.data/FutureNHS.Content.Data/bin/Debug'
      Contents: '**'
      TargetFolder: '$(Build.ArtifactStagingDirectory)'

  - task: PublishPipelineArtifact@1
    displayName: 'Publish artifact'
    inputs:
     targetPath: '$(Build.ArtifactStagingDirectory)'
     artifact: 'FNHS-Content-Database'
     publishLocation: 'pipeline'