-
Notifications
You must be signed in to change notification settings - Fork 0
159 lines (139 loc) · 5.79 KB
/
release.yml
File metadata and controls
159 lines (139 loc) · 5.79 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
name: 自动Release更新流程
on:
push:
branches:
- main
tags:
- 'v*.*.*'
pull_request:
branches:
- main
permissions:
# 配置必要的权限
contents: write # 允许创建release、tag和提交更改
pull-requests: write # 允许操作PR
jobs:
# 更新Release作业
update-release:
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 获取提交日志(用于更新内容)
id: get-changelog
run: |
# 获取提交日志作为更新内容
if [ "${{ github.event_name }}" == "pull_request" ]; then
# PR事件:获取PR相关的提交日志
PR_NUMBER=${{ github.event.pull_request.number }}
CHANGELOG=$(git log --pretty=format:"- %s" ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }})
elif [[ "${{ github.ref }}" == refs/tags/* ]]; then
# 标签事件:获取当前标签与上一个标签之间的提交日志
PREVIOUS_TAG=$(git describe --abbrev=0 --tags $(git rev-list --tags --skip=1 --max-count=1))
CHANGELOG=$(git log --pretty=format:"- %s" $PREVIOUS_TAG..${{ github.ref_name }})
else
# 分支推送事件:获取当前分支的最新提交日志
CHANGELOG=$(git log --pretty=format:"- %s" -n 10)
fi
# 保存变更日志到环境变量
echo "CHANGELOG<<EOF" >> $GITHUB_ENV
echo "$CHANGELOG" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
# 获取composer.json中的当前版本号
if [ -f "composer.json" ]; then
CURRENT_VERSION=$(grep -oP '(?<="version": ")[0-9]+\.[0-9]+\.[0-9]+' composer.json)
if [ -n "$CURRENT_VERSION" ]; then
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV
fi
fi
# 为PR创建预发布版本
- name: 为PR创建预发布版本
if: github.event_name == 'pull_request'
uses: softprops/action-gh-release@v1
with:
tag_name: pr-${{ github.event.pull_request.number }}-${{ github.sha }}
name: PR ${{ github.event.pull_request.number }} 预发布
draft: false
prerelease: true
body: |
# PR ${{ github.event.pull_request.number }} 预发布版本
- 基于提交:${{ github.sha }}
- 分支:${{ github.head_ref }}
## 更新内容
${{ env.CHANGELOG }}
# 为main分支创建更新版本
- name: 为main分支创建更新版本
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
uses: softprops/action-gh-release@v1
with:
# 优先使用composer版本号,如果不存在则使用SHA值
tag_name: update-${{ env.CURRENT_VERSION || github.sha }}
name: 更新版本 ${{ env.CURRENT_VERSION || github.sha }}
draft: false
prerelease: true
body: |
# main分支更新版本
- 基于提交:${{ github.sha }}
- 提交者:${{ github.event.head_commit.author.name }}
- 提交时间:${{ github.event.head_commit.timestamp }}
## 更新内容
${{ env.CHANGELOG }}
# 为标签创建正式发布版本
- name: 为标签创建正式发布
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v1
with:
name: 版本 ${{ github.ref_name }}
draft: false
prerelease: false
body: |
# 版本 ${{ github.ref_name }} 发布说明
- 基于标签:${{ github.ref_name }}
- 提交:${{ github.sha }}
- 发布时间:${{ github.event.head_commit.timestamp }}
## 更新内容
${{ env.CHANGELOG }}
# 自动更新版本号作业
bump-version:
needs: update-release
runs-on: ubuntu-latest
# 只在main分支的push事件时运行
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 0
# 使用token以允许提交回仓库
token: ${{ secrets.GITHUB_TOKEN }}
- name: 设置Git配置
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
- name: 增加版本号(补丁版本)
run: |
# 检查composer.json是否存在
if [ -f "composer.json" ]; then
# 读取当前版本号
CURRENT_VERSION=$(grep -oP '(?<="version": ")[0-9]+\.[0-9]+\.[0-9]+' composer.json)
if [ -n "$CURRENT_VERSION" ]; then
# 解析版本号
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
# 增加补丁版本号
PATCH_VERSION=$((VERSION_PARTS[2] + 1))
NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$PATCH_VERSION"
echo "当前版本: $CURRENT_VERSION -> 新版本: $NEW_VERSION"
# 更新composer.json中的版本号
sed -i "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" composer.json
# main分支:标准版本更新流程
git add composer.json
git commit -m "自动更新版本号至 $NEW_VERSION"
# 测试环境下不推送到main分支
# git push origin main
# 测试环境下不创建正式标签
# git tag -a v$NEW_VERSION -m "版本 $NEW_VERSION"
# git push origin v$NEW_VERSION
fi
fi