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

KickinMhl

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?

jessehouwing

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

cp command not working in Bash Script in build pipeline of azure devops

Maven build error in Azure DevOps Pipeline

Azure DevOps build pipeline unreliable triggering by schedule

Azure DevOps Build Pipeline - A failed build still gets deployed to Azure

Build pipeline for service fabric app on Azure devops

Azure Devops Pipeline build explorer

.Net Core solutions build locally, but fails build on Azure DevOPS pipeline

How do I get my Azure DevOps Pipeline build to fail when my linting script returns an error?

How build artifacts are managed in Azure DevOps pipeline?

Unable to open Excel when i run build (vstest) in azure devops pipeline

Azure DevOps build pipeline with hosted agent failing

NuGet package restore in Azure DevOps build pipeline

Whats the difference between a build pipeline and a release pipeline in Azure DevOps?

Azure DevOps: How to retrieve a build artifact from build Azure Pipeline from a PowerShell Script in Release Pipeline?

See Build Artifacts in Azure DevOps with YAML Pipeline

Azure DevOps Pipeline build triggering unexpectedly

Azure Devops Build Pipeline - Unexpected value stages

Combine variables in Azure Devops Build Pipeline YAML

Azure DevOps Release pipeline using build tags

Cannot access build number in Azure Devops pipeline bash script

Azure DevOps: Blazor Deployment fails in Pipeline Build

Provision a VM as part of an Azure Devops build pipeline

Run Google Test with build pipeline in Azure DevOps

Azure DevOps Build Pipeline with repository in GitHub

azure devops: copy and publish build artifacts - rename artifact by buildnumber

flutter build pipeline with azure devops

Azure devops build pipeline depends on other build pipeline

Azure DevOps Pipeline - Updating BuildNumber

Azure Devops Pipeline Docker Build