-
Notifications
You must be signed in to change notification settings - Fork 2
123 lines (109 loc) · 4.22 KB
/
release.yml
File metadata and controls
123 lines (109 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
name: Publish GitHub Release
on:
push:
branches:
- "master"
tags:
- "v*"
paths:
- "CHANGELOG.md"
- "metadata.yaml"
- ".github/workflows/release.yml"
workflow_dispatch:
inputs:
tag:
description: "可选版本 tag;为空时读取 CHANGELOG 顶部版本"
required: false
type: string
permissions:
contents: write
jobs:
publish-release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Resolve release metadata from CHANGELOG
id: notes
shell: python
env:
INPUT_TAG: ${{ github.event.inputs.tag }}
GITHUB_REF_TYPE: ${{ github.ref_type }}
GITHUB_REF_NAME: ${{ github.ref_name }}
run: |
import os
import re
from pathlib import Path
changelog = Path("CHANGELOG.md").read_text(encoding="utf-8")
release_section_pattern = re.compile(
r"^## \[(?P<tag>v[^\]]+)\] - (?P<date>[^\n]+)\n(?P<body>.*?)(?=^## \[|\Z)",
re.MULTILINE | re.DOTALL,
)
requested_tag = (os.getenv("INPUT_TAG") or "").strip()
if requested_tag:
match = next(
(section for section in release_section_pattern.finditer(changelog) if section.group("tag") == requested_tag),
None,
)
if not match:
raise SystemExit(f"Could not find changelog section for {requested_tag}.")
else:
ref_type = (os.getenv("GITHUB_REF_TYPE") or "").strip()
ref_name = (os.getenv("GITHUB_REF_NAME") or "").strip()
if ref_type == "tag" and ref_name.startswith("v"):
requested_tag = ref_name
match = next(
(section for section in release_section_pattern.finditer(changelog) if section.group("tag") == requested_tag),
None,
)
if not match:
raise SystemExit(f"Could not find changelog section for {requested_tag}.")
else:
match = release_section_pattern.search(changelog)
if not match:
raise SystemExit("Could not find any changelog section.")
requested_tag = match.group("tag")
release_body = match.group("body").strip()
release_name = requested_tag
is_prerelease = "true" if "-beta" in requested_tag.lower() else "false"
heading_map = {
"Added": "新增",
"Changed": "优化",
"Fixed": "修复",
"Removed": "移除",
"Security": "安全",
}
section_pattern = re.compile(
r"^###\s+(?P<title>[^\n]+)\n(?P<body>.*?)(?=^###\s+|\Z)",
re.MULTILINE | re.DOTALL,
)
formatted_sections: list[str] = []
for section in section_pattern.finditer(release_body):
title = section.group("title").strip()
body = section.group("body").strip()
if not body:
continue
localized_title = heading_map.get(title, title)
formatted_sections.append(f"### {localized_title}\n\n{body}")
if formatted_sections:
release_body = "\n\n".join(formatted_sections).strip()
output_path = Path(os.environ["GITHUB_OUTPUT"])
with output_path.open("a", encoding="utf-8") as fh:
fh.write(f"tag={requested_tag}\n")
fh.write(f"name={release_name}\n")
fh.write(f"prerelease={is_prerelease}\n")
fh.write("body<<__BODY__\n")
fh.write(release_body + "\n")
fh.write("__BODY__\n")
- name: Create or update GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.notes.outputs.tag }}
target_commitish: ${{ github.sha }}
name: ${{ steps.notes.outputs.name }}
body: ${{ steps.notes.outputs.body }}
generate_release_notes: false
draft: false
prerelease: ${{ steps.notes.outputs.prerelease }}