azure-devops - 有没有办法在 powershell 中创建数组并在 Azure DevOps YAML 中循环遍历它?

标签 azure-devops yaml azure-pipelines azure-pipelines-release-pipeline azure-pipelines-yaml

我正在尝试针对 10 个实例上的 140 多个数据库为我们的数据库部署创建管道。我们的 IT 团队提到,在某些情况下,数据库会从一个实例移动到另一个实例以进行负载平衡。所以我不能相信每个版本的数据库都会保留在同一个实例上。我们还可以在没有我们的团队听到任何消息的情况下加入新客户。这意味着我必须编写一个只能动态部署到给定实例上的数据库的管道。每个实例都有一个单独的代理。
我现在计划的工作流程。

  • 从我们的主分支构建 DacPac。
  • 添加与需要我们团队中的一名人员批准发布的环境相关联的阶段。
  • 运行 powershell 脚本以获取实例上的所有数据库并作为列表返回。
  • 循环遍历给定的列表并为每个数据库创建一个任务。

  • 现在,我在将数据库列表作为变量从 powershell 传递回 YAML 管道的部分遇到了问题。理想情况下,我可以返回一个列表,然后使用 YAML 循环功能使用给定的数据库名称创建 SQL Server 部署任务。是否可以从 Powershell 返回一个列表并使用循环功能遍历它们?我在下面使用的 YAML 示例减去我不想在那里共享的内容。
    
    /*Pipeline Being Called*/
    variables:
    - name: BuildConfiguration
      value: release
    - name: BuildPlatform
      value: any cpu
    - name: system.debug
      value: true
    - name: datamode 
      value: 0 
    - name: APIMode
      value: 0
    - name: Instance 
      value: "[ServerInstance]"
    - name: var01
    trigger: 
     - FixingDRDeployments
    
    stages: 
    - stage: Powershell 
      pool: [Agent Pool Name]
      jobs:
      - job: Powershell
        steps:
           - task: PowerShell@2
             inputs:
               targetType: 'inline'
               script: |
                 $ServerInstance = "$(Instance)"
                 
                 
                 $Databases = Get-DbaDatabase -SqlInstance $ServerInstance 
                 $ReturnValues = @()
                 
                 $query = "
                    Query to validate database is one we want to deploy against. 
                 "
                 
                 
                 foreach ( $db in $Databases) {
                   $Results = Invoke-DbaQuery -SqlInstance $serverInstance -Database $db.name -Query $query
                   if ($Results.Valid -eq 1) {
                     $ReturnValues += $db.Name
                   }
                 }
                 Write-Host $ReturnValues
                 
    
    
    
    #stages:
    - template: deploytemplates/BuildTemplate.yml
    - template: template.yml
      parameters:
        param: ["TestDatabase1","TestDatabase2"]
    **^ Instead of having a defined set of static databases, I'd like to be able to pass an array of databases from the powershell script above.**
    
    /*deploytemplates/BUildTemplate.yml*/
    
    stages:
    - stage: Build 
      jobs:
        - job: build 
          pool:
           name: mn-vs2017
           demands:
            - msbuild
            - visualstudio
          steps:
           - task: VSBuild@1
             displayName: 'Build solution **\*.sln'
             inputs:
               solution: '**\*.sln'
               vsVersion: "15.0"
               msbuildArgs: '/p:OutDir=$(build.artifactstagingdirectory) /p:CmdLineInMemoryStorage=True'
               platform: '$(BuildPlatform)'
               configuration: '$(BuildConfiguration)'
               clean: true
               maximumCpuCount: true
               msbuildArchitecture: x64
           - task: PublishBuildArtifacts@1
             displayName: 'Publish Artifact: MasterDacPac'
             inputs:
              PathtoPublish: '$(build.artifactstagingdirectory)'
              ArtifactName: DacPac
    
    /*template.yml*/
    parameters: 
      param : []
    
    
    stages:
    - stage: ApprovalGate1
      displayName:  ApprovalGate1
      dependsOn: Build
      jobs: 
      - deployment: 
        displayName: ApprovalGate1
        pool: [Pool Name Here]
        environment: Databases
        strategy:
          runOnce:
            deploy: 
             steps:
              - task: DownloadSecureFile@1
                name: PublishProfile
                displayName: 'Download secure file'
                inputs:
                  secureFile: Production.publish.xml
              - ${{ each p in parameters.param }}:
                 - task: SqlDacpacDeploymentOnMachineGroup@0
                   displayName: ${{ p }}
                   inputs:
                     DacpacFile: '$(Pipeline.Workspace)\DacPac\DacPacName.dacpac'
                     ServerName: $(Instance)
                     DatabaseName: ${{ p }}
                     PublishProfile: '$(PublishProfile.secureFilePath)'
                   continueOnError: true
                   condition: succeededOrFailed()
    

    最佳答案

    不,这是不可能的。如果您在 powershell 中创建一个变量,您将创建字符串类型的变量。 It is even worse :

    Yaml variables have always been string: string mappings.

    We are preparing to release a feature in the near future to allow you to pass more complex structures. Stay tuned!


    您可以使用数组作为参数,但这只是静态的,您已经知道了:
    steps:
    - template: template.yaml
      parameters:
        instances: 
        - test1
        - test2
        server: someServer
    

    关于azure-devops - 有没有办法在 powershell 中创建数组并在 Azure DevOps YAML 中循环遍历它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63623006/

    相关文章:

    php - Symfony2 : Database schema and entities not updating with changed metadata

    azure - 拉取请求标题更新时触发 Azure 管道

    java - 通过yaml配置java类对象列表

    Azure Devops API 用于获取包含文件夹的存储库中的所有文件

    node.js - Azure 管道中的数据库迁移

    azure-devops - 如何获取 Visual Studio Team Services 中工件的下载 url?

    javascript - 如何查看 serverless.yml 中解析的变量?

    docker - 在Docker容器中运行VSTS构建作业

    azure - 等待新部署的应用服务在 Azure Devops 发布管道中重新启动

    Azure DevOps 构建 yaml : "No repository found by name templates" even though it is displayed