如何从 Azure DevOps YAML 管道访问 AzureResourceManagerTemplateDeployment (@3) 任务中的“deploymentOutputs”?

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

我正在开发 YAML 管道,它只是基于保存在我的 Azure DevOps 存储库中的 main.bicep 文件(目前仅通过它部署 azure ADF)来部署 HUB 资源。但是,我需要访问二头肌部署的“输出” - 例如,我需要一些已部署的 Azure 资源的 ID。 (部署也没有问题>>最后我确实部署了 ADF)

这是代码: 阶段:

#  DEPLOYING INFRASTRUCTURE - HUB
  - stage: DeployHub
    displayName: Deploy Hub Infrastructure
    jobs:
      - deployment: DeployHub
        displayName: Deploy Hub Infrastructure
        environment: '$(testEnvironment)' #used for the manual approval conditions
        strategy:
         runOnce:
          deploy:
            steps:
            # the checkout will enable the use of "git diff" as to see which files have been modified in the latest commit at some point
            - checkout: git://${{ variables.my_org }}/my_project@refs/heads/feature_branch_01

          # Actual deployment of the resources - HUB
            - task: AzureResourceManagerTemplateDeployment@3
              name: ARM_Template_Deployment_HUB
              displayName: 'Bicep Deployment Task HUB'
              inputs:
                deploymentScope: 'Resource Group'
                azureResourceManagerConnection: '$(serviceConnection)'
                action: 'Create Or Update Resource Group'
                resourceGroupName: '$(rgTesting)'
                location: 'some_location'
                templateLocation: 'Linked artifact'
                csmFile: '$(Build.SourcesDirectory)/$(bicepFilePathHUB_Test)'
                deploymentMode: Incremental
                deploymentOutputs: 'armOutputsHUB'

所以问题是:

  1. 如何访问armOutputsHUB?
  2. 如果我在同一个作业、阶段中访问它,有什么关系吗?
  3. 它保存在哪里? - 我读到所有部署类型的作业都保存在:“部署作业使用 $(Pipeline.Workspace) 系统变量”。 来源:https://learn.microsoft.com/en-us/azure/devops/pipelines/process/deployment-jobs?view=azure-devops 但是,我仍然找不到名为“armOutputsHUB”的变量

我尝试过但没有成功:

  1. 在与下一个任务相同的工作中:
            - task: PowerShell@2
              displayName: 'Parse ARM deploymentOutputs'
              env:
                ARM_OUTPUTS: $(armOutputsHUB)
              inputs:
                targetType: 'inline'
                errorActionPreference: continue
                workingDirectory: '$(Pipeline.Workspace)' #default is "$(Build.SourcesDirectory)"
                pwsh: true
                script: |
                  Write-Host "armOutputsHUB = $armOutputsHUB" # >>nothing inside
                  Write-Host "armOutputsHUB = $($armOutputsHUB)" # >> nothing inside
                  Write-Host "armOutputsHUB = $($ARM_Template_Deployment_HUB.armOutputsHUB)" # >> nothing inside

                  $outputsObject = $env:ARM_OUTPUTS | ConvertFrom-Json
                  # error: Conversion from JSON failed with error: Unexpected character encountered while parsing value: $. Path '', line 0, position 0.
                  Write-Host "outputsObject = $($outputsObject)"  # >>nothing inside
                  Write-Host "outputsObject = $outputsObject"  # >>nothing insi

任何供我测试的想法或解决方案/代码将不胜感激!!

azure azure-devops azure-pipelines azure-resource-manager azure-pipelines-yaml
2个回答
1
投票

我在 GitHub 上为您找到了一个很好的示例管道。 它与您的一些示例非常相似,但您可能缺少字符串引号的使用......🚀

https://github.com/microsoftgraph/group-membership-management/blob/d009104558b228b6e74e4a601145dde03affbe42/yaml/deploy-webapp.yml#L81


- task: PowerShell@2
    name: 'SetDeploymentOutputVariables'
    displayName: 'Set Deployment Output Variables'
    inputs:
      targetType: inline
      script: |
        $armOutputObj = '$(deploymentOutputs)' | ConvertFrom-Json
        $armOutputObj.PSObject.Properties | ForEach-Object {
          $keyname = $_.Name
          $value = $_.Value.value

          # Creates a standard pipeline variable
          Write-Output "##vso[task.setvariable variable=$keyName;issecret=true]$value"

          # Display keys in pipeline
          Write-Output "output variable: $keyName"
        }
      pwsh: true

0
投票

默认情况下,每个输出都由 JSON.stringify() 处理。 AzureResourceManagerTemplateDeployment@3 任务允许通过参数

useWithoutJSON: true
禁用此处理,在 UI 中称为“使用不应用 JSON.Stringify 的单独输出值”。

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