问题描述
我有一个 yaml。我需要根据另一个任务将 serviceconnections 传递给脚本/模板,该任务检索所有必需的订阅。 问题是:我可以将动态值传递给服务连接吗?它给了我编译时错误。
我的代码如下:
trigger: none
pr: none
parameters:
- name: AzureSubscription
type: object
default:
xxx:
Sub: xxx
yyy:
Sub: yyy
jobs:
- job: Updating
condition: succeeded()
pool:
vmImage: "windows-latest"
strategy:
maxParallel: 10
matrix: ${{ parameters.AzureSubscription }}
steps:
- task: AzurePowerShell@5
displayName: Tes
inputs:
azureSubscription: 'zzz'
ScriptType: 'Inlinescript'
Inline: |
Write-Output "subcriptionList ---- $(Sub)"
FailOnStandardError: true
azurePowerShellVersion: 'LatestVersion'
pwsh: true
- task: AzurePowerShell@4
displayName: Updating
inputs:
**azureSubscription: ${{ sub }}** # here it is giving me error?
ScriptType: 'FilePath'
ScriptPath: '$(System.DefaultWorkingDirectory)/Foundation/xxxxx.ps1'
azurePowerShellVersion: 'LatestVersion'
所以在第二个任务中,我从我的参数传递订阅。
Error is : Unrecognized value: 'sub'.
有人可以帮我吗?
解决方法
您不能动态设置 azureSubcription
。是 known limitation。
@JoeGaggler 今天不支持此功能。在发布/构建定义中使用服务端点(Azure 订阅是其中一种)由某些权限控制。在保存发布/构建定义服务时,验证作者(保存定义的人)对端点具有适当的权限。如果我们支持服务端点输入的变量替换,则服务无法验证作者是否具有所需的权限,这可能会成为安全问题。
,通过一些创造力可以做到这一点,但不是任务所固有的。而是使用变量并动态加载变量文件。
我通常将服务连接名称声明为单独存储库中的变量模板文件。这允许在组织中的所有项目中重用,这不是必需的,但这样会更容易。模板名称的一部分是要部署到的环境。因此,模板文件可能被称为 azure.dev.yml
或 azure.uat.yml
,看起来像:
variables:
AzureSubscriptionServiceConnectionName: Azure - Dev
然后在阶段/作业范围内定义的变量将加载模板文件,如下所示,假设将使用给定的 environmentName 传入参数或局部变量。
variables:
- template: /azure.${{ parameters.environmentName }}.yml
然后舞台/作业可以通过以下方式引用此变量:
${{ variables.AzureSubscriptionServiceConnectionName }}
这里还有一些Microsoft Documentation on YAML Pipeline Variable Scope