是否可以使用语义发布来自动化 .Net 程序集版本控制?

Is it possible to automate .Net assembly versioning using semantic-release?

我有一个 C# 解决方案,其中包含一个项目和多个使用 .Net 6 的库。我正在使用常规提交(commitlint with husky)并想使用 semantic-release根据提交消息将最新版本作为 ZIP 文件部署到 Github。


我为 C# 项目尝试的设置:

.

npm install semantic-release -D
npm install @semantic-release/changelog -D
npm install @semantic-release/npm -D
npm install @semantic-release/github -D
npm install @semantic-release/git -D

.

{
  "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    "@semantic-release/changelog",
    "@semantic-release/npm",
    [
      "@semantic-release/github",
      {
        "assets": [
          {
            "path": "my-project.zip",
            "label": "my-project-${nextRelease.gitTag}.zip",
            "name": "my-project-${nextRelease.gitTag}.zip"
          }
        ]
      }
    ],
    "@semantic-release/git"
  ]
}

.

name: Release on push on main branch
 
on:
  push:
    branches:
      - main
 
jobs:
  release-on-push-on-main-branch:
    runs-on: ubuntu-latest
 
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 16.x

      - name: Install Node dependencies
        run: npm install
 
      - name: Setup .Net
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '6.0.x'
 
      - name: Install .Net dependencies
        run: dotnet restore ./SolutionDir
 
      - name: Run build
        run: dotnet build ./SolutionDir
 
      - name: Run publish
        run: dotnet publish ./SolutionDir
 
      - name: Rename publish directory of MyProject to my-project and move it to root
        run: mv ./SolutionDir/MyProject/bin/Debug/net6.0 ./my-project
 
      - name: ZIP my-project directory
        run: zip -r my-project.zip my-project
 
      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: npx semantic-release --branches main

它似乎有效,每当我推送到主分支时,它都会将最新版本的分发(包含 DLL)部署到 Github 版本。


但是如您所知,semantic-release 不会发布正确的包版本,因为它还不知道程序集版本。它从 package.json 文件中获取版本。

我想达到的目标:

有什么可以用的吗?

您可以在 dotnet publish 之前干 -运行 semantic-release 以获取发布的版本号(使用 @semantic-release/exec 插件)。然后将这个获取的版本号传递给 dotnet publish:

@semantic-release/exec 插件添加到您的 npm install 命令并调整您的 .releaserc.json 文件以存储下一个发布版本(例如在环境变量中):

{
   "plugins": [
     ...,
     [
       '@semantic-release/exec',
       { verifyReleaseCmd: 'echo RELEASE_VERSION=${nextRelease.version} >> $GITHUB_ENV' }
     ]
   ]
 }

dotnet publish前插入干运行,调整dotnet publish步长:

...
- name: Fetch release version
  env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  run: npx semantic-release --branches main --dry-run
- name: Run publish
  run: dotnet publish ./SolutionDir -p:Version=${{ env.RELEASE_VERSION }}
...

注意:您可以简单地使用 dotnet publish 命令的 -o 参数代替 Rename publish directory of MyProject to my-project and move it to root 步骤。