-
Notifications
You must be signed in to change notification settings - Fork 1
136 lines (116 loc) · 4.49 KB
/
create-test-release.yaml
File metadata and controls
136 lines (116 loc) · 4.49 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
name: 构建测试版本
on:
push:
branches:
- 'test-*'
workflow_dispatch:
permissions:
contents: write
jobs:
build:
name: 构建和上传发布资产
runs-on: macos-latest
steps:
- name: 结账代码
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: 检查分支
run: |
CURRENT_BRANCH=$(git branch --show-current)
if [[ ! $CURRENT_BRANCH =~ ^test- ]]; then
echo "错误: 该工作流只能在 test 分支上运行。当前分支: $CURRENT_BRANCH"
exit 1
fi
- name: 设置nodejs版本
uses: actions/setup-node@v4
with:
node-version: '22'
# - name: 缓存前端依赖
# uses: actions/cache@v3
# with:
# path: frontend/node_modules
# key: ${{ runner.os }}-frontend-${{ hashFiles('frontend/yarn.lock') }}
- name: 缓存游戏依赖
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-root-${{ hashFiles('yarn.lock') }}
- name: 检查yarn工具
run: |
if ! command -v yarn &> /dev/null; then
npm install -g yarn
fi
# - name: 安装前端依赖
# run: |
# cd frontend && yarn install --ignore-engines
- name: 安装游戏依赖
run: |
yarn install --ignore-engines
- name: 获取当前分支名
id: branch-info
run: |
CURRENT_BRANCH=$(git branch --show-current)
echo "current_branch=$CURRENT_BRANCH" >> $GITHUB_OUTPUT
echo "当前分支: $CURRENT_BRANCH"
- name: 打包
run: |
yarn build
# yarn bundle # bundle
mkdir -p dist-release
# cp -rf ./dist dist-release/dist # bundle
cp -rf ./lib dist-release/lib
cp -rf ./.puppeteerrc.cjs dist-release/.puppeteerrc.cjs
cp -rf ./package.json dist-release/package.json
cp -rf ./README.md dist-release/README.md
# 检查 dist 目录是否存在且不为空
if [[ ! -d dist-release || -z $(ls -A dist-release) ]]; then
echo "错误: 'dist' 目录不存在或为空,构建步骤可能失败。"
exit 1
fi
# 转移产物
mv dist-release ../dist-release
- name: 推送 release
run: |
CURRENT_BRANCH="${{ steps.branch-info.outputs.current_branch }}"
# 去掉 test- 前缀,添加 release- 前缀
DEPLOY_BRANCH="release-${CURRENT_BRANCH#test-}"
git fetch origin $DEPLOY_BRANCH || echo "远程分支 $DEPLOY_BRANCH 不存在,准备创建..."
if ! git show-ref --verify --quiet refs/remotes/origin/$DEPLOY_BRANCH; then
git checkout -b $DEPLOY_BRANCH
git push origin $DEPLOY_BRANCH
else
git checkout $DEPLOY_BRANCH
fi
# 删除依赖
rm -rf node_modules
rm -rf .gitignore
# 清理非 git 文件夹的其他内容(保护 .git 目录)
find . -mindepth 1 ! -path './.git*' -exec rm -rf {} + || true
# 移动 dist-release 文件夹内容到根目录
mv ../dist-release/* ./ || {
echo "错误: 无法将 'dist-release' 目录中的文件移动到根目录,目录可能为空。"
exit 1
}
# 检查 .git 是否还在
if [ ! -d .git ]; then
echo "错误:.git 目录已丢失,无法进行后续 git 操作"
exit 1
fi
# 去除 package.json 中一些字段
jq 'del(.devDependencies)' package.json > package.json.tmp && mv package.json.tmp package.json
jq 'del(.workspaces)' package.json > package.json.tmp && mv package.json.tmp package.json
jq 'del(.private)' package.json > package.json.tmp && mv package.json.tmp package.json
jq 'del(.scripts)' package.json > package.json.tmp && mv package.json.tmp package.json
# 开始产出
git config --local user.email "ningmengchongshui@gmail.com"
git config --local user.name "ningmengchongshui"
git config --local push.autoSetupRemote true
git add -A
git commit -m "Build from ${{ steps.branch-info.outputs.current_branch }}"
git push origin $DEPLOY_BRANCH || {
echo "错误:推送失败,请检查..."
exit 1
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}