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="人民的勤务员Github:ChinaGodMan(UTC+8) $current_date Fix BUG" echo "提交信息: $commit_message" git commit -m "FIX" || echo "没有需要提交的更改" git push origin ${{ env.branch }} # 推送到当前分支 env: MY_GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }} # 传递 Token 到 Git 环境变量中