-
Notifications
You must be signed in to change notification settings - Fork 3
219 lines (173 loc) · 7.18 KB
/
release-on-commit.yml
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
name: Release on Commit
on:
workflow_dispatch: # 允许手动触发
jobs:
build:
runs-on: ubuntu-latest
if: github.actor != 'github-actions[bot]' && github.actor != 'dependabot[bot]'
steps:
- name: 拉取代码
uses: actions/checkout@v4
- name: 打印 github.actor
run: |
echo "ACTOR=${{ github.actor }}" >> $GITHUB_ENV
echo "Triggered by: $ACTOR"
- name: 安装node
uses: actions/setup-node@v4
with:
node-version: 21
registry-url: "https://registry.npmjs.org"
- name: 安装pnpm
uses: pnpm/action-setup@v2
with:
version: 9.7.1
- name: 安装依赖
run: |
pnpm install
pnpm add node-karin@latest
- name: 放弃 package.json 的本地更改
run: |
git checkout HEAD package.json
- name: 删除 pnpm-lock.yaml
run: rm -rf pnpm-lock.yaml
- name: 编译
run: pnpm build
- name: 获取当前提交哈希
run: echo "COMMIT_HASH=${{ github.sha }}" >> $GITHUB_ENV
id: get_commit_hash
- name: 设置当前分支名称到环境变量
run: |
BRANCH_NAME=$(echo ${{ github.ref }} | sed 's|refs/heads/||')
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
echo "Current branch: $BRANCH_NAME"
- name: 获取短哈希值
run: echo "SHORT_COMMIT_HASH=$(echo ${{ env.COMMIT_HASH }} | cut -c1-7)" >> $GITHUB_ENV
- name: 创建临时文件夹存储编译后的lib文件夹和resources文件夹
run: |
mkdir ./../temp
- name: 复制编译后的lib文件夹和resources文件夹到临时文件夹
run: |
cp -r ./lib ./../temp/lib
cp -r ./resources ./../temp/resources
- name: 检查并创建空的 dev 分支
run: |
git fetch origin
# 检查 dev 分支是否存在
if git show-ref --verify --quiet refs/heads/dev; then
echo "Dev branch exists locally. Switching to it."
git checkout dev
else
# 检查远程是否有 dev 分支
if git ls-remote --exit-code --heads origin dev; then
echo "Dev branch exists remotely. Fetching and switching to it."
git checkout dev
else
# dev 分支不存在,创建一个空的 dev 分支
echo "Dev branch does not exist. Creating an empty dev branch."
git checkout --orphan dev # 创建一个没有历史记录的空分支
git reset --hard # 重置所有文件
git clean -fdx # 删除未被 Git 管理的文件和文件夹
echo "Empty dev branch created."
git commit --allow-empty -m "Initial empty commit on dev branch"
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} dev # 推送到远程仓库
fi
fi
- name: 切换到 build 分支
run: |
git fetch origin dev
git checkout dev
- name: 删除dev分支上的lib文件夹和resources文件夹
run: |
rm -rf lib
rm -rf resources
- name: 复制编译后的lib文件夹和resources文件夹到 dev 分支
run: |
cp -r ./../temp/lib ./
cp -r ./../temp/resources ./
- name: 删除临时文件夹
run: |
rm -rf ./../temp
- name: 从main分支复制文件到dev分支
run: |
git checkout main -- package.json CHANGELOG.md README.md config resources LICENSE
- name: 修订版本号
run: npm run commit all
if: success()
- name: 删除依赖文件夹 node_modules
run: |
rm -rf node_modules
- name: 将文件整理到 karin-plugin-ling 文件夹
run: |
mkdir -p karin-plugin-ling
find . -path "./.git" -prune -o -type f -print | while read file; do
mkdir -p "karin-plugin-ling/$(dirname "$file")"
cp "$file" "karin-plugin-ling/$file"
done
- name: 创建 dev.zip
run: zip -r "dev-${{ env.SHORT_COMMIT_HASH }}.zip" karin-plugin-ling
- name: 上传 dev.zip
uses: actions/upload-artifact@v4
with:
name: "dev-${{ env.SHORT_COMMIT_HASH }}.zip"
path: "dev-${{ env.SHORT_COMMIT_HASH }}.zip"
- name: 删除 karin-plugin-ling 文件夹
run: rm -rf karin-plugin-ling
- name: 发布到 npm
run: npm run pub-beta
if: success()
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: 创建发行版
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "${{ env.PKG_VERSION }}"
release_name: "${{ env.PKG_VERSION }}"
body: |
### 🎉 构建完成!
- 此版本是基于快照 [`${{ env.SHORT_COMMIT_HASH }}`](https://github.com/${{ github.repository }}/tree/${{ env.COMMIT_HASH }}) 所构建的,[**点击此处查看更改**](https://github.com/${{ github.repository }}/commit/${{ env.COMMIT_HASH }}) By @${{ github.actor }}
### 安装该版本
**以下安装方式任选其一**
---
<details>
<summary>使用Git</summary>
**插件根目录** 下执行
```sh
git pull
```
```sh
git checkout ${{ env.COMMIT_HASH }}
```
若报错缺失依赖,可先运行 `pnpm install` 安装或更新依赖。
</details>
<details>
<summary>使用包管理器</summary>
**Karin 根目录** 下执行
```sh
pnpm add karin-plugin-ling@${{ env.PKG_VERSION }} -w
```
</details>
<details>
<summary>使用预发行版(不推荐)</summary>
1. 下载名为 [build-${{ env.SHORT_COMMIT_HASH }}.zip](https://github.com/${{ github.repository }}/releases/download/pre-${{ env.SHORT_COMMIT_HASH }}/build-${{ env.SHORT_COMMIT_HASH }}.zip) 的压缩包
2. 在 `karin/plugins/` 目录下解压该压缩包,选择替换所有文件。
3. 完成后相关源码应在 `Karin根目录/plugins/karin-plugin-kkk/` 内<br><br>
若报错缺失依赖,可先运行 `pnpm install` 安装或更新依赖。
</details>
---
**此版本非正式版本,可能存在一些问题,请谨慎使用!**
构建时间:${{ github.event.head_commit.timestamp }}
draft: false
prerelease: true
- name: 上传
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: "./build-${{ env.SHORT_COMMIT_HASH }}.zip"
asset_name: "build-${{ env.SHORT_COMMIT_HASH }}.zip"
asset_content_type: application/zip