5

我有一个 C# 解决方案,其中包含使用 .Net 6 的单个项目和多个库。我正在使用常规提交(使用 husky 提交),并希望使用语义发布基于提交在 Github 上将最新构建部署为 ZIP 文件消息。


我为 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
  • 在根目录中创建一个.releaserc.json文件

.

{
  "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"
  ]
}
  • package.json文件中将密钥设置version0.0.0-development,将密钥设置privatetrue并添加存储库 url
  • 在工作流目录中创建一个release-on-push-on-main-branch.yml文件

.

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 版本。


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

我想要达到的目标:

  • 在解决方案中对库或应用程序进行更改时,它应该根据常规提交自动增加它们的程序集版本。但显然,如果该项目被修改。
  • 运行发布过程时,semantic-release 应该发布项目"name": "my-project-${assembly-version}.zip"

有什么我可以使用的吗?

4

1 回答 1

2

您可以semantic-release先试运行dotnet publish以获取版本的版本号(使用@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 }}
...

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

于 2021-12-31T12:45:16.307 回答