Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display build version on web page

I am developing a web site using asp.net core.
And I publish it with Visual Studio or/and VSTS.

I want to display some information about which build it is on the web page.
(something like rev 2016.9.20.4002)

How can I do that?

like image 938
qin Avatar asked Dec 15 '22 03:12

qin


2 Answers

You can track it with build number.

  1. Go to your VSTS site and create build definition
  2. Select General tab, specify build number format, for example: $(date:yyyyMMdd)$(rev:.r)
  3. (Optional) Select Triggers tab, check Continuous integration (CI) and configure filters if you want queue build for each check-in.
  4. Configure other settings (e.g. steps/task in Build tab) After build complete, go to the summary of that build definition (click build definition title hyperlink to go to summary page), the result will be like this:

enter image description here

Steps to display build number to your website:

  1. Install Replace Tokens extension to you VSTS
  2. Edit your build definition to add Replace token task
  3. Specify target files and root directory (for asp.net core app, you can specify **\appsettings.json) enter image description here
  4. Select Variable tab and add a new variable. Save your build definition enter image description here
  5. Edit appsettings.json file of your asp.net project. Sample code:
 {
      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication1-ab933d83-8f4b-4024-9f3c-1aef5339a8f3;Trusted_Connection=True;MultipleActiveResultSets=true"
      },
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Debug",
          "System": "Information",
          "Microsoft": "Information"
        }
      },
      "CodeVersion": {
        "Num": "#{MyBuildNumber}#"
      }
    }
  1. Add logical to your asp.net project to read appsettings.json to get specific value and display in the page.
  2. Check in your code and queue build.
like image 82
starian chen-MSFT Avatar answered Jan 11 '23 17:01

starian chen-MSFT


After a day of research, finally found/created a better option than using any random app (Replace Token) from Marketplace.

The option I am talking is already available in VSTS, Azure CLI task.

Here are the stpes:

  1. Add setting BUILD_NUMBER with initial value of 1.0 in appsettings.json
  2. Read appsettings.json in your app and display it. I am sure you all are smart enough to figure out how to use appsettings to display Build Number on your WebApplication.
  3. In Azure Portal, similarly create an App Setting named BUILD_NUMBER with initial value of 1.0 in Azure Application settings section under App Services for your App.
  4. In VSTS, In your Release definition, add a task Azure CLI.
  5. Populate required fields such as Azure Subscription, Script Location with Inline script and last but most important Inline Script with following CLI command

az webapp config appsettings set -n iCoreTestApi -g ArchitectsSandbox -s Dev --settings BUILD_NUMBER=$(Build.BuildNumber)

Command explanation:

  • iCoreTestApi should be replaced by your real WebApp or Api name in Azure
  • ArchitectsSandbox should be replaced by your resource group in Azure
  • Dev is the slot name, you may or may not have it.
  • Rest of the command remains same.

Once you will queue new build, after successful completion of the deployment, you can see app settings section on Azure is updated with new BUILD_NUMBER.

Let me know if you still have any question.

like image 29
Dhaval Avatar answered Jan 11 '23 19:01

Dhaval