0

New to AzureDevOps, new to Blazor server.

If I have a yaml task like this in my Azure DevOps pipeline build and want to display the build name on the UI in a Blazor Server app, how would I be able to access this information? Can I write it to the csproj file? appsettings? I'm not coming up with much info online so far and seek guidance on how to implement this.

variables:
  version.MajorMinor: '1.0' # Manually adjust the version number as needed for semantic versioning. Revision is auto-incremented.
  version.Revision: $[counter(variables['version.MajorMinor'], 0)]
  versionNumber: '$(version.MajorMinor).$(version.Revision)'

steps:
- task: PowerShell@2
  displayName: Set the name of the build (i.e. the Build.BuildNumber)
  inputs:
    targetType: 'inline'
    script: |
      [string] $buildName = "$(versionNumber)_$(Build.SourceBranchName)"
      Write-Host "Setting the name of the build to '$buildName'."
      Write-Host "##vso[build.updatebuildnumber]$buildName"
J.G.Sable
  • 1,258
  • 4
  • 26
  • 62

1 Answers1

0

There are tasks that you can use in the pipeline to write the build name in the csproj file and appsetting files.

For example. If you want to write the build name in the appsettings.json file. You can use File Transform task.

First you need to include the buildName property in the appsettings.json file. Let's say you have included the buildName property like below appsetting.json file.

{
  "AppSettings": {
        "buildName": "1234"
    }
}

Then you can replace the buildName in your pipeline using File Transform task. See below steps:

1, Add Write-Host "##vso[task.setvariable variable=AppSettings.buildName]$buildName" to your above powershell task to define a variable AppSettings.buildName and assign the buildName value to it. See below:

- task: PowerShell@2
  displayName: Set the name of the build (i.e. the Build.BuildNumber)
  inputs:
    targetType: 'inline'
    script: |
      [string] $buildName = "$(versionNumber)_$(Build.SourceBranchName)"
      Write-Host "Setting the name of the build to '$buildName'."
      Write-Host "##vso[build.updatebuildnumber]$buildName"
      
      #define variable 'AppSettings.buildName'
      Write-Host "##vso[task.setvariable variable=AppSettings.buildName]$buildName"

2, Add File Transform task in your pipeline after above powershell task

- task: FileTransform@1
  inputs:
    folderPath: '$(System.DefaultWorkingDirectory)' 
    fileType: 'json'

Then the buildName in the appsettings.json file will be replaced to the actual value of buildName.

For more information about XML variable substitution, please check it out here.

If you want to write the build name to csproj file. You need to include the buildName property in the csproj file. Then you can use tasks Magic Chunks/ RegEx Find & Replace to replace its value in your pipeline. See this thread for example.

When you have the build name in the appsettings.json/csproj files. Then you can get its value in your code to display it on the UI.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43