Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps Build Pipeline - Unable to access $(Build.BuildNumber) in Script

In my build pipeline I am doing the following:

  1. Restore from Git
  2. Powershell Script - to retrieve the Build number and write that to a json file BEFORE....
  3. Build solution
  4. Archive Files
  5. Publish Artifact.

In step 2 the Powershell script is pretty simple:

DEFINED ENV VARIABLES:

Name: buildNumber  Value: $(Build.BuildNumber)
Name: rootPath Value:$(Build.ArtifactStagingDirectory)

CODE:

$theFile = Get-ChildItem -Path $rootPath -Recurse -Filter "host.json" | Select-Object -First 1
$propertyName = "BuildNumber"

if($theFile)
{
    $json = Get-Content "$theFile" | Out-String | ConvertFrom-Json
    if($json.$propertyName)
    { 
        $json.$propertyName = $buildNumber
    }else{    
        Add-Member -InputObject $json -MemberType NoteProperty -Name $propertyName -Value $buildNumber
    }
    $json | ConvertTo-Json -depth 100 | Out-File "$theFile"

}
else
{
    Write-Warning "Found no files."
}

For some reason my $buildNumber is coming back null. The $rootPath is working. Am I not able to access the $(Build.BuildNumber) outside the build step? The build number format is defined in the Options for the Pipeline and it works fine when stamping the build, but I am unable to access it in my powershell script.

Any Thoughts?

like image 377
KickinMhl Avatar asked Oct 19 '18 20:10

KickinMhl


People also ask

What is ## VSO in PowerShell?

##vso[area.action property1=value;property2=value;...] message. To invoke a logging command, simply emit the command via standard output. For example, from a PowerShell task: Write-Host "##vso[task.setvariable variable=testvar;]testvalue"


1 Answers

Use $env:BUILD_BUILDNUMBER instead of the $(...) notation.

See the different notations for different script types in the docs.

like image 106
jessehouwing Avatar answered Oct 04 '22 08:10

jessehouwing