6

I created a new build with Azure Pipelines (Azure DevOps) and it worked really well.

Usually, you use $(Rev:.r) to get the revision in the build. Unfortunately, it seems the variable isn't replaced/set in the build steps. The only place where you can use it is the name: property in the YAML document.

Now I set it in the name and extract it in some PowerShell, which isn't necessary if you can get it via an environment variable.

How do I get the Revision (like $(Rev)) in the new builds (outside of the name: property in the YAML document)?

(The Build Agents running on-premise, inside Docker - but this shouldn't affect the things above)

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
pr177
  • 576
  • 7
  • 19

2 Answers2

6

You can't get the revision number without parsing, it is not stored as a separate field somewhere or in an environment variable.

The $(Rev:.r) portion instructs Azure DevOps to come up with the first number that makes the build number unique (and, in that specific example, put a dot in front of it).

Like you said, the only way is to use PowerShell script to get the value:

$buildNumber = $Env:BUILD_BUILDNUMBER
$revision= $buildNumber.Substring($buildNumber.LastIndexOf('.') + 1)

Edit:

You can install the Get Revision Number extension that does it.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
1

Another possible solution to the above problem could be to use counter expression for ex: we difine the variable and use it in a task to build nuget package.

 variables:
      counterVar: $[counter($(versionVariable),0)]
  .......
   - task: CmdLine@2
  inputs:
    script: >
      nuget pack ClassLibrary1/ClassLibrary1.csproj  
      -OutputDirectory $(Build.ArtifactStagingDirectory)
      -NonInteractive 
      -Properties Configuration=release
      -Version $(versionVariable).$(counterVar)
      -Verbosity Detailed 
      -IncludeReferencedProjects

Here versionVariable is a custome variable defined in pipelines->variables.And the seed value is 0(2nd param to counter). It works as below Let's assume the versionVariable is 1.19

  • Build Run 1 counterVar will be 0.
  • Build Run 2 counterVar will be 1.
  • Now say we change the versionVariable to 1.20
  • Build Run 3 counterVar will be 0.

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops Check the counter expression in above link it reset its value for diff prefix.

P.S. Benefit of using counter over $(Rev:r) is that it can start from 0 unlike $(Rev:r)

GPuri
  • 495
  • 4
  • 11