背景
Visual Studio 中的2012 SSIS Project Deployment model 包含一个用于项目参数、项目级连接管理器、包以及您添加到项目中的任何其他内容的文件。
在下图中,您可以看到我有一个名为 Lifecycle 的解决方案。该解决方案有一个名为 Lifecycle 的项目。 Lifecycle 项目定义了一个项目级连接管理器ERIADOR 和两个 SSIS 包:Package00.dtsx 和 Package01.dtsx。
当您运行一个包时,Visual Studio 会在幕后首先将所有必需的项目元素构建/编译到一个称为 ispac(发音为 eye-ess-pack,而不是 ice-pack)的可部署量子中。这将在您项目的 bin\Development 子文件夹中找到。
Lifecycle.ispac 是一个包含以下内容的 zip 文件。
这一切是什么意思?最大的区别在于,您需要部署整个 .ispac,而不是仅仅部署更新的包。是的,即使您只更改了一个包,您也确实必须重新部署所有内容。这就是生活。
如何使用 SSIS 项目部署模型部署包?
您可以使用主机选项,但您需要了解以下 3 件事
- 我的 ispac 在哪里
- 我要部署到什么服务器
- 这个项目放在什么文件夹里
SSDT
这可能是您一开始最常用的选项。在 SQL Server Data Tools、SSDT 中,您可以在配置管理器级别定义部署什么服务器和文件夹。在我的客户,我有 3 种配置:Dev、Stage、Production。定义这些值后,它们会保存到 .dtproj 文件中,然后您可以右键单击并从 Visual Studio 部署到您心仪的内容。
ISDeploymentWizard - GUI 风格
SSDT 实际上只是构建对 ISDeploymentWizard.exe 的调用,出于某种原因,它有 32 位和 64 位版本。
- C:\Program Files\Microsoft SQL Server\110\DTS\Binn\ISDeploymentWizard.exe
- C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\ISDeploymentWizard.exe
.ispac 扩展名与 ISDeploymentWizard 相关联,因此双击即可离开。第一个屏幕与使用 SSDT 界面相比是新的,但之后将是相同的一组点击来部署。
ISDeploymentWizard - 命令行风格
他们在 2012 年发布的包部署模型中做得对的是,清单文件可以以自动化方式部署。我有一个workaround,但它应该是一个标准的“东西”。
因此,请仔细查看 SSDT 或 GUI 部署中的 Review 选项卡。这不是美女吗?
使用相同的可执行文件 ISDeploymentWizard,我们可以为我们的 .ispac(s) 提供有人值守和无人值守的安装程序。突出显示那里的第二行,复制粘贴,现在您可以进行持续集成!
C:\Program Files\Microsoft SQL Server\110\DTS\Binn\ISDeploymentWizard.exe
/Silent
/SourcePath:"C:\Dropbox\presentations\SSISDB Lifecycle\Lifecycle\Lifecycle\bin\Development\Lifecycle.ispac"
/DestinationServer:"localhost\dev2012"
/DestinationPath:"/SSISDB/Folder/Lifecycle"
TSQL
您可以通过 SQL Server Management Studio、SSMS 或命令行 sqlcmd.exe 将 ispac 部署到 SQL Server。虽然SQLCMD 不是严格要求,但它简化了脚本。
您必须使用 Windows 帐户来执行此操作,否则您将收到以下错误消息。
使用 SQL Server 身份验证的帐户无法启动该操作。使用使用 Windows 身份验证的帐户启动操作。
此外,您需要能够执行批量操作(以序列化 .ispac)和对 SSISDB 数据库的 ssis_admin/sa 权限。
这里我们使用带有 BULK 选项的 OPENROWSET 将 ispac 读入 varbinary 变量。如果文件夹不存在,我们通过catalog.create_folder 创建一个文件夹,然后使用catalog.deploy_project 实际部署项目。完成后,我想检查操作消息表以验证事情是否按预期进行。
USE SSISDB
GO
-- You must be in SQLCMD mode
-- setvar isPacPath "C:\Dropbox\presentations\SSISDB Lifecycle\Lifecycle\Lifecycle\bin\Development\Lifecycle.ispac"
:setvar isPacPath "<isPacFilePath, nvarchar(4000), C:\Dropbox\presentations\SSISDB Lifecycle\Lifecycle\Lifecycle\bin\Development\Lifecycle.ispac>"
DECLARE
@folder_name nvarchar(128) = 'TSQLDeploy'
, @folder_id bigint = NULL
, @project_name nvarchar(128) = 'TSQLDeploy'
, @project_stream varbinary(max)
, @operation_id bigint = NULL;
-- Read the zip (ispac) data in from the source file
SELECT
@project_stream = T.stream
FROM
(
SELECT
*
FROM
OPENROWSET(BULK N'$(isPacPath)', SINGLE_BLOB ) AS B
) AS T (stream);
-- Test for catalog existences
IF NOT EXISTS
(
SELECT
CF.name
FROM
catalog.folders AS CF
WHERE
CF.name = @folder_name
)
BEGIN
-- Create the folder for our project
EXECUTE [catalog].[create_folder]
@folder_name
, @folder_id OUTPUT;
END
-- Actually deploy the project
EXECUTE [catalog].[deploy_project]
@folder_name
, @project_name
, @project_stream
, @operation_id OUTPUT;
-- Check to see if something went awry
SELECT
OM.*
FROM
catalog.operation_messages AS OM
WHERE
OM.operation_message_id = @operation_id;
你的妈妈
正如你的Managed Object Model 提供了一个用于部署包的.NET 接口。这是一种用于部署 ispac 和创建文件夹的 PowerShell 方法,因为 ISDeploymentWizard 不支持该选项。
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null
#this allows the debug messages to be shown
$DebugPreference = "Continue"
# Retrieves a 2012 Integration Services CatalogFolder object
# Creates one if not found
Function Get-CatalogFolder
{
param
(
[string] $folderName
, [string] $folderDescription
, [string] $serverName = "localhost\dev2012"
)
$connectionString = [String]::Format("Data Source={0};Initial Catalog=msdb;Integrated Security=SSPI;", $serverName)
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$integrationServices = New-Object Microsoft.SqlServer.Management.IntegrationServices.IntegrationServices($connection)
# The one, the only SSISDB catalog
$catalog = $integrationServices.Catalogs["SSISDB"]
$catalogFolder = $catalog.Folders[$folderName]
if (-not $catalogFolder)
{
Write-Debug([System.string]::Format("Creating folder {0}", $folderName))
$catalogFolder = New-Object Microsoft.SqlServer.Management.IntegrationServices.CatalogFolder($catalog, $folderName, $folderDescription)
$catalogFolder.Create()
}
return $catalogFolder
}
# Deploy an ispac file into the SSISDB catalog
Function Deploy-Project
{
param
(
[string] $projectPath
, [string] $projectName
, $catalogFolder
)
# test to ensure file exists
if (-not $projectPath -or -not (Test-Path $projectPath))
{
Write-Debug("File not found $projectPath")
return
}
Write-Debug($catalogFolder.Name)
Write-Debug("Deploying $projectPath")
# read the data into a byte array
[byte[]] $projectStream = [System.IO.File]::ReadAllBytes($projectPath)
# $ProjectName MUST match the value in the .ispac file
# else you will see
# Failed to deploy the project. Fix the problems and try again later.:The specified project name, test, does not match the project name in the deployment file.
$projectName = "Lifecycle"
$project = $catalogFolder.DeployProject($projectName, $projectStream)
}
$isPac = "C:\Dropbox\presentations\SSISDB Lifecycle\Lifecycle\Lifecycle\bin\Development\Lifecycle.ispac"
$folderName = "Folder"
$folderName = "SSIS2012"
$folderDescription = "I am a description"
$serverName = "localhost\dev2012"
$catalogFolder = Get-CatalogFolder $folderName $folderDescription $serverName
Deploy-Project $isPac $projectName $catalogFolder