Search code examples
azure-devopsazure-pipelinesazure-pipelines-build-taskazure-pipelines-yamlazure-pipelines-tasks

Azure Pipeline - Use variable set in template task as a parameter in another template task


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?

My config template:

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)

I have tried the following for appFullName: <name>:

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

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)


Solution

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

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

    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.

    Your config template:

    steps:
    - task: AzureCLI@2
      name: Config
      displayName: Get Config and Generate Variables
      inputs:
        xxx
    

    Your deploy template:

    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'
    

    Your stage:

    - template: ../../tasks/azure/getConfig.yml
    - template: ../../tasks/azure/deployToFA.yml
    

    Hope it helps.