forked from ChinaGodMan/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
159 lines (133 loc) · 7.54 KB
/
Copy pathupdate-Script-version.yml
File metadata and controls
159 lines (133 loc) · 7.54 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: update-Script-version
on:
# schedule:
# - cron: "0 2 * * 2" # 每周二的 UTC 时间 2 点,即北京时间的早上 10 点
workflow_dispatch: # 允许手动触发工作流
jobs:
update-versions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
name: 检出代码
with:
token: ${{ secrets.MY_GITHUB_TOKEN }} # 使用你创建的 Token
- name: 获取当前分支
id: get_branch
run: |
echo "当前分支: $(git rev-parse --abbrev-ref HEAD)"
echo "branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
- name: 拉取最新更改
run: |
git pull origin ${{ env.branch }} # 拉取最新的远程更改
- name: 调试 JSON 文件
run: |
echo "当前脚本路径列表:"
cat docs/ScriptsPath.json # 打印 JSON 文件内容以进行调试
- name: 提取当前版本
id: extract_version
run: |
jq -c '.scripts[] | select(.isUpdated == true) | .path' docs/ScriptsPath.json | while IFS= read -r file; do
file=$(echo "$file" | tr -d '"') # 去除 JSON 中的双引号
echo "处理文件: \"$file\""
if [ -f "$file" ]; then
current_version=$(awk '/^\/\/ @version/ {print $NF}' "$file")
echo "文件 $file 的当前版本: $current_version"
if [ -z "$current_version" ]; then
echo "在脚本文件 $file 中未找到版本号"
exit 1
fi
version_number=$(echo "$current_version" | sed 's/\./ /g')
echo "版本号(转换为数字): $version_number"
version_number=$(echo $version_number | awk '{printf "%d", ($1*1000000 + $2*10000 + $3*100 + $4 + 1)}')
echo "版本号(加1后的数字): $version_number"
major=$((version_number / 1000000))
minor=$(( (version_number % 1000000) / 10000 ))
patch=$(( (version_number % 10000) / 100 ))
build=$(( version_number % 100 ))
new_version="$major.$minor.$patch.$build"
echo "文件 $file 的新版本号: $new_version"
sed -i "s|// @version[ ]\+[0-9]*\([.][0-9]*\)*|// @version $new_version|" "$file"
echo "${file}_NEW_VERSION=$new_version" >> $GITHUB_ENV
# 读取 backuppath 并复制文件
backup_path=$(jq -r --arg file "$file" '.scripts[] | select(.path == $file) | .backuppath' docs/ScriptsPath.json)
if [ -n "$backup_path" ]; then
echo "备份路径: $backup_path"
mkdir -p "$backup_path" # 创建备份目录
cp -f "$file" "$backup_path" # 强制覆盖文件
echo "文件 $file 备份到 $backup_path"
else
echo "未为文件 $file 指定备份路径"
fi
else
echo "未找到脚本文件: $file"
exit 1
fi
done
- name: 检查 @version 是否已更新
run: |
jq -c '.scripts[] | select(.isUpdated == true) | .path' docs/ScriptsPath.json | while IFS= read -r file; do
file=$(echo "$file" | tr -d '"')
echo "检查文件中的版本号: $file"
grep '^// @version' "$file"
done
- name: 调试环境变量
run: |
echo "当前环境变量:"
cat $GITHUB_ENV # 打印环境变量以进行调试
- name: 调试更改
run: |
echo "Git 状态:"
git status
echo "更改的差异:"
git diff # 打印更改的差异以进行调试
- name: 记录操作日志
run: |
# 获取更新的文件数量
updated_files_count=$(jq -c '.scripts[] | select(.isUpdated == true) | .path' docs/ScriptsPath.json | wc -l)
echo "Updated files count: $updated_files_count"
# 获取当前日期和时间,精确到秒,24小时制
current_date=$(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S')
echo "当前日期和时间: $current_date"
# 如果文件不存在,则创建并初始化为空数组
if [ ! -f docs/log/updatedUserScripts.json ]; then
echo "[]" > docs/log/updatedUserScripts.json
fi
# 更新 JSON 文件,先添加新记录
jq --arg date "$current_date" --argjson count "$updated_files_count" \
'. += [{"date": $date, "updated_files_count": $count}]' \
docs/log/updatedUserScripts.json > docs/log/updatedUserScripts.json.tmp
if [ $? -ne 0 ]; then
echo "jq 命令失败"
exit 1
fi
# 计算现有成员数量,排除 member_count 字段
member_count=$(jq '[.[] | select(has("member_count") | not)] | length' docs/log/updatedUserScripts.json.tmp)
echo "当前成员数量: $member_count"
# 更新 JSON 文件,修改或添加 member_count 字段
jq --argjson member_count "$member_count" \
'if .[0] | has("member_count") then
.[0].member_count = $member_count
else
[{"member_count": $member_count}] + .
end' \
docs/log/updatedUserScripts.json.tmp > docs/log/updatedUserScripts.json
if [ $? -ne 0 ]; then
echo "jq 命令失败"
exit 1
fi
rm docs/log/updatedUserScripts.json.tmp
# 输出最终结果用于调试
echo "更新后的 docs/log/updatedUserScripts.json 内容:"
cat docs/log/updatedUserScripts.json
- name: 提交和推送更改
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add .
current_date=$(TZ='Asia/Shanghai' date '+%Y/%m/%d %H:%M:%S')
commit_message="<b><img src='https://avatars.githubusercontent.com/in/15368?v=4%27&size=32'><a href='https://github.com/ChinaGodMan'><ruby>人民的勤务员<rt>Github:ChinaGodMan</rt></ruby></a><span>(UTC+8) $current_date Fix BUG</span></b>"
echo "提交信息: $commit_message"
git commit -m "FIX" || echo "没有需要提交的更改"
git push origin ${{ env.branch }} # 推送到当前分支
env:
MY_GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }} # 传递 Token 到 Git 环境变量中