简体   繁体   中英

Ensuring azure pipeline built dll and nuget package contain same info as csproj version

I am reposting with a specific question as i have understand a powershell script is requried in addition to azure-pipelines.yml .

How would i achieve the same output/formatting using azure pipelines to provide the following versioning in an output file (and matching nuget package info per https://www.nuget.org/packages/MediatR/ ).

dll 属性

I understand this would be a powershell script, i have checked on MediatR and see https://github.com/jbogard/MediatR/blob/master/Build.ps1 but can't see how i would apply that to an azure pipeline step.

In summary the the verisn prefix should be used <VersionPrefix>1.0.1</VersionPrefix> (or whatever the version is) from my csproj, and applied to the file version property both in the binary dll and nuget package.

I would also like to set the product version of the dll to the specific build-datetime (or commit string as has been used in case of MediatR).

I am specifically looking for a script (powershell and associated yaml to link the scripted version strings) and associated steps, appreciate a big ask however I cannot find any concrete answers on google or Stackorerflow - a link to a tutorial would be perfect.

Pointers appreciated.

In summary the the verisn prefix should be used 1.0.1 (or whatever the version is) from my csproj, and applied to the file version property both in the binary dll and nuget package.

I would also like to set the product version of the dll to the specific build-datetime (or commit string as has been used in case of MediatR).

Apart from the PowerShell direction, we may consider msbuild property as another direction.

This is my 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>

I have one custom Property Group to define properties like File Version, Product Version and Package Version. Instead of using <VersionPrefix>1.0.1</VersionPrefix> , I use custom <MyVersionPrefix>1.0.1</MyVersionPrefix> .

This can work well to restore,build in local development. If you also need to pack the project locally, you can comment(using <----> ) this Property Group.

To configure this project with CI/CD:

My opinion is to override the properties defined in xx.csproj . For me, I created one Suffix pipeline variable:

在此处输入图片说明

And then set the pipeline build number format to: $(date:yyyyMMdd)$(rev:r) .

Then I specify the dotnet build task and dotnet pack task like this:

- 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)'

In this way, I can pass value of $(Suffix) and $(Build.BuildNumber) from build pipeline to msbuild command. I can control the file version, product version, nuget package version by pipeline variables. (About the corresponding property of the file,product,package versions see my project file above.)

If set the Suffix variable 19 , then after the build and package I can get a Nuget package whose package version is 1.0.1.19 . In which the assembly has same file version 1.0.1.19 and product version like 1.0.1.202001208 . If you don't want one increasing product version, you can then specify the value you want in build task with: -p:BuildDateTime=AnyValueYouWant .

Hope it helps and if I misunderstand anything, feel free to correct me :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM