0

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!

mattb
  • 384
  • 1
  • 4
  • 24

1 Answers1

1

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.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • thanks for taking the time to look at this, unfortunutally I still can't make this work, even if I just run what you have above and none of my other code. Is there anything enviromental needed to also make this work? – mattb Mar 06 '20 at 13:50
  • @mattb, There is actually no additional environmental. I updated the answer with more attributes to make sure it work for you. You can test it. Besides, you should make sure those three .yml file in the same location, same branch. If it still not work for you, try to share the log to me. – Leo Liu Mar 06 '20 at 14:25
  • thanks, I've narrowed the problem down to this not working in a deployment job, using a normal job is ok, but not a deployment one. Is there something different I should do when using deployment jobs? – mattb Mar 09 '20 at 10:54
  • 1
    Actaully I think I figured it out, I needed to reference the deployment job inside this square brackets which you don't seem to need to do with a standard job `$[dependencies.DeployInfra.outputs['**DeployInfra**.outputDeployInfra.dbserver']]` as documented here [link](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-variables-in-scripts) – mattb Mar 09 '20 at 10:59