确保Azure管道内置的dll和nuget程序包包含与csproj版本相同的信息

时间:2020-01-20 01:41:26

标签: azure powershell azure-devops continuous-integration azure-pipelines

我正在用specific question重新发布,因为我知道除了azure-pipelines.yml之外还需要Powershell脚本。

我如何使用azure管道在输出文件中提供以下版本控制(以及每个https://www.nuget.org/packages/MediatR/匹配的nuget软件包信息)来实现相同的输出/格式。

dll properties

我知道这将是一个Powershell脚本,我已经检查了MediatR并看到了https://github.com/jbogard/MediatR/blob/master/Build.ps1,但看不到如何将其应用于天蓝色的管道步骤。

总而言之,应该从我的csproj <VersionPrefix>1.0.1</VersionPrefix>使用verisn前缀(或任何版本),并将其应用于二进制dll和nuget包中的文件版本属性。

我还想将dll的产品版本设置为特定的build-datetime(或在MediatR的情况下使用提交字符串)。

我专门在寻找脚本(powershell和关联的yaml以链接脚本版本字符串)和关联的步骤,感谢一个大问题,但是我在google或Stackorerflow上找不到任何具体答案-指向教程的链接将是完美的

赞赏指针。

1 个答案:

答案 0 :(得分:1)

总之,应该使用verisn前缀 1.0.1(或任何版本)从 我的csproj,并应用于二进制文件的文件版本属性 dll和nuget程序包。

我还想将dll的产品版本设置为 特定的build-datetime(或在 MediatR)。

除了PowerShell指令外,我们可以考虑将msbuild property视为另一个指令。

这是我的test.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <!--Add other properties here.-->
  </PropertyGroup>

  <PropertyGroup> <!--Custom Property Group for CI/CD-->
    <MyVersionPrefix>1.0.1</MyVersionPrefix>
    <MyVersionSuffix>0</MyVersionSuffix>
    <BuildDateTime>0</BuildDateTime>
    <AssemblyVersion>$(MyVersionPrefix).$(MyVersionSuffix)</AssemblyVersion> <!--File Version-->
    <Version>$(MyVersionPrefix).$(BuildDateTime)</Version> <!--Product Version-->
    <PackageVersion>$(MyVersionPrefix).$(MyVersionSuffix)</PackageVersion> <!--Nuget Package Version-->
    <Copyright>Just for test.</Copyright>
  </PropertyGroup>
</Project>

我有一个自定义属性组来定义诸如File Version, Product Version and Package Version.之类的属性,而不是使用<VersionPrefix>1.0.1</VersionPrefix>,而是使用自定义<MyVersionPrefix>1.0.1</MyVersionPrefix>

这可以很好地还原,构建本地开发。如果还需要在本地打包项目,则可以(使用<---->对此属性组添加注释。

要使用CI / CD配置该项目:

我的意见是覆盖xx.csproj中定义的属性。对我来说,我创建了一个Suffix管道变量:

enter image description here

然后将管道内部版本号格式设置为:$(date:yyyyMMdd)$(rev:r)

然后我像这样指定dotnet build taskdotnet pack task

- task: DotNetCoreCLI@2
      displayName: "Build Solution"
      inputs:
        command: build
        projects: '**/*.csproj'
        arguments: '--configuration $(BuildConfiguration) -p:MyVersionSuffix=$(Suffix) -p:BuildDateTime=$(Build.BuildNumber)'

- task: DotNetCoreCLI@2
      displayName: 'dotnet pack'
      inputs:
        command: pack
        nobuild: true
        buildProperties: 'MyVersionSuffix=$(Suffix)'

通过这种方式,我可以将$(Suffix)$(Build.BuildNumber)的值从构建管道传递给msbuild命令。我可以通过管道变量来控制file version, product version, nuget package version。 (关于文件,产品,软件包版本的相应属性,请参见上面的我的项目文件。)

如果设置后缀变量19,则在构建和打包后,我可以获得一个Nuget软件包,其软件包版本为1.0.1.19。其中程序集具有相同的文件版本1.0.1.19和产品版本,例如1.0.1.202001208。如果您不希望增加一个产品版本,则可以使用-p:BuildDateTime=AnyValueYouWant指定要在构建任务中使用的值。

希望它会有所帮助,如果我误解了任何内容,请随时纠正我:)

相关问题