Skip to content

Commit 314082d

Browse files
committed
ci: 添加自动打 tag 和发布版本的工作流
- 新增 release.yml:Push 到 main 时自动分析 commits、计算版本号、创建 tag 和 GitHub Release - 优化 publish-nuget.yml:升级 actions 版本、支持多 .NET 版本、优化构建输出 - 新增 cliff.toml:git-cliff 配置,用于生成 CHANGELOG - 新增 CHANGELOG.md:变更日志文件
1 parent f233e64 commit 314082d

4 files changed

Lines changed: 155 additions & 4 deletions

File tree

.github/workflows/publish-nuget.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@ name: Publish NuGet
33
on:
44
push:
55
tags:
6-
- "*"
6+
- "[0-9]+.[0-9]+.[0-9]+"
7+
8+
permissions:
9+
contents: read
710

811
jobs:
912
build:
1013
runs-on: ubuntu-latest
1114

1215
steps:
13-
- uses: actions/checkout@v2
16+
- name: Checkout
17+
uses: actions/checkout@v4
1418

1519
- name: Setup .NET
1620
uses: actions/setup-dotnet@v4
1721
with:
18-
dotnet-version: '10.0.x'
22+
dotnet-version: |
23+
8.0.x
24+
9.0.x
25+
10.0.x
1926
2027
- name: Install dotnet tool
2128
run: dotnet tool install -g dotnetCampus.TagToVersion
@@ -29,5 +36,8 @@ jobs:
2936
- name: Build
3037
run: dotnet build --configuration Release --no-restore
3138

39+
- name: Pack
40+
run: dotnet pack --configuration Release --no-build --output ./artifacts
41+
3242
- name: Publish To Nuget
33-
run: dotnet nuget push ./bin/app/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
43+
run: dotnet nuget push ./artifacts/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate

.github/workflows/release.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
release:
12+
runs-on: ubuntu-latest
13+
if: "!startsWith(github.event.head_commit.message, 'chore(release)')"
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
token: ${{ secrets.GITHUB_TOKEN }}
20+
21+
- name: Setup .NET
22+
uses: actions/setup-dotnet@v4
23+
with:
24+
dotnet-version: |
25+
8.0.x
26+
9.0.x
27+
10.0.x
28+
29+
- name: Get latest tag
30+
id: tag
31+
run: |
32+
latest=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0")
33+
echo "tag=$latest" >> $GITHUB_OUTPUT
34+
35+
- name: Analyze commits and calculate version
36+
id: version
37+
uses: paulhatch/semantic-version@v5.4.0
38+
with:
39+
tag_prefix: ""
40+
major_pattern: "BREAKING CHANGE:"
41+
minor_pattern: "feat:"
42+
format: "${major}.${minor}.${patch}"
43+
44+
- name: Check if release needed
45+
id: check
46+
run: |
47+
if [ "${{ steps.version.outputs.version }}" != "${{ steps.tag.outputs.tag }}" ]; then
48+
echo "need_release=true" >> $GITHUB_OUTPUT
49+
else
50+
echo "need_release=false" >> $GITHUB_OUTPUT
51+
fi
52+
53+
- name: Update Version.props
54+
if: steps.check.outputs.need_release == 'true'
55+
run: |
56+
sed -i "s|<Version>.*</Version>|<Version>${{ steps.version.outputs.version }}</Version>|" Version.props
57+
58+
- name: Generate CHANGELOG
59+
if: steps.check.outputs.need_release == 'true'
60+
uses: orhun/git-cliff-action@v3
61+
with:
62+
config: cliff.toml
63+
args: --tag ${{ steps.version.outputs.version }} --strip header
64+
env:
65+
OUTPUT: CHANGELOG.md
66+
67+
- name: Commit changes
68+
if: steps.check.outputs.need_release == 'true'
69+
run: |
70+
git config user.name "github-actions[bot]"
71+
git config user.email "github-actions[bot]@users.noreply.github.com"
72+
git add Version.props CHANGELOG.md
73+
git commit -m "chore(release): ${{ steps.version.outputs.version }}"
74+
75+
- name: Create tag
76+
if: steps.check.outputs.need_release == 'true'
77+
run: |
78+
git tag ${{ steps.version.outputs.version }}
79+
80+
- name: Create GitHub Release
81+
if: steps.check.outputs.need_release == 'true'
82+
uses: softprops/action-gh-release@v2
83+
with:
84+
tag_name: ${{ steps.version.outputs.version }}
85+
body_path: CHANGELOG.md
86+
env:
87+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
88+
89+
- name: Push changes
90+
if: steps.check.outputs.need_release == 'true'
91+
run: |
92+
git push origin main
93+
git push origin ${{ steps.version.outputs.version }}

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [1.0.1] - 2025-01-02
9+
10+
### Features
11+
12+
- Initial release with core foundation libraries

cliff.toml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[changelog]
2+
header = """# Changelog\n\n"""
3+
body = """
4+
{% if version %}\
5+
## [{{ version }}] - {{ timestamp | date(format="%Y-%m-%d") }}
6+
{% else %}\
7+
## [unreleased]
8+
{% endif %}\
9+
{% for group, commits in commits | group_by(attribute="group") %}
10+
### {{ group | upper_first }}
11+
{% for commit in commits %}
12+
- {{ commit.message | upper_first }}\
13+
{% endfor %}
14+
{% endfor %}\n
15+
"""
16+
trim = true
17+
18+
[git]
19+
conventional_commits = true
20+
filter_unconventional = true
21+
split_commits = false
22+
commit_preprocessors = []
23+
commit_parsers = [
24+
{ message = "^feat", group = "Features" },
25+
{ message = "^fix", group = "Bug Fixes" },
26+
{ message = "^doc", group = "Documentation" },
27+
{ message = "^perf", group = "Performance" },
28+
{ message = "^refactor", group = "Refactor" },
29+
{ message = "^style", group = "Styling" },
30+
{ message = "^test", group = "Testing" },
31+
{ message = "^chore\\(release\\)", skip = true },
32+
{ message = "^chore", group = "Miscellaneous Tasks" },
33+
]
34+
protect_breaking_commits = false
35+
filter_commits = false
36+
tag_pattern = "[0-9].*"

0 commit comments

Comments
 (0)