Continuous integration 在网页上显示生成版本
我正在使用asp.net core开发一个网站。Continuous integration 在网页上显示生成版本,continuous-integration,asp.net-core,azure-devops,Continuous Integration,Asp.net Core,Azure Devops,我正在使用asp.net core开发一个网站。 我用visualstudio或VSTS发布它 我想在网页上显示有关哪个版本的一些信息。 (类似于rev 2016.9.20.4002) 我该怎么做呢?看起来课堂就是你所需要的: var appEnv = new Microsoft.Extensions.PlatformAbstractions.ApplicationEnvironment(); string version = appEnv.ApplicationVersion; 也 您可能也
我用
visualstudio
或VSTS
发布它
我想在网页上显示有关哪个版本的一些信息。(类似于
rev 2016.9.20.4002
)
我该怎么做呢?看起来课堂就是你所需要的:
var appEnv = new Microsoft.Extensions.PlatformAbstractions.ApplicationEnvironment();
string version = appEnv.ApplicationVersion;
也
您可能也感兴趣,但请记住,
iaapplicationenvironment
已被删除。您的意思是:
在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
然后,可以在视图模型或模型中创建特性,例如:
public static string Version
{
get
{
var assembly = Assembly.GetExecutingAssembly();
var fileVersion = GetCustomAttribute<AssemblyFileVersionAttribute>(assembly);
return fileVersion?.Version;
}
}
公共静态字符串版本
{
得到
{
var assembly=assembly.getExecutionGassembly();
var fileVersion=GetCustomAttribute(程序集);
返回fileVersion?.Version;
}
}
在你看来:
@model Namespace.CustomViewModel
<!--Other HTML Code-->
<span id="applicationVersion">@CustomViewModel.Version</span>
@model Namespace.CustomViewModel
@CustomViewModel.Version
作为替代选项,您可以读取程序集创建的时间并以版本格式显示。每次重新生成程序集时,此值都将更改为创建该程序集的时间
(改编自.Net Core)
您可以使用内部版本号跟踪它
经过一天的研究,最终发现/创建了一个比使用市场上任何随机应用程序(替换令牌)更好的选择 我所说的选项已经在VSTS、Azure CLI任务中可用 以下是STPE:
az webapp config appsettings set-n iCoreTestApi-g architectsandbox-s Dev--settings BUILD\u NUMBER=$(BUILD.BuildNumber)
命令说明:
- iCoreTestApi应替换为Azure中的真实WebApp或Api名称
- ArchitectSandbox应替换为Azure中的资源组
- Dev是插槽名称,您可能有,也可能没有
- 命令的其余部分保持不变
如果您还有任何问题,请告诉我。可以是相关的,谢谢,但我只需要发布的版本号,不需要构建。而
app.useruntimeinfo()只适合开发时间。“rev 2016.9.20.4002”是VSTS生成的版本号吗?是的,我只需要一个字符串就可以告诉我代码版本。它可能由VST生成。构建编号或git提交哈希都可以。这是个好主意,但我必须更改版本。我希望它能自动完成。但它怎么能显示在我的网站上呢?@qin我更新了我的答案,您可以参考我的步骤来实现您的要求。看起来很好,我稍后将尝试替换令牌扩展名
。@qin Replace Token extension:@qin您是否成功地使用我的步骤将版本号显示到您的网站?这不会获得生成版本,而是应用程序版本。前者在每次生成时自动递增,后者是静态的,并输入到应用程序属性/程序集信息中。
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;
}
}
@AppInfo.BuildVersion
{
"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}#"
}
}