【问题标题】:Azure Pipeline - Use variable set in template task as a parameter in another template taskAzure Pipeline - 使用模板任务中的变量集作为另一个模板任务中的参数
【发布时间】:2020-04-08 20:14:49
【问题描述】:

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

我的配置模板:

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】:

    但是,遗憾的是,这些似乎都不起作用,因为它们都被作为 文字。有没有办法做到这一点?我见过使用方法 dependsOn 但我不希望两者之间存在隐藏的依赖关系 模板(如果可能的话)

    抱歉,您的模板结构目前不支持。你可以查看Process the pipeline:

    为了将管道转变为运行,Azure Pipelines 按以下顺序执行几个步骤: 首先,展开模板并计算模板表达式。

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

    作为替代方式,请查看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
    

    希望对你有帮助。

    【讨论】:

    • 啊,我也是这么想的。感谢你的回答!拥有$(Config.EastName) 会增加我希望避免的隐藏依赖。我想我最终必须想出另一种方法来做这件事
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2014-04-16
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多