【发布时间】:2019-12-22 12:09:11
【问题描述】:
当在 Azure Pipelines 中使用来自 yaml 的 multistage pipelines 并且每个阶段都将资源部署到单独的环境时,我想为每个阶段使用专用的服务连接。就我而言,每个阶段都使用相同的部署作业,即 yaml 模板。因此,我使用了许多具有特定值的变量,具体值取决于环境。这工作正常,除了服务连接。
理想情况下,包含服务连接名称的变量被添加到阶段级别,如下所示:
stages:
- stage: Build
# (Several build-stage specific jobs here)
- stage: DeployToDEV
dependsOn: Build
condition: succeeded()
variables:
AzureServiceConnection: 'AzureSubscription_DEV' # This seems like a logical solution
jobs:
# This job would ideally reside in a yaml template
- job: DisplayDiagnostics
pool:
vmImage: 'Ubuntu-16.04'
steps:
- checkout: none
- task: AzurePowerShell@4
inputs:
azureSubscription: $(AzureServiceConnection)
scriptType: inlineScript
inline: |
Get-AzContext
azurePowerShellVersion: LatestVersion
- stage: DeployToTST
dependsOn: Build
condition: succeeded()
variables:
AzureServiceConnection: 'AzureSubscription_TST' # Same variable, different value
jobs:
# (Same contents as DeployToDEV stage)
当执行这段代码sn-p时,会产生错误信息:
存在资源授权问题:“管道无效。 Job DisplayDiagnostics:步骤 AzurePowerShell 输入 ConnectedServiceNameARM 引用服务连接 找不到 $(AzureServiceConnection)。服务 连接不存在或未被授权使用。为了 授权详情请参考https://aka.ms/yamlauthz。
所以,它可能无法在运行开始时尽快expand the variable AzureServiceConnection。但如果情况确实如此,那么为每个阶段使用单独的服务连接的替代解决方案是什么?
一个确实可行的选项是将服务连接名称直接设置为所有任务,但这将涉及为每个阶段复制相同的 yaml 任务,我显然希望避免这种情况。
有人对此有线索吗?提前致谢!
【问题讨论】:
-
这对我来说真的很烦人,因为我需要能够指定它们,因为我们在不同的 Azure 订阅中进行开发和生产,并且硬编码变量是真的不好的做法进入文件。
标签: azure-devops yaml azure-pipelines