Azure DevOps 现在允许下载在管道的早期阶段已作为工件发布的文件。
在我分享的代码中,我通过数据库更改实现了 SQL 模式生成,然后发布这些更改(在批准后)。一些评论:
- 如果更改会导致数据丢失,这将不起作用。
- 仅当安装 Visual Studio 2019 时,路径
sqlpackage
才是正确的,例如在windows-2019
图像中。
- 我通过组变量传递了以下变量(有关如何在此处创建组变量的更多信息):
targetDBConnectionString
servername
databasename
adminlogin
adminPassword
- 我已经向舞台添加了批准(
ApplyChanges
在 Pipelines 菜单中,选择环境,然后是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)'