Skip to content

Instantly share code, notes, and snippets.

@PadreSVK
Last active December 22, 2020 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PadreSVK/e45804305ddbe1da8221f774aefe4b14 to your computer and use it in GitHub Desktop.
Save PadreSVK/e45804305ddbe1da8221f774aefe4b14 to your computer and use it in GitHub Desktop.
Combo of props + targets that add to dlls and nuget git metadata and source link support for pdb

Combo of files that add git metadata to your dlls and nugets

  1. Put Directory.Build.props and Directory.Build.targets to root of your repository (or to where exist .sln file). These files are automatically added by msbuild to all projects (csprojs) that are in file system in folders under .targets and .props files.
  2. On your CI set required GitBranch msbuild property. This is required because most of CI do not checkout to branch, but to commit so you need explicitly add this to msbuild (dotnet cli). It is added to nuget metadata.
    1. [optional] (but recommended) - BuildNumber is used for build number for prerelease packages (it is used . instead of + because nuget do not support it). If is not filled, is used git SHA (with this is not working proper prerelease sorting in git) (i.e. 20201222.12 will be `1.0.0-prereleasefield..20201222.12)
    2. [optional] - PreReleaseField is used for specifying prerelease field. If is not filled it is created from git branch without /

This infrastructure provide

  • Add Git metadata to nugets (repository url, git branch, git commit)
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>TestClassLib</id>
    <version>1.0.0-featurenewitem.20201222.11</version>
    <authors>Patrik</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <repository type="git" url="https://dev.azure.com/mysuperorganization/GitMetadataInfrastructure/_git/GitMetadataInfrastructure" branch="feature/newitem" commit="4d21db7d9389f9d94d03b820ae4f5a9970390249" />
    <dependencies>
      <group targetFramework=".NETStandard2.0" />
    </dependencies>
  </metadata>
</package>
  • Add git metadata to all created dlls (set InformationalVersion)
  • If is dll/nuget created with some nonstandard way (from dirty repo) add dirty field to dll InformationalVersion and nuget version.
  • Add Sourcelink support for packages
  • Add pdb to nugets (It is not best practice , but ADO do not support snupkg 😢 see docs)
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
trigger:
- 'main'
pool:
vmImage: 'ubuntu-latest'
variables:
solution: '**/*.sln' # path to your solution file
projectToPackaging: 'TestClassLib/TestClassLib.csproj' #path to csproj for pack
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
gitBranch: ${{replace(variables['Build.SourceBranch'],'refs/heads/','') }} # bare name of branch (without refs/heads/)
steps:
- task: NuGetToolInstaller@1
displayName: "Install nuget tools required for login to internal feed"
- task: DotNetCoreCLI@2
displayName: "Restore nugets for solution"
inputs:
command: 'restore'
projects: '$(solution)'
- task: DotNetCoreCLI@2
displayName: "Build solution"
inputs:
command: 'build'
projects: '$(solution)'
- task: DotNetCoreCLI@2
displayName: "Pack TestClassLib"
inputs:
command: 'pack'
packagesToPack: $(projectToPackaging)
versioningScheme: 'off'
buildProperties: 'BuildNumber=$(Build.BuildNumber);GitBranch=$(gitBranch)'
- task: DotNetCoreCLI@2
displayName: "Push nuget to internal feed"
inputs:
command: 'push'
packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg'
nuGetFeedType: 'internal'
publishVstsFeed: '00000000-0000-0000-0000-000000000000/00000000-0000-0000-0000-000000000000'
<Project>
<PropertyGroup>
<StableVersionBranch>master</StableVersionBranch>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GitInfo" Version="2.1.2" PrivateAssets ="All">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.AzureRepos.Git" Version="1.0.0" PrivateAssets="All"/>
</ItemGroup>
</Project>
<Project>
<!-- https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.targets#L544 -->
<Target Name="AddGitMetadata" AfterTargets="GitVersion">
<PropertyGroup>
<!--https://docs.microsoft.com/en-us/dotnet/core/tools/csproj-->
<RepositoryCommit>$(GitSha)</RepositoryCommit>
<!-- most CI do checkout to bare commit not branch, in this case set p:GitBranch while inkoking msbuild/dotnet cli -->
<RepositoryBranch>$(GitBranch)</RepositoryBranch>
<BuildNumber Condition="$(BuildNumber) == ''">$(GitCommit)</BuildNumber>
<InformationalVersion>$(Version)</InformationalVersion>
</PropertyGroup>
<!-- pre-release versions of nugets-->
<PropertyGroup Condition="'$(GitBranch)' != '$(StableVersionBranch)'">
<!-- create prerelease field from git brachn if is not specified -->
<PreReleaseField Condition="'$(PreReleaseField)' == ''">$(GitBranch.Replace("/", ""))</PreReleaseField>
<PackageVersion>$(Version)-$(PreReleaseField)</PackageVersion>
<!-- if is package/dll builded from dirty repo add reprerelease field dirty -->
<PackageVersion Condition="'$(GitIsDirty)' == '1'">$(PackageVersion).dirty</PackageVersion>
<InformationalVersion>$(PackageVersion)</InformationalVersion>
<!-- nuget do not support semversion 2 build "+" suffix, add "." instead -->
<PackageVersion>$(PackageVersion).$(BuildNumber)</PackageVersion>
</PropertyGroup>
<!-- Add suffix prerelease "dirty" field if is builded from dirty StableVersionBranch -->
<PropertyGroup Condition="'$(GitIsDirty)' == '1' AND '$(GitBranch)' == '$(StableVersionBranch)'">
<PackageVersion>$(PackageVersion)-dirty</PackageVersion>
<InformationalVersion>$(InformationalVersion).dirty</InformationalVersion>
</PropertyGroup>
<!-- to InformationalVersion will be append +gitSHA by SourceLink infrastructure -->
<PropertyGroup>
<InformationalVersion>$(InformationalVersion)-sha</InformationalVersion>
</PropertyGroup>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment