azure - 将 terraform 输出作为 Azure DevOps 管道 YAML 中的变量

标签 azure azure-devops yaml azure-pipelines terraform-provider-azure

我正在尝试从 Terraform 输出中获取变量并显示/输出它。稍后我可以在另一个步骤中传递这个变量。 Terraform 输出我试图循环并获取变量的 json 但是,当我想在 Azure DevOps 的控制台中查看输出时,我只能看到脚本已保存,而看不到写出的任何值,因此检查变量是否已正确分配。

我想输出 reg_name 的值,但到目前为止,即使成功执行这部分后,我也没有看到任何运行后的结果。 关于这里可能出现问题的任何提示吗?

提前谢谢您!

enter image description here

当前代码:

       - task: TerraformTaskV3@3
          name: terraformOutput
          inputs:
            provider: 'azurerm'
            command: 'output'
            workingDirectory: '$(System.DefaultWorkingDirectory)/.azureDevOps/terraform'
            environmentServiceNameAzureRM: 'xxxxx'

        - task: AzurePowerShell@5
          inputs:
            azureSubscription: 'service-connection-creditrisk'
            ScriptType: 'InlineScript'
            Inline: |
              $json = Get-Content $(TerraformOutput.jsonOutputVariablesPath) | Out-String | ConvertFrom-Json
              foreach($prop in $json.psobject.properties) {
                  Write-Host("##vso[task.setvariable variable=$$prop.reg_name;]$($prop.reg_name.value)")
                }
            workingDirectory: '$(System.DefaultWorkingDirectory)/.azureDevOps/terraform'
            azurePowerShellVersion: 'LatestVersion'

最佳答案

如果您遵循本文档: https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform#terraform-output-to-pipeline-variables

您可以添加以下步骤:

- task: TerraformTaskV4@4
  name: terraformOutput
  inputs:
    provider: 'azurerm'
    command: 'output'
    workingDirectory: '$(System.DefaultWorkingDirectory)/.azureDevOps/terraform'
    environmentServiceNameAzureRM: 'xxxxx'

这会将所有输出设置为 $(TF_OUT_<OUPUT NAME>)
例如如果输出被称为 reg_name那么它将是 $(TF_OUT_REG_NAME)

关于azure - 将 terraform 输出作为 Azure DevOps 管道 YAML 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75416196/

相关文章:

Azure Functions V4 NET 6 服务总线触发器未触发

azure - 如何验证 Azure Active Directory 中刷新的 id_token 的签名

azure - 无法从变量组(库)更新变量

azure - 如何在 Azure 经典发布管道中使用 Powershellscript - 存储在 Azure Devops 安全文件中的脚本文件

c# - 如何在微服务架构中进行集成测试

azure-devops - 在Azure DevOps管道中安全签署ClickOnce应用程序

azure-devops - 遍历 Azure Pipelines 变量组中的变量

amazon-web-services - 创建Stack操作: Template format error: Unresolved resource dependencies [VpcId] in the Resources block of the template

yaml - 使用一组值重复 GitHub 操作工作流程中的一个步骤

azure - 如何将不同订阅的 ACR 附加到 AKS?