1

I'm trying to use the Build variables in a script. According to this documentation I should be able to use the following:

Write-Host "BUILD_DATE: $Env:BUILD_DATE"
Write-Host "BUILD_REV: $Env:BUILD_REV"

However, I only get the following output

BUILD_DATE:
BUILD_REV:

I've also tried this syntax:

Write-Host "BUILD_DATE: $(Env:BUILD_DATE)"
Write-Host "BUILD_REV: $(Env:BUILD_REV)"

Write-Host "BUILD_DATE: $(Build.Date)"
Write-Host "BUILD_REV: $(Build.Rev)"

But the first segment gives The term 'Env:BUILD_DATE' is not recognized and the second segment gives The term 'Build.Date' is not recognized

How can I use the build variables in my script?

2
  • 1
    first one should work. oh wait, variable called build_Date doesnt exist, same as build_rev Commented Jun 15, 2019 at 12:38
  • The "Build number format" contains $(Date:yyyyMMdd)$(Rev:.r) by default, and I want to access those from my script (and format them the same way) Commented Jun 15, 2019 at 13:18

1 Answer 1

2

Disclaimer: I know virtually nothing about Azure pipelines, so my answer is based on reading the docs. Do let us know if I got things wrong.

Your first command uses the correct syntax for referencing environment variables in PowerShell (also inside an expandable (double-quoted) string).
(The other commands, based on subexpression operator $(...), mistakenly try to execute commands named Env:BUILD_DAT, ... rather than referencing variables.)

Your problem seems to be that the targeted environment variables do not exist.

The list of predefined variables that are exposed as environment variables does not contain variables named Build.Date / $env:BUILD_DATE and Build.Rev / $env:BUILD_REV.

By contrast, variables named Date and Rev seemingly do exist - as you state, they are used in the default format definition for the Build.BuildNumber / $Env:BUILD_BUILDNUMBER build variable, $(Date:yyyyMMdd)$(Rev:.r) - but are seemingly of a different kind not exposed as env. vars. (unlike Build.BuildNumber / $Env:BUILD_BUILDNUMBER itself, which is exposed).

(I don't know where these variables are defined or how they are classified, and where this is documented - do tell us if you know.)

A quick workaround would be to split the value of $Env:BUILD_BUILDNUMBER into its constituent parts:

# Split the build number into date and revision, by "."
$date, $rev = $Env:BUILD_BUILDNUMBER -split '\.'

"BUILD_DATE: $date"
"BUILD_REV: $rev"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.