gpt4 book ai didi

azure - 无法使用 Azure DevOps 管道中设置的输出变量

转载 作者:行者123 更新时间:2023-12-02 23:12:43 25 4
gpt4 key购买 nike

我有一个使用模板化作业的管道,如下所示:

  - template: '/pipeline/templates/jobs/DeployInfraToRegion.yml@devops'
parameters:
...

此作业将编译的 Bicep 模板部署到 Azure。该模板创建一个 KeyVault 并输出其名称,如下所示:

output sqlSecretsKeyVaultName string = keyvault.outputs.keyVaultName

上面的模板包含一个名为DeployInfrastruct的作业:

jobs:
- deployment: DeployInfrastructure
displayName: Deploy Infrastructure
variables:
...

它运行一个最终任务,该任务获取所有输出并将它们设置为管道变量:

            - task: PowerShell@2
name: SetArmOutputsForRegion
displayName: "Set Arm outputs as variables"
inputs:
targetType: inline
pwsh: true
errorActionPreference: stop
script: |
$armOutputString = '$(${{ parameters.armOutputVariable }})'

$armOutputObj = $armOutputString | convertfrom-json
$armOutputObj.PSObject.Properties | ForEach-Object {
$type = ($_.value.type).ToLower()
$key = $_.name
$value = $_.value.value

if ($type -eq "securestring") {
Write-Host "##vso[task.setvariable variable=$key;isOutput=true;issecret=true]$value"
} elseif ($type -eq "string") {
Write-Host "##vso[task.setvariable variable=$key;isOutput=true]$value"
} else {
Throw "Type '$type' not supported!"
}
}

请注意,此任务名为SetArmOutputsForRegion

运行时,我可以在管道中看到该管道变量已按预期设置:

##[debug]Processed: ##vso[task.setvariable variable=sqlSecretsKeyVaultName;isOutput=true]expected-keyvault-name

运行此模板化作业后,下一个作业需要使用此输出变量。我是这样配置的:

  - job: 'AddSecretsToKeyVault'
dependsOn: DeployInfrastructure
variables:
keyVaultName: $[ dependencies.DeployInfrastructure.outputs['SetArmOutputsForRegion.sqlSecretsKeyVaultName'] ]
...

请注意,此作业取决于设置变量的作业,这被引用为dependency.DeployInfrastruct。该字符串的格式为 [任务名称].[变量名称],按照 documentation 。但是,当我尝试访问此变量时,它始终为空:

    - task: AzurePowerShell@4
displayName: Ensure required secrets exist
inputs:
azureSubscription: MyConnection
failOnStandardError: true
azurePowerShellVersion: 'LatestVersion'
scriptType: 'inlineScript'
inline: |
Write-Host "Vault name is $(keyVaultName)"

我觉得此时我已经尝试了变量声明的所有可能的变体。我已经对所有名称进行了三次检查,我的所有代码都由其他人检查过。我只是不明白为什么这些变量对我来说不可用。在这里发帖,绝望地希望有人能解释我做错了什么。提前致谢。

更新1

我现在发现了documentation about deployment jobs ,这显然以不同的方式对待输出变量。就我而言,我有一个 runOnce 作业,因此我需要包含作业名称作为前缀。换句话说,改变...

    variables:
keyVaultName: $[ dependencies.DeployInfrastructure.outputs['SetArmOutputsForRegion.sqlSecretsKeyVaultName'] ]

...到...

    variables:
keyVaultName: $[ dependencies.DeployInfrastructure.outputs['DeployInfrastructure.SetArmOutputsForRegion.sqlSecretsKeyVaultName'] ]

但是,进行此更改后,变量值仍然为空。

这可能与另一个存储库中的模板化作业设置的变量有关吗?我不明白怎么做,但老实说我不认为我做错了什么:(

最佳答案

终于解决了这个问题。最终用我想分享的一个巧妙的技巧解决了这个问题。

在需要使用输出变量的作业中,添加以下变量:

variables:
checkDependencies: $[ convertToJson(dependencies) ]

然后添加以下任务:

    - script: "echo '$(checkDependencies)'"
name: checkDependencies

这会将整个 dependency JSON 对象打印到您的管道输出,允许您检查输出并避免所有猜测。如上所述here ,该对象采用以下格式:

"dependencies": {
"<STAGE_NAME>" : {
"result": "Succeeded|SucceededWithIssues|Skipped|Failed|Canceled",
"outputs": {
"jobName.stepName.variableName": "value"
}
},
"...": {
// another stage
}
}

就我的具体情况而言,(根据我的阅读)我在更新 1 中引用的文档似乎是错误的。执行上述检查告诉我,我需要使用这种格式来检索我的输出变量值:

$[ dependencies.DeployInfrastructure.outputs['Deploy.SetArmOutputsForRegionEun.sqlSecretsKeyVaultName'] ]

这解决了我的问题。文档建议我需要重新声明作业名称 (DeployInfrastruct) 作为变量名称前缀,但实际上我需要工作 Deploy。可能我误解了,但无论如何,上述技巧应该可以避免将来出现任何尝试和错误!

(请注意,对于跨阶段依赖项,您可以使用相同的检查技巧,但使用 stagedependencies 对象而不是 dependency)。

关于azure - 无法使用 Azure DevOps 管道中设置的输出变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72116469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com