Azure Pipeline-将模板任务中设置的变量用作另一个模板任务中的参数

问题描述 投票:0回答:1

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

我的配置模板:

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'

我的工作阶段看起来像这样(有不必要的切口):

- template: ../../tasks/azure/getConfig.yml
- template: ../../tasks/azure/deployToFA.yml
  parameters:
    appFullName: $(EastName)

我已经尝试过以下appFullName: <name>

  • $(EastName)
  • ${{ EastName }}
  • $[ EastName ]
  • $EastName

但是,可悲的是,这些似乎都没有用,因为它们都是按原义插入的。有办法吗?我已经看到了使用dependsOn的方法,但是我不希望两个模板之间存在隐藏的依赖关系(如果可能的话)

azure-devops azure-pipelines azure-pipelines-build-task azure-pipelines-yaml azure-pipelines-tasks
1个回答
0
投票

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

很抱歉,目前您的模板结构尚不支持。您可以检查Process the pipeline

要使管道变为运行,Azure管道按此顺序执行几个步骤:第一,展开模板并评估模板表达式。

因此${{ parameters.appFullName }}中的deploy template被评估之前 config template运行AzureCli任务。这就是none of these seem to work as they all get pulled in as literals的原因。根据设计,您的$(EastName)(运行时变量)在传递给参数时没有任何意义。

作为替代方法,检查Use variables as task inputs。它描述了另一种满足您需求的方法。

您的配置模板:

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

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.