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

故事扮演 提交于 2020-02-02 11:56:26

问题


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.


回答1:


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.




回答2:


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:

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


来源:https://stackoverflow.com/questions/56517351/how-to-use-output-variables-across-agent-jobs-in-azure-release-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!