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?
You can track it with build number.
- Go to your VSTS site and create build definition
- Select General tab, specify build number format, for example: $(date:yyyyMMdd)$(rev:.r)
- (Optional) Select Triggers tab, check Continuous integration (CI) and configure filters if you want queue build for each check-in.
- 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:
Steps to display build number to your website:
- Install Replace Tokens extension to you VSTS
- Edit your build definition to add Replace token task
- Specify target files and root directory (for asp.net core app, you can specify **\appsettings.json)
- Select Variable tab and add a new variable. Save your build definition
- 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}#"
}
}
- Add logical to your asp.net project to read appsettings.json to get specific value and display in the page.
- Check in your code and queue build.
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:
- Add setting BUILD_NUMBER with initial value of 1.0 in appsettings.json
- 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.
- 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.
- In VSTS, In your Release definition, add a task Azure CLI.
- 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.