How to append nightly build date to VersionSuffix for a .NET Core 3.1 Visual Studio 2019 project using GitHub Actions?

Viewed 551

I want to define my software's version number manually by hand within the source code. At least the major, minor, and patch version components (terminology taken from Semantic Versioning 2.0.0). However, for the build metadata, and .NET's assembly revision version component, I want them to be automatically overwritten by the build server. For example, in a development environment (developers desk), the build metadata, and assembly revision version component, is always empty resp. 0 (e.g. SemVer "1.2.3", Assembly "1.2.3.0"). When the developer checks-in the code changes, the build server replaces the build metadata, and assembly revision version component, with the format yyMMdd (nightly build). Using the already given example, the result would be like SemVer "1.2.3+200721", and Assembly "1.2.3.200721".

The software I am developing is a .NET Core 3.1 C# application, written with Visual Studio 2019. Therefore, I am using the dotnet CLI with a single solution file, referencing several C# projects (one primary project, and several supporting libraries). For the final build/publish (only the one primary project is build), I want to overwrite the build metadata, and assembly revision version component, with the current date for automatic nightly releases as described above.

The build server is GitHub Actions. This is my workflow script:

name: "Solution.sln (main)"

# Controls when the action will run. Triggers the workflow on push or
# pull request events but only for the master branch.
on:
  push:
    branches: [ "develop" ]
  pull_request:
    branches: [ "develop" ]

# A workflow run is made up of one or more jobs that can run sequentially or in
# parallel.
jobs:
  ubuntu-build:
    name: Ubuntu 20.04 build
    runs-on: ubuntu-20.04
    env:
      DOTNET_NOLOGO: true
      DOTNET_CLI_TELEMETRY_OPTOUT: true
      DOTNET_MULTILEVEL_LOOKUP: true
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2
        with:
          clean: true
          fetch-depth: 1
          lfs: true
          submodules: true
      - name: Setup dotnet core cli
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: "3.1.x"
      - name: Restores NuGet packages
        run: dotnet restore Solution.sln
      - name: Builds solution assemblies
        run: dotnet build Solution.sln --configuration Release --no-restore
      - name: Tests solution assemblies
        run: dotnet test Solution.sln --configuration Release --no-build --no-restore
      - name: Publishes application
        run: dotnet publish MainApplication/MainApplication.csproj --configuration Release --no-restore --output publish/ -p:PublishReadyToRun=false -p:PublishSingleFile=false -p:PublishTrimmed=true --self-contained false -p:Version

I have the following within my *.csproj files:

  <PropertyGroup>
    <VersionPrefix>1.2.3</VersionPrefix>
    <VersionSuffix></VersionSuffix>
    <Version>$(VersionPrefix)$(VersionSuffix)</Version>
    <AssemblyVersion>$(VersionPrefix).0</AssemblyVersion>
    <FileVersion>$(AssemblyVersion)</FileVersion>
    <InformationalVersion>$(Version)</InformationalVersion>
    <PackageVersion>$(Version)</PackageVersion>
  </PropertyGroup>

Be aware, that VersionSuffix may already contain information, e.g. alpha.1 (pre-release version notation). Such information must not be overwritten, and have to persists in the final assembly. You probably noticed, that I have overwritten the default Version concatenation mechanics, this is because a stable release (VersionSuffix would be empty) version would result into a build server version like "1.2.3-+200721", which is an invalid SemVer, as "-" is the marker for the pre-release version, but there is no pre-release version content, so I have overwritten it, and have to put the "-" for pre-releases manually in VersionSuffix.

I am aware, that I can overwrite CSPROJ properties via the dotnet CLI using command line arguments like -p:VersionSuffix=+200721, -p:AssemblyVersion=1.2.3.200721.

However, the issue with this solution (exactly as described) is that the build server does not know about the already assigned version within csproj file.

How do I extract VersionPrefix, and VersionSuffix within the GitHub Actions environment, to use them as (environment) variables in the GitHub Actions script's job steps?

To summarize what I want to do:

  • extract VersionSuffix (, and VersionSuffix if VersionSuffix cannot be appended but only replaced),
  • (via dotnet CLI) append (not replace) to VersionSuffix the nightly build date (format +yyMMdd),
  • (via dotnet CLI) replace AssemblyVersion with "$(VersionSuffix).yyMMdd".
0 Answers
Related