全景图:我们正在使用Azure DevOps发布过程(到目前为止,我们在设计器中创建步骤,而不是在YAML管道中创建步骤)。我们发布到具有3个不同数据库的3个环境。该发行版的一部分是从DACPAC文件部署数据库。由于使用SqlPackage.exe直接发布到数据库不是很透明(您不会看到并查看实际的SQL脚本),因此我们希望分两个阶段进行发布:
问题:如何在阶段之间共享sql脚本文件,以及如何查看该脚本以进行批准。可以在不同的代理上触发阶段。
我尝试过的事情:
还有其他建议吗?
答案 0 :(得分:1)
could use Universal Packages in Package Management to publish arbitrary files for later retrieval是无法使用管道人工制品的地方。仅将文件内容转储到管道日志中是允许人们检查文件的最简单方法。
您还可以创建一个带有占位符的文件作为构建人工制品,并在每个阶段中通过Pipeline变量合并到最终设置中,这样就可以将其保留为构建人工制品。这就是我倾向于处理这种性质的文件的方式。听起来这不适用于您生成的SQL文件。
或者,如果“寻求批准”部分很重要,则可以生成该部分并将其写入日志,然后将其上传到Universal Package Management。然后在阶段结束时请求批准。在下一步中,您然后从Universal Package Management下载脚本,或者在执行之前使用完全相同的任务配置重新生成脚本。
答案 1 :(得分:0)
Azure DevOps现在允许下载在管道的早期阶段作为工件发布的文件。
在我共享的代码中,我实现了SQL模式生成和数据库更改,然后发布了这些更改(在批准之后)。一些说明:
sqlpackage
的路径仅在安装Visual Studio 2019时才正确,例如windows-2019
图像中。 targetDBConnectionString
servername
databasename
adminlogin
adminPassword
ApplyChanges
阶段添加了approval(在“管道”菜单中,选择环境,然后选择ApplyChanges
环境,然后从三个点按钮中选择approvals and checks
,在右上角)。这样,在进行手动批准之前,更改不会应用于数据库。 stages:
- stage: 'Build'
displayName: 'Build the web application'
jobs:
(...)
- job: BuildDacpac
pool:
vmImage: 'windows-2019'
steps:
- task: DotNetCoreCLI@2
displayName: 'Build the database project'
inputs:
command: 'custom'
custom: 'build'
projects: 'FQDN\For\The\DB\Project\DBProjectName.sqlproj'
- task: CopyFiles@2
displayName: 'Copy dacpac file to a staging directory'
inputs:
contents: |
FQDN\For\The\DB\Project\bin\**\*.dacpac
targetFolder: '$(Build.StagingDirectory)'
- task: PublishBuildArtifacts@1
displayName: 'Publish build artifact'
inputs:
pathToPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: dropDacpac
condition: succeededOrFailed()
- 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)'