如何使用 GitHub Actions 将夜间构建日期附加到 .NET Core 3.1 Visual Studio 2019 项目的 VersionSuffix?

bur*_*rsk 6 versioning .net-core dotnet-cli github-actions

我想在源代码中手动定义软件的版本号。至少主要版本、次要版本和补丁版本组件(术语取自Semantic Versioning 2.0.0)。但是,对于构建元数据和.NET 的程序集修订版本组件,我希望它们能够被构建服务器自动覆盖。例如,在开发环境(开发人员桌面)中,构建元数据和程序集修订版本组件始终为空。0(例如 SemVer“1.2.3”,程序集“1.2.3.0”)。当开发人员签入代码更改时,构建服务器会使用格式yyMMdd(夜间构建)替换构建元数据和程序集修订版本组件。使用已经给出的示例,结果将类似于 SemVer“1.2.3+200721”和 Assembly“1.2.3.200721”。

我正在开发的软件是一个.NET Core 3.1 C#应用程序,使用 Visual Studio 2019 编写。因此,我将dotnetCLI 与单个解决方案文件结合使用,引用多个 C# 项目(一个主要项目和多个支持库)。对于最终的构建/发布(仅构建一个主要项目),我想使用如上所述的每晚自动发布的当前日期覆盖构建元数据和程序集修订版本组件。

构建服务器是GitHub Actions。这是我的工作流程脚本:

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
Run Code Online (Sandbox Code Playgroud)

我的 * .csproj文件中有以下内容:

  <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>
Run Code Online (Sandbox Code Playgroud)

意识到VersionSuffix可能已经包含信息,例如alpha.1(预发布版本符号)。此类信息不得被覆盖,并且必须保留在最终装配中。您可能注意到,我已经覆盖了默认的Version串联机制,这是因为稳定版本(VersionSuffix将为空)版本会导致构建服务器版本如“1.2.3-+200721”,这是一个无效的 SemVer,如“ -”是预发布版本的标记,但是没有预发布版本内容,所以我把它覆盖了,必须手动将预发布的“-”放入VersionSuffix.

我知道,我可以dotnet使用命令行参数(如-p:VersionSuffix=+200721, )通过 CLI 覆盖 CSPROJ 属性-p:AssemblyVersion=1.2.3.200721

但是,此解决方案的问题(正如所描述的那样)是构建服务器不知道 csproj 文件中已分配的版本。

我如何提取VersionPrefixVersionSuffix在 GitHub Actions 环境中将它们用作 GitHub Actions 脚本的作业步骤中的(环境)变量?

总结一下我想做的事情:

  • 提取VersionSuffix(,和VersionSuffix如果VersionSuffix不能追加但只能替换),
  • (通过dotnetCLI)附加(而不是替换)到VersionSuffix每晚构建日期(格式+yyMMdd),
  • (通过dotnetCLI)替换AssemblyVersion为“$(VersionSuffix).yyMMdd”。