How to use output variables across agent jobs in azure release pipeline
Asked Answered
F

2

8

In my azure release pipeline I have 2 agent jobs, one is for sql deployment using power-shell and other is for kubernetes using power-shell. How to set an output variable in 1st agent job and use that in second agent job using power-shell.

enter image description here

Faveolate answered 9/6, 2019 at 18:34 Comment(0)
A
4

How to use output variables across agent jobs in azure release pipeline

I am afraid there is no way to use output variables across agent jobs directly for now.

There is a related issue Variables set via logging commands are not persistent between agents, you can follow up.

To resolve this problem, you can try following workaround:

  • Define a variable in the release definition Variable.
  • Use REST API (Definitions - Update) to update the value of the release definition variable in the agent job 1.
  • Use the updated value of the release definition variable in the next agent job.

The details info about using REST API to update the value of the release definition variable, you can follow the below ticket:

How to modify Azure DevOps release definition variable from a release task?

Hope this helps.

Alyciaalyda answered 11/6, 2019 at 2:49 Comment(6)
Thanks a lot.. Could you please guide how can I use the variable using powershell in my next job. I tried $env:variableName, $(variableName), ${env:variableName} but not working by powershell.Faveolate
The $(variableName) should be work. Have you get the value from the release definition Variable?Alyciaalyda
Came to know very strange behaviour that the variable must be in all uppercase then only I am able to get the value on Linux. Tried echo "hint is $env:VARIABLENAME" and it worked. Is there anything i can try so it work on both cases??Faveolate
Yes, its release def variable.Faveolate
@VatanSoni, We could use the secret value explicitly into a script or a program from your build step as $(variableName), but we could not show it in our task. #50110815Alyciaalyda
If you modify a release variable by calling the REST api within one job of the release, subsequent jobs in the release do not seem to pick up the modified variables. Only if you create a new release after modifying the variable do you see the changes.Panfish
D
13

Use

Write-Output "##vso[task.setvariable variable=testvar;isOutput=true;]testvalue"

Then reference the output variable as if it exists from a future task.

$(taskreference.testvariable)

The task reference name can be set on the output section of the powershell script task:

enter image description here

But it looks like cross-job references aren't available yet, when I read the docs:

TODO

I am not sure how are we going to generate Job ref name, since we don’t have job chaining at this point.

It should be something like:

{DefinitionName}_{JobName}

See: Azure-Pipelines-Agent/docs/Outputvariable.md

So for now the variable will only work within the same Job.

It does look like YAML build do already support cross-phase output variable references.

jobs:

# Set an output variable from job A
- job: A
  pool:
    vmImage: 'vs2017-win2016'
  steps:
  - powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true;]this is the value"
    name: setvarStep
  - script: echo $(setvarStep.myOutputVar)
    name: echovar

# Map the variable into job B
- job: B
  dependsOn: A
  pool:
    vmImage: 'ubuntu-16.04'
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ]  # map in the variable
                                                                          # remember, expressions require single quotes
  steps:
  - script: echo $(myVarFromJobA)
    name: echovar
Durkin answered 9/6, 2019 at 20:17 Comment(5)
That may work. I'm not 100% sure whether it works in release stages.Durkin
How to consume it, what is taskreference here??Faveolate
Added more guidance. I was missing a ; after isOutput=true;, the agent can be picky so try that first. The docs are conflicting on whether cross-job-output-variables do or do not work. The docs clearly state how to make this work when using the new YAML build format. The UI-based docs state that this functionality is still TODO. Try for yourself.Durkin
I tried, it works within same job but don't go across another job. Anyway thanks a lot.Faveolate
Could you be more specific about how to set it in the task reference name? and post your answer here #59370119Stumpy
A
4

How to use output variables across agent jobs in azure release pipeline

I am afraid there is no way to use output variables across agent jobs directly for now.

There is a related issue Variables set via logging commands are not persistent between agents, you can follow up.

To resolve this problem, you can try following workaround:

  • Define a variable in the release definition Variable.
  • Use REST API (Definitions - Update) to update the value of the release definition variable in the agent job 1.
  • Use the updated value of the release definition variable in the next agent job.

The details info about using REST API to update the value of the release definition variable, you can follow the below ticket:

How to modify Azure DevOps release definition variable from a release task?

Hope this helps.

Alyciaalyda answered 11/6, 2019 at 2:49 Comment(6)
Thanks a lot.. Could you please guide how can I use the variable using powershell in my next job. I tried $env:variableName, $(variableName), ${env:variableName} but not working by powershell.Faveolate
The $(variableName) should be work. Have you get the value from the release definition Variable?Alyciaalyda
Came to know very strange behaviour that the variable must be in all uppercase then only I am able to get the value on Linux. Tried echo "hint is $env:VARIABLENAME" and it worked. Is there anything i can try so it work on both cases??Faveolate
Yes, its release def variable.Faveolate
@VatanSoni, We could use the secret value explicitly into a script or a program from your build step as $(variableName), but we could not show it in our task. #50110815Alyciaalyda
If you modify a release variable by calling the REST api within one job of the release, subsequent jobs in the release do not seem to pick up the modified variables. Only if you create a new release after modifying the variable do you see the changes.Panfish

© 2022 - 2024 — McMap. All rights reserved.