Azure DevOps: store the TAG version in a system variable

Luca Nardelli
3 min readFeb 5, 2019

--

Microsoft Azure DevOps is a beautiful system, well documented and with lot of functionalities ready out-of-the-box.

The CI (Continuous Integration) and CD (Continuous Delivery) are a pleasure to use, but sometimes a little bit tricky.

A common problem is to use a stable release pipeline to create a directory or a zip file, named with the TAG of the Artifact used as source.

To do that, the first step is to define a build pipeline that automatically starts when a TAG is pushed on the master branch, by using the filter refs/tags/*:

Trigger definition in the build pipeline

At this point the build starts only if a TAG is pushed on the master:

As you can see, the build has been executed on the 1.0.4 TAG:

Build pipeline completed with success on 1.0.4 TAG.

Now we can define a release pipeline, using the previous build (his Artifact) as source and trigger the first Stage only if the Artifact is created on a branch stored in /refs/tags/ (this mean a TAG).

Release pipeline with CI filtered on TAGs (refs/tags/*)

Now the CI and CD are triggered when a new TAG is created on the master branch.

But how to get, transform and use the TAG version (e.g. 1.0.4) via system variable?

Imagine for example that I’d like to build my repository and zip the created Artifact in a file named Stable_v1_0_4_234.zip, where 234 is the build number of the specific build pipeline that triggers the release.

I need to get the TAG version (1.0.4), modify it (v1_0_4) and store it in a system variable.

As defined in the Azure DevOps documentation, there is a default release variable that can be used to retrieve the source branch (in our case refs/tags/1.0.4): Release.Artifacts.{alias}.SourceBranch, where “alias” is the name of the artifact.

So let me to change the Artifact’s alias to “_art1”:

Change the artifact source alias to “_art1”

Now I can retrieve the branch name using Release.Artifacts._art1.SourceBranch.

In the Stage1 I will use this variable to set an environment variable (named ReleaseFullPath) in a PowerShell Task and I will define zipSuffix as output variable:

The PowerShell Task will manipulate the ReleaseFullPath variable (refs/tags/1.0.4) in order to extract just the TAG version (1.0.4) and transform it in the requested syntax (v1_0_4), publishing then the result in the zipSuffix output variable. The task use the follow PowerShell code:

Write-Host “Value of ReleaseFullPath: ‘“(Get-ChildItem Env:ReleaseFullPath).value”’”

$_zipSuffix = “v”+((Get-ChildItem Env:ReleaseFullPath).value).split(“/”)[2]

$_zipSuffix = $_zipSuffix.replace(“.”,”_”)

Write-Host (“##vso[task.setvariable variable=zipSuffix;isSecret=false;]$_zipSuffix”)

Now is possible to use the zipSuffix system (output) variable in the zip task:

That’s all :/)

--

--