Search code examples
azure-pipelinesazure-pipelines-tasksazure-pipelines-yaml

Pass values between template jobs in azure pipelines yaml


I have the following setup where the first templated job (deploy-infra.yml) runs some terraform jobs which produce some output which are needed in later templated jobs, what I can seem to do is pass this output to other templated jobs, this seems to be because template parameters are determined at compile time not runtime.

Is there a way to do this? This is what I have currently:

- stage: Deploy_Canary
  displayName: Deploy Canary

  jobs:

  - template: deploy-infra.yml

  - template: deploy-software.yml
    parameters:
      dbserver: $[dependencies.DeployInfra.outputs['outputDeployInfra.dbserver']]

deploy-infra.yml produces this as an output which is taken from a powershell script which in turn takes output from a terraform module:

- pwsh: |
    echo "##vso[task.setvariable variable=dbserver]$(db.server)"
  name: outputDeployInfra

If I echo out parameters.dbserver in the deploy-software.yml job I just get:

$[dependencies.DeployInfra.outputs['outputDeployInfra.dbserver']]

Any ideas?! Thanks!


Solution

  • Pass values between template jobs in azure pipelines yaml

    We need move the parameters from azure-pipelines.yaml, then parse it in the deploy-software.yml with variables:

      variables:
        Parametersdbserver: $[dependencies.DeployInfra.outputs['outputDeployInfra.dbserver']]
    

    As test, I create deploy-infra.yml, deploy-software.yml and azure-pipelines.yaml:

    deploy-infra.yml (Since I do not have the value of db.server, I defined it by variable with test value 123456.):

    jobs:
    - job: DeployInfra
      variables:
        db.server: 123456
    
      steps:
      - checkout: none
      - pwsh: |
          echo "##vso[task.setvariable variable=dbserver;isOutput=true]$(db.server)"     
        name: outputDeployInfra
    

    deploy-software.yml:

    jobs:
    - job: deploysoftware
      dependsOn: DeployInfra
      variables:
        Parametersdbserver: $[dependencies.DeployInfra.outputs['outputDeployInfra.dbserver']]
    
      steps:
      - checkout: none
      - pwsh: |
          Write-Host "$(Parametersdbserver)"
    

    azure-pipelines.yaml:

    pool:
      vmImage: 'windows-latest'
    
    stages:
      - stage: Deploy_Canary
        jobs:
        - template: deploy-infra.yml
    
        - template: deploy-software.yml
    

    As the test result:

    enter image description here

    Hope this helps.