Azure管道-将模板任务中设置的变量用作另一个模板任务中的参数 [英] Azure Pipeline - Use variable set in template task as a parameter in another template task

本文介绍了Azure管道-将模板任务中设置的变量用作另一个模板任务中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个模板-一个用于获取和设置某些配置(例如区域名称),另一个用于部署.我正在尝试使用在配置模板任务中设置的变量作为部署模板的参数输入.有实际的方法吗?

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)

推荐答案

但是,不幸的是,这些似乎都不起作用,因为它们都被拉入文字.有没有办法做到这一点?我已经看到了使用方法DependOn,但是我不希望两者之间存在隐藏的依赖关系模板(如果可能的话)

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管道-将模板任务中设置的变量用作另一个模板任务中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆