Continuous integration 在网页上显示生成版本

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; 也 您可能也

我正在使用asp.net core开发一个网站。
我用
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站点并创建生成定义
  • 选择“常规”选项卡,指定生成编号格式,例如:$(日期:yyyyMMdd)$(版本:.r)
  • (可选)如果要为每个签入生成队列,请选择“触发器”选项卡,选中“连续集成”(CI)并配置筛选器
  • 配置其他设置(例如“生成”选项卡中的步骤/任务) 生成完成后,转到该生成定义的摘要(单击生成定义标题超链接转到摘要页面),结果如下:
  • 将内部版本号显示到网站的步骤:

  • 为您的VST安装替换令牌扩展
  • 编辑生成定义以添加替换令牌任务
  • 指定目标文件和根目录(对于asp.net核心应用程序,您可以指定**\appsettings.json)
  • 选择变量选项卡并添加新变量。保存生成定义
  • 编辑asp.net项目的appsettings.json文件。示例代码:
  • 将逻辑添加到asp.net项目以读取appsettings.json以获取特定值并显示在页面中
  • 签入代码并生成队列

  • 经过一天的研究,最终发现/创建了一个比使用市场上任何随机应用程序(替换令牌)更好的选择

    我所说的选项已经在VSTS、Azure CLI任务中可用

    以下是STPE:

  • 在appsettings.json中添加初始值为1.0的设置BUILD_NUMBER
  • 在应用程序中读取appsettings.json并显示它。我相信你们都足够聪明,知道如何使用appsettings在Web应用程序上显示内部版本号
  • 在Azure Portal中,类似地,在应用程序的应用程序服务下的Azure应用程序设置部分中创建名为BUILD_NUMBER的应用程序设置,初始值为1.0
  • 在VSTS中,在发布定义中,添加任务Azure CLI
  • 使用内嵌脚本填充必需的字段,例如Azure订阅、脚本位置,以及使用以下CLI命令填充最后但最重要的内嵌脚本
  • az webapp config appsettings set-n iCoreTestApi-g architectsandbox-s Dev--settings BUILD\u NUMBER=$(BUILD.BuildNumber)

    命令说明:

    • iCoreTestApi应替换为Azure中的真实WebApp或Api名称
    • ArchitectSandbox应替换为Azure中的资源组
    • Dev是插槽名称,您可能有,也可能没有
    • 命令的其余部分保持不变
    一旦您将新版本排队,在成功完成部署后,您可以看到Azure上的应用程序设置部分已更新为新版本号


    如果您还有任何问题,请告诉我。

    可以是相关的,谢谢,但我只需要发布的版本号,不需要构建。而
    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}#"
          }
        }