chore(ci): 优化 GitHub Release 工作流,提升 release 分支发布与资产同步稳定性 #1101
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: build | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: | |
| - master | |
| paths: | |
| - "backend/package.json" | |
| pull_request: | |
| branches: | |
| - master | |
| paths: | |
| - "backend/package.json" | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v6 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v6 | |
| with: | |
| node-version-file: ".node-version" | |
| - name: Install dependencies | |
| run: | | |
| corepack enable | |
| cd backend && pnpm i --no-frozen-lockfile | |
| - name: Sync latest mihomo version | |
| run: | | |
| MIHOMO_VERSION="$(curl -fsSL https://github.com/MetaCubeX/mihomo/releases/latest/download/version.txt | tr -d '\r\n')" | |
| test -n "$MIHOMO_VERSION" | |
| echo "Using mihomo version: $MIHOMO_VERSION" | |
| cd backend | |
| MIHOMO_VERSION="$MIHOMO_VERSION" node <<'NODE' | |
| const fs = require('fs'); | |
| const files = ['src/utils/download.js', 'src/utils/flow.js']; | |
| const version = process.env.MIHOMO_VERSION; | |
| let replacements = 0; | |
| for (const file of files) { | |
| const source = fs.readFileSync(file, 'utf8'); | |
| const next = source.replace(/clash\.meta\/[^"']+/g, () => { | |
| replacements += 1; | |
| return `clash.meta/${version}`; | |
| }); | |
| if (next !== source) { | |
| fs.writeFileSync(file, next); | |
| } | |
| } | |
| if (replacements === 0) { | |
| throw new Error('No clash.meta version strings were updated.'); | |
| } | |
| console.log(`Updated ${replacements} clash.meta version string(s).`); | |
| NODE | |
| - name: Test | |
| run: | | |
| cd backend | |
| pnpm test | |
| # - name: Build | |
| # run: | | |
| # cd backend | |
| # pnpm run build | |
| - name: Bundle | |
| run: | | |
| cd backend | |
| pnpm bundle:esbuild | |
| - id: tag | |
| name: Generate release tag | |
| run: | | |
| cd backend | |
| SUBSTORE_RELEASE="$(node --eval="process.stdout.write(require('./package.json').version)")" | |
| echo "release_tag=${SUBSTORE_RELEASE}" >> "${GITHUB_OUTPUT}" | |
| - name: Prepare release | |
| run: | | |
| cd backend | |
| pnpm i -D conventional-changelog-cli | |
| pnpm run changelog | |
| - name: Release | |
| uses: softprops/action-gh-release@v3 | |
| if: ${{ success() && github.event_name != 'pull_request' && github.ref == 'refs/heads/master' }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| body_path: ./backend/CHANGELOG.md | |
| tag_name: ${{ steps.tag.outputs.release_tag }} | |
| # generate_release_notes: true | |
| files: | | |
| ./backend/sub-store.min.js | |
| ./backend/dist/sub-store-0.min.js | |
| ./backend/dist/sub-store-1.min.js | |
| ./backend/dist/sub-store-parser.loon.min.js | |
| ./backend/dist/cron-sync-artifacts.min.js | |
| ./backend/dist/proxy-utils.esm.mjs | |
| ./backend/dist/sub-store.bundle.js | |
| - name: Git push assets to "release" branch | |
| if: ${{ success() && github.event_name != 'pull_request' && github.ref == 'refs/heads/master' }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| auth_header="AUTHORIZATION: bearer ${GITHUB_TOKEN}" | |
| repo_url="https://github.com/${GITHUB_REPOSITORY}.git" | |
| release_dir="${RUNNER_TEMP}/release-dist" | |
| rm -rf "${release_dir}" | |
| if git -c http.https://github.com/.extraheader="${auth_header}" ls-remote --exit-code --heads "${repo_url}" release > /dev/null; then | |
| git -c http.https://github.com/.extraheader="${auth_header}" clone --depth 1 --branch release "${repo_url}" "${release_dir}" | |
| else | |
| git init -b release "${release_dir}" | |
| git -C "${release_dir}" remote add origin "${repo_url}" | |
| fi | |
| rsync -a --delete --exclude='.git' backend/dist/ "${release_dir}/" | |
| cd "${release_dir}" | |
| git config --local user.name "github-actions[bot]" | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git add -A | |
| if git diff --cached --quiet; then | |
| echo "No release asset changes to push." | |
| exit 0 | |
| fi | |
| git commit -m "release: ${{ steps.tag.outputs.release_tag }}" | |
| git -c http.https://github.com/.extraheader="${auth_header}" push origin release | |
| # - name: Sync to GitLab | |
| # env: | |
| # GITLAB_PIPELINE_TOKEN: ${{ secrets.GITLAB_PIPELINE_TOKEN }} | |
| # run: | | |
| # curl -X POST --fail -F token=$GITLAB_PIPELINE_TOKEN -F ref=master https://gitlab.com/api/v4/projects/48891296/trigger/pipeline |