Using Revision Variable in Docker Build Step

Solution 1:

You can't get just "the revision number" without parsing, it is not stored as a separate field somewhere. 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). Only the final build number is available.

At workaround, add a the $(Rev:.r) in the end of your build number (if it not there). add a PowerShell script task (you can do it inline PowerShell) before the Docker task and put this code:

$buildNumber = $Env:BUILD_BUILDNUMBER
$revision= $buildNumber.Substring($buildNumber.LastIndexOf('.') + 1)
Write-Host ("##vso[task.setvariable variable=revision;]$revision")

In your docker use the $revision variable:

-t wp/imagename:0.6$(revision)

Solution 2:

I've only been able to get it to be recognized in the Build number format section under options.

If you are using this like a build number, why not just set the build number there instead and then reference using $(Build.BuildNumber)?