Skip to content

Update Azure Pipeline BuildNumber using PowerShell

I recently moved a build-job for our front-end library over to Azure Pipelines. This build-job builds multiple TypeScript projects where each one produces its own NPM package using the Lerna mono-repo toolkit.

We manage the version of the packages using the Lerna CLI. Lerna updates all the projects package.json as well as a file called lerna.json in the root of the repo.

The goal for our release build-job was to update the BuildNumber of the job instance from within the running build-job to the version configured in lerna.json and suffix it with the original build-number (which is a increasing integer number). The build-job should be listed as 1.4.3.598 instead of #598.

Solution

I created a little PowerShell script which I checked into the repo and added it as one of the first build steps to the build-job.

$lernaPath = "$Env:BUILD_SOURCESDIRECTORY\lerna.json"
$json = Get-Content "$lernaPath" | Out-String | ConvertFrom-Json
$version = $json.version
$buildNumber = $Env:BUILD_BUILDNUMBER;

Write-Host "##vso[build.updatebuildnumber]$version.$buildNumber"

The first line gets the path of the lerna.json. The second line reads the file content and converts its JSON content into a PowerShell object. The fourth line gets the version number out of that object. The fifth line gets the original build-number from Azure Pipelines using the environment variable.

The most important line ist the last one. It writes a special text to the console which gets interpreted by Azure Pipelines. The ##vso[... is the trigger for Azure Pipelines. The function build.updatebuildnumber is the Azure Pipeline function that should be called. Finally the $version is the variable assigned in line three and $buildNumber the variable from line four. The both variables get concat with a ..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: