Generate SQL Server schema change script on Azure DevOps pipeline
我正在尝试允许管道将架构更改发布到本地 SQL Server 2017 实例,但我想分两步完成:
- 生成架构更改脚本操作
- 审核通过后发布
我知道可以通过设置
发布到 SQL Azure 来实现
有没有办法以类似的方式发布到本地 SQL Server?我已经尝试过 SqlDacpacDeploymentOnMachineGroup 任务,但似乎无法通过此任务分两步完成
我终于设法通过数据库更改实现 SQL 模式生成,然后发布这些更改(在批准后)。一些备注:
- 如果更改会导致数据丢失,这将不起作用。
-
sqlpackage 的路径仅在安装 Visual Studio 2019 时正确,例如在windows-2019 图像中。 -
以前的
package.dacpac 之前是通过构建.sqlproj 项目生成的。 -
我通过组变量传递了以下变量(有关如何在此处创建组变量的更多信息):
-
targetDBConnectionString -
servername -
databasename -
adminlogin -
adminPassword
-
-
我已向
ApplyChanges 阶段添加了批准
(在 Pipelines 菜单中,选择环境,然后选择ApplyChanges
环境,然后是approvals and checks 从三个点
按钮,在右上角)。这样的变化不是
在手动批准之前应用到数据库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | stage: VerifyScript displayName: 'Script database schema changes' dependsOn: - Build jobs: - deployment: VerifyScript pool: vmImage: 'windows-2019' variables: - group: 'Timeline CV - Release' environment: 'scriptverification' strategy: runOnce: deploy: steps: - download: current artifact: dropDacpac patterns: '**/*' - task: CmdLine@2 displayName: 'Generate schema changes script' inputs: script: | "c:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\Common7\\IDE\\Extensions\\Microsoft\\SQLDB\\DAC\\140\\sqlpackage.exe" ^ /action:script ^ /diagnostics:true ^ /sourcefile:$(Pipeline.Workspace)\\dropDacpac\\path\\to\\the\\dacpacFile\\package.dacpac ^ /targetConnectionString:$(targetDBConnectionString) ^ /outputpath:$(Build.StagingDirectory)\\changesScript.sql - task: PublishPipelineArtifact@1 inputs: targetPath: '$(Build.StagingDirectory)' artifactName: dropSqlSchemaChangesScript condition: succeededOrFailed() - task: PowerShell@2 displayName: Show Auto Generated SQL Script inputs: targetType: 'inline' script: | Write-Host"Auto Generated SQL Update Script:" Get-Content $(Build.StagingDirectory)\\changesScript.sql | foreach {Write-Output $_} - stage: ApplyChanges displayName: 'Apply database schema changes' dependsOn: VerifyScript jobs: - deployment: ApplyChanges pool: vmImage: 'windows-2019' variables: - group: 'Timeline CV - Release' environment: 'applyChanges' strategy: runOnce: deploy: steps: - download: current artifact: dropSqlSchemaChangesScript - task: SqlDacpacDeploymentOnMachineGroup@0 displayName: 'Deploy SQL schema changes script' inputs: taskType: 'sqlQuery' sqlFile: '$(Pipeline.Workspace)\\dropSqlSchemaChangesScript\\changesScript.sql' targetMethod: 'server' authScheme: 'sqlServerAuthentication' serverName: '$(servername)' databaseName: '$(databasename)' sqlUsername: '$(adminlogin)' sqlPassword: '$(adminPassword)' |
我认为没有内置任务可以为您执行此操作。但是,SQLPackage.exe 有这个选项。看看这篇文章:http://diegogiacomelli.com.br/azure-pipelines-generating-db-script/。它描述了使用命令行任务生成 sql 脚本。
一旦你有了脚本,你可以使用 SqlDacpacDeploymentOnMachineGroup 来发布脚本(虽然它不会重复使用已经生成的脚本),或者你可以编写一个 powershell 脚本来发布 sql 脚本到数据库。可以在此处找到此类脚本的示例:https://careers.centric.eu/nl/blog/custom-azure-devops-pipeline-task-execute-sql-script/