-
Notifications
You must be signed in to change notification settings - Fork 7
190 lines (162 loc) · 7.54 KB
/
auto_update_release_notes.yml
File metadata and controls
190 lines (162 loc) · 7.54 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
name: Update release notes from feishu
on:
schedule:
# 每天凌晨 1 点(北京时间)运行
- cron: "0 17 * * *"
# 添加 workflow_dispatch 以允许手动触发
workflow_dispatch:
jobs:
update-release-notes:
runs-on: ubuntu-latest
steps:
# Step 1: 检出仓库代码
- name: Checkout repository
uses: actions/checkout@v6
with:
persist-credentials: false
ref: master
# Step 2: 下载必要的可执行文件 feishu2md、jq、ossutil
# feishu2md 是用于获取飞书文档并转换为 markdown 文档的工具
# jq 是用于处理 json 数据的工具
# ossutil 是用于上传图片到阿里云 oss 服务器的工具
- name: Download binaries
run: |
mkdir -p ./bin
wget -P ./bin ${{ secrets.MERICO_DOCS_ASSETS_ALIYUN_OSS_PATH }}bin/feishu2md
wget -P ./bin ${{ secrets.MERICO_DOCS_ASSETS_ALIYUN_OSS_PATH }}bin/jq
wget -P ./bin ${{ secrets.MERICO_DOCS_ASSETS_ALIYUN_OSS_PATH }}bin/ossutil
# Step 3: 给 feishu2md、jq、ossutil 可执行权限
- name: Make feishu2md and jq and ossutil executable
run: |
chmod +x ./bin/feishu2md
chmod +x ./bin/jq
chmod +x ./bin/ossutil
# Step 4: 配置 feishu2md
- name: Configure feishu2md
run: |
# 查看 feishu2md 版本
./bin/feishu2md --version
# 生成 feishu2md 配置文件
./bin/feishu2md config --appId ${{ secrets.FEISHU_APP_ID }} --appSecret ${{ secrets.FEISHU_APP_SECRET }}
# 设置 feishu2md 配置文件中的 title_as_filename 为 true
FEISHU2MD_CONF_FILE="$HOME/.config/feishu2md/config.json"
./bin/jq '.output.title_as_filename = true' "$FEISHU2MD_CONF_FILE" > tmp.$$.json && mv tmp.$$.json "$FEISHU2MD_CONF_FILE"
# 查看最新的配置文件内容
cat "$FEISHU2MD_CONF_FILE"
# Step 5: 运行 feishu2md 获取飞书文档(含文档中的图片)并转换为 markdown 文档
- name: Run feishu2md to fetch feishu docs
run: |
mkdir -p ./feishu2md_output
cd feishu2md_output
../bin/feishu2md download --batch ${{ secrets.FEISHU_RELEASE_NOTES_FOLDER_URL }}
ls -al
# 去除 markdown 文档文件名中开头和结尾的空白字符
echo "Trimming markdown file names"
for file in *.md; do
if [[ -f "$file" ]]; then
new_name=$(echo "$file" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+\.md$/.md/')
if [[ "$file" != "$new_name" ]]; then
# 重命名,如果有重名文件,那就直接覆盖了
echo "→ Renaming '$file' to '$new_name'"
mv "$file" "$new_name"
fi
fi
done
ls -al
cd ..
# Step 6: 使用仓库内的 ossutil 将刚才获取的图片上传到阿里云 oss 服务器
- name: Upload images in docs to Aliyun OSS
run: |
if [ -d "./feishu2md_output/static/" ]; then
./bin/ossutil config -e ${{ secrets.ALIYUN_OSS_ENDPOINT }} \
-i ${{ secrets.ALIYUN_OSS_ACCESS_KEY_ID }} \
-k ${{ secrets.ALIYUN_OSS_ACCESS_KEY_SECRET }}
./bin/ossutil cp -r ./feishu2md_output/static/ oss://${{ secrets.ALIYUN_OSS_BUCKET_COME_WITH_STATIC_PATH }} --exclude ".*" -u
else
echo "Directory ./feishu2md_output/static/ does not exist. Skipping upload."
fi
# Step 7: 处理 markdown 文件
- name: Process downloaded markdown docs
run: |
NEW_STATIC_PATH="${{ secrets.ALIYUN_OSS_STATIC_PUBLIC_PATH }}"
for file in ./feishu2md_output/*.md; do
if [ -f "$file" ]; then
# 去除文档内容的前两行(feishu 那边的发版文档已经约定好了格式)
sed -i '1,2d' "$file"
# 删除“## 内部信息”行及其后的所有内容(feishu 那边的发版文档已经约定好了格式)
sed -i '/^## 内部信息$/,$d' "$file"
# 替换图片地址为新的阿里云 oss 图片地址
sed -i -E "s|\!\[(.*?)\]\(static/(.*?)\)|\!\[\1\]($NEW_STATIC_PATH\2)|g" "$file"
echo "Updated $file."
else
echo "$file is not a regular file."
fi
done
# Step 8: 使用 feishu2md_output 中的 markdown 文件替换 release_notes 目录中的文件
- name: Replace release_notes with feishu markdown files
run: |
# 检查 release_notes 目录是否存在,存在则清空目录,不存在则创建目录
if [ -d "./release_notes/" ]; then
rm -rf ./release_notes/*
else
mkdir -p ./release_notes
fi
# 检查 feishu2md_output 目录中是否有 markdown 文件,有则全部复制到 release_notes 目录
if ls ./feishu2md_output/*.md > /dev/null 2>&1; then
cp ./feishu2md_output/*.md ./release_notes/
else
echo "There are no markdown files in feishu2md_output directory."
fi
ls -al ./release_notes
# Step 9: 设置 Python 环境
- name: Set up Python environment
uses: actions/setup-python@v6
with:
python-version: ">=3.10"
# Step 10: 安装 semver 依赖,用于接下来对 release notes 按版本号进行排序
- name: Install semver using pip
run: |
pip install semver
# Step 11: 根据 release_notes 目录中的 markdown 文件列表更新 toc
- name: Update toc with release notes
run: |
python scripts/update_toc_with_release_notes.py
# Step 12: 生成分支名,用于发起 pull request
- name: Generate branch name
run: |
BRANCH_NAME=update-release-notes-with-toc-$(date +%Y%m%d%H%M%S)
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
# Step 13: 就 release_notes 和 toc 的改动发起 pull request
- name: Create pull request
uses: peter-evans/create-pull-request@v8
with:
token: ${{ secrets.REPO_SCOPED_TOKEN }}
commit-message: "Update release notes with markdown docs from feishu"
committer: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
branch: ${{ env.BRANCH_NAME }}
base: master
title: "Auto update release notes from feishu"
body: "This PR contains updated release notes from feishu."
delete-branch: true
add-paths: |
release_notes/*.md
TOC.md
labels: |
release_notes_with_toc
auto_merge
notify-feishu-on-failure:
runs-on: ubuntu-latest
needs: update-release-notes
if: failure()
steps:
- name: Send notification to Feishu on failure
run: |
curl -X POST ${{ secrets.FEISHU_CI_BOT_WEBHOOK_URL }} \
-H 'Content-Type: application/json' \
-d '{
"msg_type": "text",
"content": {
"text": "🚨 GitHub Action 失败通知:从飞书更新 Release Notes 并发起 PR 时失败!\n- 仓库: ${{ github.repository }}\n- 分支: ${{ github.ref }}\n- 工作流: ${{ github.workflow }}\n- 触发人: ${{ github.actor }}\n- 查看日志: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
}
}'