mirror of
https://github.com/linyqh/NarratoAI.git
synced 2025-12-10 18:02:51 +00:00
新增自动发布 cicd
This commit is contained in:
parent
0a3e497add
commit
261e1d8c71
124
.github/workflows/auto-release-generator.yml
vendored
Normal file
124
.github/workflows/auto-release-generator.yml
vendored
Normal file
@ -0,0 +1,124 @@
|
||||
name: Auto Release Generator
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- '**/project_version' # 假设版本号存储在这个文件中,根据实际情况调整
|
||||
|
||||
jobs:
|
||||
check-version-and-release:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0 # 获取完整历史以检查变更
|
||||
|
||||
- name: Check if version changed
|
||||
id: check-version
|
||||
run: |
|
||||
git fetch origin main
|
||||
|
||||
# 获取当前版本号
|
||||
CURRENT_VERSION=$(cat project_version)
|
||||
echo "Current version: $CURRENT_VERSION"
|
||||
|
||||
# 获取上一个版本号(如果存在)
|
||||
if git show origin/main:project_version &>/dev/null; then
|
||||
PREVIOUS_VERSION=$(git show origin/main:project_version)
|
||||
echo "Previous version: $PREVIOUS_VERSION"
|
||||
|
||||
if [[ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]]; then
|
||||
echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
|
||||
echo "version_changed=true" >> $GITHUB_OUTPUT
|
||||
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "Version unchanged"
|
||||
echo "version_changed=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
else
|
||||
echo "First version detected"
|
||||
echo "version_changed=true" >> $GITHUB_OUTPUT
|
||||
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: Set up Python
|
||||
if: steps.check-version.outputs.version_changed == 'true'
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.10'
|
||||
|
||||
- name: Install OpenAI SDK
|
||||
if: steps.check-version.outputs.version_changed == 'true'
|
||||
run: pip install openai
|
||||
|
||||
- name: Get commits since last release
|
||||
if: steps.check-version.outputs.version_changed == 'true'
|
||||
id: get-commits
|
||||
run: |
|
||||
# 获取上次发布以来的所有提交信息
|
||||
COMMITS=$(git log $(git describe --tags --abbrev=0 2>/dev/null || echo HEAD~50)..HEAD --pretty=format:"%s")
|
||||
echo "commits<<EOF" >> $GITHUB_OUTPUT
|
||||
echo "$COMMITS" >> $GITHUB_OUTPUT
|
||||
echo "EOF" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Generate release notes with AI
|
||||
if: steps.check-version.outputs.version_changed == 'true'
|
||||
id: generate-notes
|
||||
env:
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
OPENAI_BASE_URL: https://api.siliconflow.cn/v1
|
||||
run: |
|
||||
cat > generate_release_notes.py << 'EOF'
|
||||
import os
|
||||
import sys
|
||||
import openai
|
||||
|
||||
# 设置OpenAI API参数
|
||||
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
||||
openai.base_url = os.environ.get("OPENAI_BASE_URL")
|
||||
|
||||
# 获取提交信息和版本号
|
||||
commits = sys.stdin.read()
|
||||
version = os.environ.get("CURRENT_VERSION")
|
||||
|
||||
# 调用API生成发布说明
|
||||
response = openai.chat.completions.create(
|
||||
model="deepseek-ai/DeepSeek-V3",
|
||||
messages=[
|
||||
{"role": "system", "content": "你是一个专业的软件发布说明生成助手。请根据提供的git提交信息,生成一个结构化的发布说明,包括新功能、改进、修复的bug等类别。使用中文回复。"},
|
||||
{"role": "user", "content": f"请根据以下git提交信息,生成一个版本{version}的发布说明:\n\n{commits}"}
|
||||
],
|
||||
temperature=0.7,
|
||||
)
|
||||
|
||||
# 输出生成的发布说明
|
||||
release_notes = response.choices[0].message.content
|
||||
print(release_notes)
|
||||
|
||||
# 保存到GitHub输出
|
||||
with open(os.environ.get("GITHUB_OUTPUT"), "a") as f:
|
||||
f.write("release_notes<<RELEASE_NOTES_EOF\n")
|
||||
f.write(release_notes)
|
||||
f.write("\nRELEASE_NOTES_EOF\n")
|
||||
EOF
|
||||
|
||||
python generate_release_notes.py < <(echo "${{ steps.get-commits.outputs.commits }}")
|
||||
env:
|
||||
CURRENT_VERSION: ${{ steps.check-version.outputs.current_version }}
|
||||
|
||||
- name: Create GitHub Release
|
||||
if: steps.check-version.outputs.version_changed == 'true'
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
tag_name: v${{ steps.check-version.outputs.current_version }}
|
||||
name: Release v${{ steps.check-version.outputs.current_version }}
|
||||
body: ${{ steps.generate-notes.outputs.release_notes }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
97
.github/workflows/discord-release-notification.yml
vendored
Normal file
97
.github/workflows/discord-release-notification.yml
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
name: Discord Release Notification
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
notify-discord:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.10'
|
||||
|
||||
- name: Install dependencies
|
||||
run: pip install openai discord-webhook
|
||||
|
||||
- name: Enhance release notes and send to Discord
|
||||
env:
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
OPENAI_BASE_URL: https://api.siliconflow.cn/v1
|
||||
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
|
||||
run: |
|
||||
cat > send_discord_notification.py << 'EOF'
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import openai
|
||||
from datetime import datetime
|
||||
from discord_webhook import DiscordWebhook, DiscordEmbed
|
||||
|
||||
# 设置OpenAI API参数
|
||||
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
||||
openai.base_url = os.environ.get("OPENAI_BASE_URL")
|
||||
|
||||
# 获取GitHub release信息
|
||||
release_info = json.loads(os.environ.get("RELEASE_INFO"))
|
||||
release_notes = release_info["body"]
|
||||
version = release_info["tag_name"]
|
||||
release_date = datetime.strptime(release_info["published_at"], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y年%m月%d日")
|
||||
|
||||
# 使用大模型润色发布说明
|
||||
response = openai.chat.completions.create(
|
||||
model="deepseek-ai/DeepSeek-V3",
|
||||
messages=[
|
||||
{"role": "system", "content": "你是一个专业的软件发布公告优化助手。请优化以下发布说明,使其更加生动、专业,并明确区分新功能、优化内容、修复内容和移除内容等类别。保持原有信息的完整性,同时增强可读性和专业性。使用中文回复。"},
|
||||
{"role": "user", "content": f"请优化以下版本{version}的发布说明,使其更适合在Discord社区发布:\n\n{release_notes}"}
|
||||
],
|
||||
temperature=0.7,
|
||||
)
|
||||
|
||||
enhanced_notes = response.choices[0].message.content
|
||||
|
||||
# 创建Discord消息
|
||||
webhook = DiscordWebhook(url=os.environ.get("DISCORD_WEBHOOK_URL"))
|
||||
|
||||
# 创建嵌入式消息
|
||||
embed = DiscordEmbed(
|
||||
title=f"🚀 NarratoAI {version} 发布公告",
|
||||
description=f"发布日期: {release_date}",
|
||||
color="5865F2" # Discord蓝色
|
||||
)
|
||||
|
||||
# 添加润色后的发布说明
|
||||
embed.add_embed_field(name="更新内容", value=enhanced_notes[:1024] if len(enhanced_notes) > 1024 else enhanced_notes)
|
||||
|
||||
# 如果内容太长,分段添加
|
||||
if len(enhanced_notes) > 1024:
|
||||
remaining = enhanced_notes[1024:]
|
||||
chunks = [remaining[i:i+1024] for i in range(0, len(remaining), 1024)]
|
||||
for i, chunk in enumerate(chunks):
|
||||
embed.add_embed_field(name=f"更新内容(续{i+1})", value=chunk)
|
||||
|
||||
# 添加下载链接
|
||||
embed.add_embed_field(name="下载链接", value=release_info["html_url"])
|
||||
|
||||
# 设置页脚
|
||||
embed.set_footer(text="NarratoAI 团队")
|
||||
embed.set_timestamp()
|
||||
|
||||
# 添加嵌入式消息到webhook
|
||||
webhook.add_embed(embed)
|
||||
|
||||
# 发送消息
|
||||
response = webhook.execute()
|
||||
print(f"Discord notification sent with status code: {response.status_code}")
|
||||
EOF
|
||||
|
||||
# 获取发布信息并传递给脚本
|
||||
echo "RELEASE_INFO='$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" https://api.github.com/repos/${{ github.repository }}/releases/latest)'" >> $GITHUB_ENV
|
||||
|
||||
# 执行脚本
|
||||
python send_discord_notification.py
|
||||
1
project_version
Normal file
1
project_version
Normal file
@ -0,0 +1 @@
|
||||
0.6.2
|
||||
Loading…
x
Reference in New Issue
Block a user