Deploy Dacpac packages via power shell script to Azure SQL Server

Question:

I’m trying to deploy multiple dacpac’s during single build process by using PowerShell script.

On my local environment everything works great, but during build process on the Visual Studio team services I get an error:

2017-02-24T06:03:09.7955300Z *********.dacpac deployment has been failed
2017-02-24T06:03:09.9785258Z ##[error]Exception calling “Deploy” with “3” argument(s): “Could not deploy package.”
At D:\a\1\s********************\deploydatabase.ps1:104 char:13
+ $dacService.Deploy($dp, $database, $true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DacServicesException

2017-02-24T06:03:10.0085256Z ##[error]Process completed with exit code 1 and had 1 error(s) written to the error stream.

Answer:

First, you need to add firewall rule in order to connect to Azure SQL Server.

  1. Edit your build definition
  2. Select Option tab and check Allow Scripts to Access OAuth Token
  3. Add Azure PowerShell step (arguments: -RestAddress https://[account].vsdtl.visualstudio.com/DefaultCollection/_apis/vslabs/ipaddress -Token $(System.AccessToken) -RG [resource group] -Server [server name] -ruleName $(Build.BuildNumber)

Code:

Secondly, I recommend you use the assembly in this package:
Microsoft.SqlServer.Dac.

Thirdly, to get the detail error, you can use this code instead:

On the other hand, I recommend you can deploy SQL package through SqlPackage.exe.

Source:

Deploy Dacpac packages via power shell script to Azure SQL Server by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply