从 Terraform 外部数据中的 powershell 脚本 运行 输出 Build.Repository.Name

Output of Build.Repository.Name from a powershell script running in data external of Terraform

我需要从 Terraform 脚本中 Terraform 外部数据源中 运行 的 powershell 脚本中获取 Azure DevOps 存储库名称(Build.Repository.Name 的输出)。但是我得到空白输出。以下是要参考的脚本。我是否在 powershell 中正确调用了系统构建变量?请在此处查看并提供帮助。

地形 main.tf

    data "external" "repo" {
  program = ["pwsh", "${abspath(path.module)}/fetch_repo_name.ps1"]
  query = {
    always = timestamp()
  }
}

Powershell 脚本

    [CmdletBinding()]
$jsonpayload = [Console]::In.ReadLine()
$json = ConvertFrom-Json $jsonpayload

$always = $json.always

$outputJson = @{RepoName = $($env:BUILD_Repository_Name);} | ConvertTo-Json

    Write-Output $outputJson

输出

Plan: 0 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + repo-name = [
      + (known after apply),
    ]
data.external.repo: Reading...
data.external.repo: Read complete after 1s [id=-]

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

repo-name = [
  tomap({
    "RepoName" = ""
  }),
]

我修改了代码。我在 PowerShell 中使用错误的方式引用 Azure DevOps 系统变量。必须全部大写,点必须用“_”代替。也使用 $env.

更正了 PowerShell 脚本

$outputJson = @{RepoName = $($env:BUILD_REPOSITORY_NAME);} | ConvertTo-Json

Write-Output $outputJson

输出

Outputs:
    
    repo-name = [
      tomap({
        "RepoName" = "Vnetest_Repo"
      }),
    ]