是否可以使用语义发布来自动化 .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
- 在根目录下创建.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 文件中将键
version
设置为 0.0.0-development
,将键 private
设置为 true
并添加存储库 url
- 在 workflows 目录[=58]中创建一个 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 版本。
但是如您所知,semantic-release 不会发布正确的包版本,因为它还不知道程序集版本。它从 package.json 文件中获取版本。
我想达到的目标:
- 在解决方案中对库或应用程序进行更改时,它应该会根据常规提交自动增加它们的程序集版本。但显然只是修改了该项目。
- 当运行发布过程semantic-release应该用
"name": "my-project-${assembly-version}.zip"
发布项目
有什么可以用的吗?
您可以在 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
步骤。
我有一个 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
- 在根目录下创建.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 文件中将键
version
设置为0.0.0-development
,将键private
设置为true
并添加存储库 url - 在 workflows 目录[=58]中创建一个 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 版本。
但是如您所知,semantic-release 不会发布正确的包版本,因为它还不知道程序集版本。它从 package.json 文件中获取版本。
我想达到的目标:
- 在解决方案中对库或应用程序进行更改时,它应该会根据常规提交自动增加它们的程序集版本。但显然只是修改了该项目。
- 当运行发布过程semantic-release应该用
"name": "my-project-${assembly-version}.zip"
发布项目
有什么可以用的吗?
您可以在 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
步骤。