Skip to content

Auto Update Worker #272

Auto Update Worker

Auto Update Worker #272

Workflow file for this run

name: Auto Update Worker
on:
push:
branches:
- main
schedule:
- cron: "0 16 * * *" # 中国时间凌晨 0 点(UTC+8 = UTC 16:00)
workflow_dispatch:
inputs:
force_update:
description: '是否强制更新(忽略版本检查)'
required: false
default: 'false'
permissions:
contents: write
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: 检出仓库
uses: actions/checkout@v4
- name: 安装依赖
run: |
sudo apt-get update
sudo apt-get install -y jq unzip
- name: 设置环境变量
run: |
echo "REPO_URL=https://api.github.com/repos/bia-pain-bache/BPB-Worker-Panel/releases/latest" >> $GITHUB_ENV
echo "TARGET_FILE=worker.zip" >> $GITHUB_ENV
- name: 检查并更新 Worker(仅正式版本)
id: check_update
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FORCE_UPDATE: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.force_update == 'true' }}
run: |
log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"; }
log "开始检查更新..."
LOCAL_VERSION=$(cat version.txt 2>/dev/null || echo "")
log "本地版本: ${LOCAL_VERSION:-无}"
log "获取最新 Release 信息..."
RESPONSE=$(curl -s --retry 3 -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" "$REPO_URL")
TAG_NAME=$(echo "$RESPONSE" | jq -r '.tag_name')
DOWNLOAD_URL=$(echo "$RESPONSE" | jq -r '.assets[] | select(.name == "'"$TARGET_FILE"'") | .browser_download_url')
if [ -z "$DOWNLOAD_URL" ] || [ "$DOWNLOAD_URL" == "null" ]; then
log "ERROR: 未找到 $TARGET_FILE"
exit 1
fi
log "最新版本: $TAG_NAME"
if [ "$LOCAL_VERSION" = "$TAG_NAME" ] && [ "$FORCE_UPDATE" != "true" ]; then
log "已是最新版本,无需更新"
exit 0
fi
log "下载并解压 $TARGET_FILE..."
wget -q -O "$TARGET_FILE" "$DOWNLOAD_URL"
unzip -o "$TARGET_FILE" -d .
rm "$TARGET_FILE"
echo "$TAG_NAME" > version.txt
log "更新完成,新版本: $TAG_NAME"
echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT"
- name: 提交更改
if: success()
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "🔄 自动同步 Worker 版本: ${{ steps.check_update.outputs.tag_name }}"
commit_author: "github-actions[bot] <github-actions[bot]@users.noreply.github.com>"