Reputation: 1713
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?
Upvotes: 4
Views: 10236
Reputation: 695
It may be not exactly what requested but was quite efficient in my situation. If docker is used to build and deploy web apps, consider adding this step to your CI build script before docker build
command (bash):
echo ENV BUILD_NUMBER=$(Build.BuildNumber) >> Dockerfile
In the app code just use BUILD_NUMBER
environment variable.
The advantage of this method is that build number value becomes metadata of the docker image just like labels but you can also access it from the inside your app.
Upvotes: 0
Reputation: 113
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:
az webapp config appsettings set -n iCoreTestApi -g ArchitectsSandbox -s Dev --settings BUILD_NUMBER=$(Build.BuildNumber)
Command explanation:
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.
Upvotes: 3
Reputation: 33708
You can track it with build number.
Steps to display build number to your website:
{ "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}#" } }
Upvotes: 6
Reputation: 10889
Just as an alternative option, you could read the time that the assembly was created and display it in a version format. Every time the assembly is rebuilt, this value would change to the time it was created.
(adapted from this answer for .Net Core)
public static class AppInfo
{
private static Lazy<string> buildVersion =
new Lazy<string>(() => GetBuildVersion(Assembly.GetEntryAssembly()));
public static string BuildVersion { get; } = buildVersion.Value;
private static string GetBuildVersion(Assembly assembly)
{
var filePath = assembly.Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;
var buffer = new byte[2048];
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
stream.Read(buffer, 0, 2048);
var offset = BitConverter.ToInt32(buffer, c_PeHeaderOffset);
var secondsSince1970 = BitConverter.ToInt32(buffer, offset + c_LinkerTimestampOffset);
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var linkTimeUtc = epoch.AddSeconds(secondsSince1970);
var localTime = TimeZoneInfo.ConvertTime(linkTimeUtc, TimeZoneInfo.Local);
var minutesFromMidnight = localTime.Minute + localTime.Hour * 60;
return localTime.ToString("yyyy.M.dd.") + minutesFromMidnight;
}
}
Then just reference it in Razor as:
@AppInfo.BuildVersion
Upvotes: 1
Reputation: 71
Do you mean something like this:
In project.json:
{
"title": "Your Application name",
"version": "2016.9.20.4002",
"copyright": "Your Company 2016",
"description": "Awesome ASP.Net Core Application",
"dependencies": {
//rest of project.json
You can then create a property in your view model or model such as:
public static string Version
{
get
{
var assembly = Assembly.GetExecutingAssembly();
var fileVersion = GetCustomAttribute<AssemblyFileVersionAttribute>(assembly);
return fileVersion?.Version;
}
}
In your view:
@model Namespace.CustomViewModel
<!--Other HTML Code-->
<span id="applicationVersion">@CustomViewModel.Version</span>
Upvotes: 2
Reputation: 49789
Looks like ApplicationEnvironment class is what you need:
var appEnv = new Microsoft.Extensions.PlatformAbstractions.ApplicationEnvironment();
string version = appEnv.ApplicationVersion;
Also
How can I auto-increment an MVC 6 version number? may be also interesting to you, but keep in mind, that IApplicationEnvironment
has been removed.
Upvotes: 1