Azure DevOps - Task output to a variable

I’ve been building a couple of Continuous Integration / Continuous Delivery (CI / CD) pipelines on Azure DevOps lately.

Azure DevOps is formerly known as Visual Studio Team Service (VSTS). As I mentioned recently, VSTS had nothing to do with Visual Studio and we’re very happy with the new brand name.

With CI / CD, as with automation in general, I want the least code possible for maximum impact.

Something was missing in my tool belt. I needed to carry information from a task to another. I needed to get the output of a task and use it as the input of other tasks.

I struggled for a while before two of my colleagues (Mathieu Benoit & Kevin Kraus) showed me how to do that.

It’s a simple trick but one that isn’t documented prominently, so I wanted to share that with you here.

A Use Case

Let’s give an example where that technique was needed. That was actually the scenario that triggered our search for an output variable technique.

We wanted to manage the version of a micro service with Semantic Versioning, i.e. MAJOR.MINOR.PATCH.

For us, the MAJOR & MINOR belong to the code as it relates to compatibility and feature set. The PATCH part didn’t and it would be error prone to expect programmers to update a patch number in the code base each time they deploy something. We were keen to use the build number of Azure Dev Ops. That number increments each time there is a build.

We also wanted the full version (i.e. MAJOR.MINOR.PATCH) to be injected in the code so it could be returned as a service version. This way we have an easy way to identify which version of a service we are talking to.

This is a schematized version of our build pipeline:

Version management

Task Description
Fetch Full Version Python task taking the build number as an input. It fetches the MAJOR.MINOR version from the code files, append the build number and set the Full Version variable.
Write Full Version We write the Full Version variable as a build artefact. This will be used at release time.
Inject Version in code Python task taking the Full Version variable and performing a simple find / replace in the code. This will allow the service to return the full version.
Build Docker Image Builds a Docker from the code. This doesn't directly use the Full Version variable.
Push Docker Image Pushes the Docker image to Docker Hub. We use the Full Version as the image tag.
Helm Package Packages an Helm Chart. We use the Full Version as the chart version.

It’s easy to see that without an output variable, this pipeline would be very tedious.

The Solution

The solution is pretty straightforward. It doesn’t require calling an API or an SDK, just to output something on the console:

##vso[task.setvariable variable=<variable name>;]<variable value>

That’s it!

Azure DevOps will automatically pick it up. So we can do that from Python, PowerShell, Bash shell, anything that can output to the console.

For instance, our Python line was:

print('##vso[task.setvariable variable=full-version;]%s' % (fullVersion))

The variable we defined here is then instantiated as a variable in Azure DevOps and can be used by subsequent tasks in the same pipeline.

Here we used it in a build. The same thing can be done in a release pipeline.


Something simple that I found extremely useful in some pipeline.

It allows us to couple loosely tasks together. The alternative is having tasks do more while they hold the state of what should be a variable.

4 responses

  1. Ramesh 2018-11-18 at 10:27

    I have integrated SonarQube as a task 1) Powershell - analysis start 2) Prepare Analysis - Prepare Analysis Configuration Task Here I have added in the Adfanced –> Additional Properties sonar.cs.analyzer.projectOutPaths=$(Build.SourcesDirectory) sonar.issuesReport.json.enable=true


    3) Run Code Analysis - Run Code Analysis Task 4) Publish Quality Gate Result 5) Powershell - analysis end

    But in spite of those 3 parameters , I do not see the analysis file being generated. If you can let me know a way to captur ethe analysis file that would be great.

    FYI, my sonar server is separate from the ADO servers and I could see the results being published in the sonar server successfully.

  2. cmoate 2019-09-23 at 19:23

    Thanks so much for this.

  3. Paulius 2019-10-09 at 06:23

    thank you! :)

  4. Pay 2020-01-24 at 20:13

    Thanks a ton.

Leave a comment