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

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

问题描述

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

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"

我的部署模板如下所示:

and my deploy template looks like this:

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)

推荐答案

但是,遗憾的是,这些似乎都不起作用,因为它们都被拉进来了文字.有没有办法做到这一点?我见过使用方法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 Pipelines 按以下顺序执行几个步骤:首先,扩展模板并评估模板表达式.

To turn a pipeline into a run, Azure Pipelines goes through several steps in this order: First, expand templates and evaluate template expressions.

所以 deploy template 中的 ${{ parameters.appFullName }} 被评估 before config template运行 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 Pipeline - 使用模板任务中设置的变量作为另一个模板任务中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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