章鱼部署。ps1 DACPAC 部署失败

Octopus Deploy.ps1 DACPAC Deployment Fails

我在 TeamCity 上使用 Octopus 将数据库部署到我们的生产环境,但它一直出错。

这是部署。ps1代码:

$dbName = $OctopusParameters['DBName']
$dbUser = $OctopusParameters['AdminDBUsername']
$dbPassword = $OctopusParameters['AdminDBPassword']
$dbSource = $OctopusParameters['DBDataSource']

# Set params
if (! $dbName)
{
    Write-Host "Missing required variable dbName" -ForegroundColor Yellow
    exit 1
}
if (! $dbUser)
{
    Write-Host "Missing required variable dbUser" -ForegroundColor Yellow
    exit 1
}
if (! $dbPassword)
{
    Write-Host "Missing required variable dbPassword" -ForegroundColor Yellow
    exit 1
}
if (! $dbSource)
{
    Write-Host "Missing required variable dbSource" -ForegroundColor Yellow
    exit 1
}

# Add the DLL
# For 64-bit machines
Add-Type -path ((Get-Item -Path ".\" -Verbose).FullName + "\bin\Microsoft.SqlServer.TransactSql.ScriptDom.dll")
Add-Type -path ((Get-Item -Path ".\" -Verbose).FullName + "\bin\Microsoft.SqlServer.Dac.dll")

# Create the connection string
$d = New-Object Microsoft.SqlServer.Dac.DacServices ("data source=" + $dbSource + ";User Id = " + $dbUser + ";pwd=" + $dbPassword)

#Load the dacpac
$dacpac = ((Get-Item -Path ".\" -Verbose).FullName + "\DeployScripts\Database.dacpac")
$dacpacoptions = ((Get-Item -Path ".\" -Verbose).FullName + "\DeployScripts\publish.xml")

#Load dacpac from file & deploy to database
$dp = [Microsoft.SqlServer.Dac.DacPackage]::Load($dacpac)

#Read a publish profile XML to get the deployment options
$dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($dacpacoptions)

# Deploy the dacpac
$d.Deploy($dp, $dbName, $TRUE, $dacProfile.DeployOptions)

以下是发布配置文件,以备不时之需:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <IncludeCompositeObjects>True</IncludeCompositeObjects>
    <TargetDatabaseName>MyDatabase</TargetDatabaseName>
    <DeployScriptFileName>MyDatabase.sql</DeployScriptFileName>
    <ProfileVersionNumber>1</ProfileVersionNumber>
    <BlockWhenDriftDetected>True</BlockWhenDriftDetected>
    <RegisterDataTierApplication>True</RegisterDataTierApplication>
  </PropertyGroup>
</Project>

以下是我遇到的错误:

使用“4”个参数调用 "Deploy" 异常:

"An error occurred during deployment plan generation. Deployment cannot continue."

部署时。ps1:60 char:2

$d.Deploy($dp, $dbName, $TRUE, $dacProfile.DeployOptions)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~

类别信息:未指定:(:)[],ParentContainsErrorRecordException

FullyQualifiedErrorId : DacServicesException

有没有人知道是什么原因造成的?以及如何解决?提前致谢!

好的,伙计们,我明白了。这是我所做的:

在 ps1 文件中添加一个 try catch 块,其中 $d.Deploy 的调用方式如下:

 try
 {
     # Deploy the dacpac
     $d.Deploy($dp, $dbName, $TRUE, $dacProfile.DeployOptions)
 }
 catch
 {
     Write-Host "LoadException";
     $Error | format-list -force
     Write-Host $Error[0].Exception.ParentContainsErrorRecordException;
 }

这样做告诉我 sqlproj 文件是针对 Sql Server 2014,但部署到的数据库是 Sql Server 2012。只需进入属性sql 项目文件并将其更改为针对 Sql Server 2012,现在可以使用了!万岁!