2016-03-04 80 views
2

我在TeamCity上使用Octopus將數據庫部署到我們的生產環境,但它一直給出錯誤。Octopus Deploy.ps1 DACPAC部署失敗

這裏是Deploy.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.ps1:60字符:2

$ d.Deploy($ DP,$ DBNAME,$ True時,$ dacProfile.DeployOptions)

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

CategoryInfo:NotSpecified:(:) [],ParentContainsErrorRecordException

FullyQualifiedErrorId:DacServicesException

有沒有人有任何想法可以導致這一點?以及如何解決它?提前致謝!

回答

5

好吧,我想通了。下面是我所做的:

在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服務器2014年,但部署的數據庫是Sql Server 2012.只需進入sql項目文件的屬性並進行更改,以便它針對Sql Server 2012,現在它可以工作!好哇!