问题描述
我创建了两个模板-一个用于获取和设置某些配置(例如区域名称),另一个用于部署.我正在尝试使用在配置模板任务中设置的变量作为部署模板的参数输入.有实际的方法吗?
I have two templates created -- one for getting and setting some configurations such as region names and one for deploying. I'm trying to use the variables set in the configuration template task as parameter inputs to the deploy template. Is there an actual way of doing this?
我的配置模板:
steps:
- task: AzureCLI@2
name: Config
displayName: Get Config and Generate Variables
inputs:
azureSubscription: '$(Subscription)'
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
Environment="prod"
echo "##vso[task.setvariable variable=Environment;isOutput=true]prod"
echo "##vso[task.setvariable variable=EastName;isOutput=true]$(AppNamePrefix)-$Environment-eastus"
echo "##vso[task.setvariable variable=East2Name;isOutput=true]$(AppNamePrefix)-$Environment-eastus2"
echo "##vso[task.setvariable variable=CentralName;isOutput=true]$(AppNamePrefix)-$Environment-centralus"
echo "##vso[task.setvariable variable=WestName;isOutput=true]$(AppNamePrefix)-$Environment-westus"
我的部署模板如下:
parameters:
- name: artifactName
type: string
default: MyBuildOutputs
- name: appFullName
type: string
- name: condition
type: boolean
default: true
steps:
- task: AzureFunctionApp@1
condition: ${{ parameters.condition }}
displayName: 'Production deploy'
inputs:
azureSubscription: '$(Subscription)'
appType: 'functionApp'
appName: ${{ parameters.appFullName }}
package: '$(System.ArtifactsDirectory)/${{ parameters.artifactName }}/$(Build.BuildId).zip'
deploymentMethod: 'auto'
和我的阶段看起来像这样(有不必要的切口):
and my stage for this looks like so (with unnecessary bits cutout):
- template: ../../tasks/azure/getConfig.yml
- template: ../../tasks/azure/deployToFA.yml
parameters:
appFullName: $(EastName)
我为 appFullName尝试了以下操作:< name>
:
-
$(EastName)
-
$ {{EastName}}
-
$ [EastName]
-
$ EastName
但是,可悲的是,这些似乎都不起作用,因为它们都被当作字面量引入了.有没有办法做到这一点?我已经看到了使用 dependsOn
的方法,但是我不希望两个模板之间存在隐藏的依赖关系(如果可能的话)
but, sadly, none of these seem to work as they all get pulled in as literals. Is there a way of doing this? I have seen ways of using dependsOn
but I don't want the hidden dependency between the two templates (if that's even possible)
推荐答案
对不起,但恐怕暂时不支持您的模板结构.您可以检查处理管道:
Sorry but I'm afraid your template structure is not supported for now. You can check Process the pipeline:
要将管道变成运行管道,Azure管道按以下顺序执行几个步骤:首先,展开模板并评估模板表达式.
To turn a pipeline into a run, Azure Pipelines goes through several steps in this order:First, expand templates and evaluate template expressions.
因此,在配置模板
之前评估部署模板
中的 $ {{parameters.appFullName}}
运行AzureCli任务.这就是为什么这些代码似乎都不起作用的原因,因为它们全部都被当作字面量提取了
.根据设计,您的 $(EastName)
(运行时变量)在传递给参数时没有任何意义.
So the ${{ parameters.appFullName }}
in deploy template
is evaluated before the config template
runs the AzureCli task. That's why none of these seem to work as they all get pulled in as literals
. It's by design that your $(EastName)
(runtime variable) has no meaning when being passed to parameter.
另一种方法,请检查将变量用作任务输入.它描述了另一种满足您需求的方法.
As an alternative way, check Use variables as task inputs. It describes another way to meet your needs.
您的配置模板:
steps:
- task: AzureCLI@2
name: Config
displayName: Get Config and Generate Variables
inputs:
xxx
您的部署模板:
parameters:
- name: artifactName
type: string
default: MyBuildOutputs
- name: appFullName
type: string
- name: condition
type: boolean
default: true
steps:
- task: AzureFunctionApp@1
condition: ${{ parameters.condition }}
displayName: 'Production deploy'
inputs:
azureSubscription: '$(Subscription)'
appType: 'functionApp'
appName: $(Config.EastName) // Changes here. ********* Config is the name of your AzureCLI task.
package: '$(System.ArtifactsDirectory)/${{ parameters.artifactName }}/$(Build.BuildId).zip'
deploymentMethod: 'auto'
您的阶段:
- template: ../../tasks/azure/getConfig.yml
- template: ../../tasks/azure/deployToFA.yml
希望有帮助.
这篇关于Azure管道-将模板任务中设置的变量用作另一个模板任务中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!