update-version #31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Class Widgets 2 Releases JSON | |
| on: | |
| workflow_dispatch: | |
| repository_dispatch: | |
| types: [update-version] | |
| permissions: | |
| contents: write | |
| jobs: | |
| update-version: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v3 | |
| - name: Install jq | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y jq | |
| - name: Fetch latest release and alpha version of Class Widgets 2 | |
| id: fetch_versions | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| REPO_OWNER="RinLit-233-shiroko" | |
| REPO_NAME="Class-Widgets-2" | |
| API_URL="https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases" | |
| # 1. 一次性获取所有 Release 数据,并带上 Token 避免频率限制 | |
| RESPONSE=$(curl -s -H "Authorization: token $GH_TOKEN" "$API_URL") | |
| # 检查响应是否为有效数组 | |
| if ! echo "$RESPONSE" | jq -e 'if type == "array" then . else empty end' > /dev/null; then | |
| echo "Error: API returned invalid data or error message." | |
| echo "$RESPONSE" | |
| exit 1 | |
| fi | |
| # 2. 从响应中提取数据 (使用 jq 的 map 过滤) | |
| # 正式版 | |
| RELEASE_DATA=$(echo "$RESPONSE" | jq -c '[.[] | select(.prerelease == false)][0]') | |
| VERSION_RELEASE=$(echo "$RELEASE_DATA" | jq -r '.tag_name // ""') | |
| URL_RELEASE=$(echo "$RELEASE_DATA" | jq -r '.assets[] | select(.name | test("Windows")) | .browser_download_url' | head -n 1) | |
| # 测试版 | |
| ALPHA_DATA=$(echo "$RESPONSE" | jq -c '[.[] | select(.prerelease == true)][0]') | |
| VERSION_ALPHA=$(echo "$ALPHA_DATA" | jq -r '.tag_name // ""') | |
| URL_ALPHA=$(echo "$ALPHA_DATA" | jq -r '.assets[] | select(.name | test("Windows")) | .browser_download_url' | head -n 1) | |
| # 3. 兜底逻辑 | |
| if [ -z "$VERSION_RELEASE" ] || [ "$VERSION_RELEASE" = "null" ]; then | |
| VERSION_RELEASE="$VERSION_ALPHA" | |
| URL_RELEASE="$URL_ALPHA" | |
| fi | |
| # 4. 写入环境变量 | |
| echo "VERSION_RELEASE=$VERSION_RELEASE" >> $GITHUB_ENV | |
| echo "URL_RELEASE=$URL_RELEASE" >> $GITHUB_ENV | |
| echo "VERSION_ALPHA=$VERSION_ALPHA" >> $GITHUB_ENV | |
| echo "URL_ALPHA=$URL_ALPHA" >> $GITHUB_ENV | |
| - name: Create version.json | |
| run: | | |
| mkdir -p public/2 | |
| echo "{" > public/2/releases.json | |
| echo ' "release": {' >> public/2/releases.json | |
| echo ' "version": "'$VERSION_RELEASE'",' >> public/2/releases.json | |
| echo ' "url": {' >> public/2/releases.json | |
| echo ' "windows": "'$URL_RELEASE'"' >> public/2/releases.json | |
| echo ' }' >> public/2/releases.json | |
| echo ' },' >> public/2/releases.json | |
| echo ' "alpha": {' >> public/2/releases.json | |
| echo ' "version": "'$VERSION_ALPHA'",' >> public/2/releases.json | |
| echo ' "url": {' >> public/2/releases.json | |
| echo ' "windows": "'$URL_ALPHA'"' >> public/2/releases.json | |
| echo ' }' >> public/2/releases.json | |
| echo ' }' >> public/2/releases.json | |
| echo "}" >> public/2/releases.json | |
| cat public/2/releases.json | |
| - name: Commit and push changes | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| git add public/2/releases.json | |
| git commit -m "Update releases.json → Release: $VERSION_RELEASE | Alpha: $VERSION_ALPHA" || echo "No changes to commit" | |
| git push |