使用 Azure DevOps 部署 SQL 脚本的 Yaml

问题描述 投票:0回答:1

在我们的 git 存储库中,我们有一个名为ExternalSQLScripts 的文件夹,其中包含表、视图、函数、StoredProcedures 等子文件夹。循环遍历每个子文件夹并执行外部 SQL Server 上的所有 .sql 文件。我们只能部署 SQL Server 数据库对象脚本,无法进行 .dacpac 数据库部署。

当我尝试运行管道时,我遇到了两个问题:

1) (Get-Module was unexpected at this time. Cmd.exe exited with code '255'.
2) PublishBuildArtifacts task throws error and I had to exclude it from the pipeline.

一般来说,部署 SQL Server 数据库脚本的最佳方法是什么。

YAML:

variables:
  sqlServerConnection: $(System.ConnectionStrings.DatabaseConnectionString)
  sqlScriptPath: $(Build.SourcesDirectory)/ExternalSQLScripts

steps:
- script: |
    # Install SqlServer module
    if (-!Test-Path (Get-Module -ListAvailable SqlServer)) {
      Install-Module SqlServer -Scope CurrentUser -Force
    }

    Get-ChildItem -Path $sqlScriptPath -Filter "*.sql" -Recurse | ForEach-Object {
    $scriptPath = $_.FullName
    $scriptName = $_.BaseName

    try {
        Invoke-Sqlcmd -ServerInstance $sqlServerConnection -Database [System.DefaultWorkingDirectory] -InputFile $scriptPath
        Write-Host "Successfully executed script: $scriptName"
    } catch {
        Write-Error "Error executing script: $scriptName - $($_.Exception.Message)"
    }
}
- task: PublishBuildArtifacts@1
    inputs:
      pathToPublish: $(sqlScriptPath)
      artifactName: sql-scripts
azure-devops yaml
1个回答
0
投票

此步骤运行

cmd.exe
而不是 PowerShell:

steps:
- script:

检查此链接:steps.脚本定义

脚本步骤在 Windows 上使用 cmd.exe 运行脚本,在 Bash 上运行脚本 其他平台。

将其更改为PowerShell@2

© www.soinside.com 2019 - 2024. All rights reserved.