更新 release.yml,优化发布日志生成逻辑,改用 jq 处理输入文件,简化了 payload 生成过程,提高了代码可读性和维护性。 #20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Always Release | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
always-release: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Get latest tag | |
id: get_tag | |
run: | | |
git fetch --tags | |
latest_tag=$(git tag --list 'v*' --sort=-v:refname | head -n 1) | |
if [ -z "$latest_tag" ]; then | |
echo "tag=v1.0.0" >> $GITHUB_OUTPUT | |
else | |
IFS='.' read -r major minor patch <<<"${latest_tag#v}" | |
if [[ -z "$minor" ]]; then minor=0; fi | |
if [[ -z "$patch" ]]; then patch=0; fi | |
if (( patch < 9 )); then | |
patch=$((patch+1)) | |
else | |
patch=0 | |
if (( minor < 9 )); then | |
minor=$((minor+1)) | |
else | |
minor=0 | |
major=$((major+1)) | |
fi | |
fi | |
echo "tag=v$major.$minor.$patch" >> $GITHUB_OUTPUT | |
fi | |
- name: Collect git diff | |
id: diff | |
run: | | |
git log -1 --pretty=format:"%s%n%n%b" > commit_msg.txt | |
if git rev-parse HEAD^ >/dev/null 2>&1; then | |
git diff HEAD^ HEAD > diff.txt | |
else | |
git show HEAD > diff.txt | |
fi | |
- name: Generate release notes with LLM (primary) | |
id: llm1 | |
run: | | |
set -e | |
jq -Rs ' | |
{model:"gpt-4o-mini", | |
messages:[ | |
{"role":"system","content":"You are a helpful assistant."}, | |
{"role":"user","content":"请用简洁中文总结以下代码变更内容,适合用作 GitHub Release 发布日志:\n\n" + input[0] + "\n\n变更详情:\n" + input[1]} | |
] | |
} | |
' commit_msg.txt diff.txt > payload.json | |
response=$(curl -sS -X POST "https://api.chatanywhere.tech/v1/chat/completions" \ | |
-H "Authorization: Bearer sk-aKcwXkHYsIKmQ7ZZuaiItE2iw498L7OX8krxkXI5CZ6Q6r44" \ | |
-H "Content-Type: application/json" \ | |
-d @payload.json) | |
echo "$response" > llm1.json | |
note=$(jq -r '.choices[0].message.content // empty' llm1.json) | |
if [ -z "$note" ]; then | |
echo "llm1_failed=true" >> $GITHUB_ENV | |
else | |
echo "$note" > release_note.txt | |
echo "llm1_failed=false" >> $GITHUB_ENV | |
fi | |
- name: Generate release notes with LLM (fallback) | |
if: env.llm1_failed == 'true' | |
id: llm2 | |
run: | | |
set -e | |
jq -Rs ' | |
{model:"free:Qwen3-30B-A3B", | |
messages:[ | |
{"role":"user","content":"请用简洁中文总结以下代码变更内容,适合用作 GitHub Release 发布日志:\n\n" + input[0] + "\n\n变更详情:\n" + input[1]} | |
] | |
} | |
' commit_msg.txt diff.txt > payload.json | |
response=$(curl -sS -X POST "https://api.suanli.cn/v1/chat/completions" \ | |
-H "Authorization: Bearer sk-W0rpStc95T7JVYVwDYc29IyirjtpPPby6SozFMQr17m8KWeo" \ | |
-H "Content-Type: application/json" \ | |
-d @payload.json) | |
echo "$response" > llm2.json | |
note=$(jq -r '.choices[0].message.content // empty' llm2.json) | |
if [ -z "$note" ]; then | |
note="自动生成发布日志失败,请手动补充。" | |
fi | |
echo "$note" > release_note.txt | |
- name: Create tag and release | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
git config user.name github-actions | |
git config user.email github-actions@github.com | |
git tag ${{ steps.get_tag.outputs.tag }} | |
git push origin ${{ steps.get_tag.outputs.tag }} | |
note="$(cat release_note.txt)" | |
gh release create ${{ steps.get_tag.outputs.tag }} --notes "$note" --title "${{ steps.get_tag.outputs.tag }}" |