Skip to content

Commit

Permalink
Merge pull request #213 from mlowijs/master
Browse files Browse the repository at this point in the history
Deploy DACPAC step template can now load DAC dll from DACfx, no need for VS on your Tentacle
  • Loading branch information
hnrkndrssn committed Sep 6, 2015
2 parents 16f25da + 32ccc7c commit 98796eb
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions step-templates/sql-deploy-dacpac.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"Id": "ActionTemplates-12",
"Name": "SQL - Deploy DACPAC",
"Description": "Deploys a DACPAC to a target database. Requires Visual Studio and the SSDT extension to be installed.",
"Description": "Deploys a DACPAC to a target database. Requires DACfx (from the Web Platform Installer) or Visual Studio and the SSDT extension to be installed.",
"ActionType": "Octopus.Script",
"Version": 8,
"Version": 14,
"Properties": {
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = 'Stop'\n\n$regKeyFormat = 'HKLM:\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\{0}.0'\n$ssdtSubfolder = 'Extensions\\Microsoft\\SQLDB\\DAC\\120'\n$vsVersions = @( '14', '12', '11' )\n\n$progressChangedEvent = \"ProgressChanged\"\n\nfunction Validate-Argument($name, $value) {\n if (!$value) {\n throw ('Missing required value for parameter ''{0}''.' -f $name)\n }\n\n return $value\n}\n\nfunction Load-DacAssembly {\n Write-Verbose 'Attempting to discover Visual Studio install folder...' \n $regKey = $vsVersions | foreach { $regKeyFormat -f $_ } | where { Test-Path $_ } | select -first 1\n \n if (!$regKey) {\n throw 'No usable Visual Studio installation found.'\n }\n\n Write-Verbose 'Visual Studio found. Attempting to discover SSDT...'\n $vsInstallDir = Get-ItemProperty -Path $regKey | select -ExpandProperty InstallDir\n $ssdtPath = $vsInstallDir + $ssdtSubfolder\n\n if (!(Test-Path $ssdtPath)) {\n throw 'SSDT not found. Please install the latest SSDT.'\n }\n\n Write-Verbose 'Found SSDT, attempting to load DAC assembly...'\n Add-Type -Path ($ssdtPath + '\\Microsoft.SqlServer.Dac.dll')\n \n Write-Verbose 'Loaded DAC assembly.'\n}\n\n#\n# Script\n#\n\n# Get arguments\n$dacpacPackageStepName = Validate-Argument 'DACPAC Package Step Name' $OctopusParameters['DacpacPackageStepName']\n$dacpacName = Validate-Argument 'DACPAC Name' $OctopusParameters['DacpacName']\n$targetConnectionString = Validate-Argument 'Target Connection String' $OctopusParameters['TargetConnectionString']\n$targetDatabaseName = Validate-Argument 'Target Database Name' $OctopusParameters['TargetDatabaseName']\n\n$profileName = $OctopusParameters['ProfileName']\n\n# Load the DAC assembly\nLoad-DacAssembly\n\n# Set .NET CurrentDirectory to package installation path\n$installDirPathFormat = 'Octopus.Action[{0}].Output.Package.InstallationDirectoryPath' -f $dacpacPackageStepName\n$installDirPath = $OctopusParameters[$installDirPathFormat]\n\nWrite-Verbose ('Setting CurrentDirectory to ''{0}''' -f $installDirPath)\n[System.Environment]::CurrentDirectory = $installDirPath\n\n# Load DacPackage\n$dacPackage = [Microsoft.SqlServer.Dac.DacPackage]::Load($dacpacName + '.dacpac')\n\n# Setup DacProfile\nif ($profileName) {\n $dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($profileName)\n Write-Host ('Loaded publish profile ''{0}''.' -f $profileName)\n} else {\n $dacProfile = New-Object Microsoft.SqlServer.Dac.DacProfile\n}\n\n# Setup DacServices\n$dacServices = New-Object Microsoft.SqlServer.Dac.DacServices -ArgumentList $targetConnectionString\nRegister-ObjectEvent $dacServices $progressChangedEvent -SourceIdentifier $progressChangedEvent -Action { Write-Verbose ('DacServices: {0}' -f $EventArgs.Message) } | Out-Null\n\n# Deploy package\ntry {\n Write-Host 'Starting DACPAC deployment...'\n $dacServices.Deploy($dacPackage, $targetDatabaseName, $true, $dacProfile.DeployOptions, $null)\n Write-Host 'Deployment succeeded!'\n} catch [Microsoft.SqlServer.Dac.DacServicesException] {\n Unregister-Event -SourceIdentifier $progressChangedEvent\n throw ('Deployment failed: ''{0}'' Reason: ''{1}''' -f $_.Exception.Message, $_.Exception.InnerException.Message)\n}"
"Octopus.Action.Script.ScriptBody": "$regKeyFormat = 'HKLM:\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\{0}.0'\n$ssdtSubfolder = 'Extensions\\Microsoft\\SQLDB\\DAC\\120'\n$dacfxPath = 'C:\\Program Files\\Microsoft SQL Server\\120\\DAC\\bin'\n$vsVersions = @( '14', '12' )\n\n$progressChangedEvent = \"ProgressChanged\"\n\nfunction Validate-Argument($name, $value) {\n if (!$value) {\n throw ('Missing required value for parameter ''{0}''.' -f $name)\n }\n\n return $value\n}\n\nfunction Load-DacAssembly {\n Write-Verbose 'Testing if DACfx was installed...'\n \n if (Test-Path $dacfxPath) {\n Add-Type -Path ($dacfxPath + '\\Microsoft.SqlServer.Dac.dll')\n } else {\n Write-Verbose 'Attempting to discover Visual Studio install folder...' \n $regKey = $vsVersions | foreach { $regKeyFormat -f $_ } | where { Test-Path $_ } | select -First 1\n \n if (!$regKey) {\n throw 'No usable Visual Studio installation found.'\n }\n \n Write-Verbose 'Visual Studio found. Attempting to discover SSDT...'\n $vsInstallDir = Get-ItemProperty -Path $regKey | select -ExpandProperty InstallDir\n $ssdtPath = $vsInstallDir + $ssdtSubfolder\n \n if (!(Test-Path $ssdtPath)) {\n throw 'SSDT not found. Please install the latest SSDT.'\n }\n \n Write-Verbose 'Found SSDT, attempting to load DAC assembly...'\n Add-Type -Path ($ssdtPath + '\\Microsoft.SqlServer.Dac.dll')\n }\n \n Write-Verbose 'Loaded DAC assembly.'\n}\n\n#\n# Script\n#\n\n# Get arguments\n$dacpacPackageStepName = Validate-Argument 'DACPAC Package Step Name' $OctopusParameters['DacpacPackageStepName']\n$dacpacName = Validate-Argument 'DACPAC Name' $OctopusParameters['DacpacName']\n$targetConnectionString = Validate-Argument 'Target Connection String' $OctopusParameters['TargetConnectionString']\n$targetDatabaseName = Validate-Argument 'Target Database Name' $OctopusParameters['TargetDatabaseName']\n\n$profileName = $OctopusParameters['ProfileName']\n\n# Load the DAC assembly\nLoad-DacAssembly\n\n# Set .NET CurrentDirectory to package installation path\n$installDirPathKey = 'Octopus.Action[{0}].Output.Package.InstallationDirectoryPath' -f $dacpacPackageStepName\n$installDirPath = $OctopusParameters[$installDirPathKey]\n\n[System.Environment]::CurrentDirectory = $installDirPath\nWrite-Verbose ('Set Environment.CurrentDirectory to ''{0}''.' -f $installDirPath)\n\n# Load DacPackage\n$dacPackage = [Microsoft.SqlServer.Dac.DacPackage]::Load($dacpacName + '.dacpac')\n\n# Load DacProfile\nif ($profileName) {\n $dacProfile = [Microsoft.SqlServer.Dac.DacProfile]::Load($profileName)\n Write-Host ('Loaded publish profile ''{0}''.' -f $profileName)\n} else {\n $dacProfile = New-Object Microsoft.SqlServer.Dac.DacProfile\n}\n\n# Setup DacServices\n$dacServices = New-Object Microsoft.SqlServer.Dac.DacServices -ArgumentList $targetConnectionString\nRegister-ObjectEvent $dacServices $progressChangedEvent -SourceIdentifier $progressChangedEvent -Action { Write-Verbose ('DacServices: {0}' -f $EventArgs.Message) } | Out-Null\n\n# Deploy package\ntry {\n Write-Host 'Starting DACPAC deployment...'\n $dacServices.Deploy($dacPackage, $targetDatabaseName, $true, $dacProfile.DeployOptions, $null)\n Write-Host 'Deployment succeeded!'\n} catch [Microsoft.SqlServer.Dac.DacServicesException] {\n Unregister-Event -SourceIdentifier $progressChangedEvent\n throw ('Deployment failed: ''{0}'' Reason: ''{1}''' -f $_.Exception.Message, $_.Exception.InnerException.Message)\n}"
},
"SensitiveProperties": {},
"Parameters": [
Expand Down Expand Up @@ -50,8 +50,8 @@
],
"LastModifiedBy": "mlowijs",
"$Meta": {
"ExportedAt": "2015-08-13T07:43:59.474Z",
"OctopusVersion": "3.0.10.2278",
"ExportedAt": "2015-09-06T09:10:53.821Z",
"OctopusVersion": "3.0.21.0",
"Type": "ActionTemplate"
}
}

0 comments on commit 98796eb

Please sign in to comment.