Версия документации #1
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: Deploy Documentation | |
| on: | |
| push: | |
| branches: | |
| - master | |
| - develop | |
| tags: | |
| - 'v*' | |
| paths: | |
| - 'docs/**' | |
| permissions: | |
| contents: write # нужно для push в gh-pages | |
| pages: write | |
| id-token: write | |
| concurrency: | |
| group: pages-${{ github.ref_name }} | |
| cancel-in-progress: true | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| base: ${{ steps.vars.outputs.base }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # нужны все теги | |
| - name: Compute deployment base | |
| id: vars | |
| run: | | |
| REF="${{ github.ref_name }}" | |
| if [ "$REF" = "master" ]; then BASE="/" | |
| elif [ "$REF" = "develop" ]; then BASE="/develop/" | |
| else BASE="/${REF}/" # теги: v1.2.3 → /v1.2.3/ | |
| fi | |
| echo "base=$BASE" >> "$GITHUB_OUTPUT" | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: npm | |
| cache-dependency-path: docs/package-lock.json | |
| - name: Install dependencies | |
| working-directory: docs | |
| run: npm ci | |
| - name: Build docs | |
| working-directory: docs | |
| env: | |
| DOCS_BASE: ${{ steps.vars.outputs.base }} | |
| run: npm run docs:build | |
| - name: Upload artifact | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: docs/.vitepress/dist | |
| # Обновляем единый /versions.json в корне gh-pages после каждого деплоя. | |
| # VersionSwitcher загружает его в рантайме — любая старая версия | |
| # всегда видит актуальный список, включая теги, добавленные позже. | |
| update-versions-json: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout full history (for tags) | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Checkout gh-pages | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: gh-pages | |
| path: gh-pages | |
| continue-on-error: true | |
| - name: Init gh-pages if branch missing | |
| run: | | |
| if [ ! -f gh-pages/.git/config ]; then | |
| mkdir -p gh-pages && cd gh-pages | |
| git init | |
| git remote add origin \ | |
| https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git | |
| git checkout --orphan gh-pages | |
| fi | |
| - name: Generate /versions.json | |
| run: | | |
| python3 - <<'EOF' | |
| import json, subprocess | |
| versions = [ | |
| {"label": "master (stable)", "base": "/"}, | |
| {"label": "develop (next)", "base": "/develop/"}, | |
| ] | |
| # Теги вида v1.2.3, отсортированные по semver, последние 10 | |
| out = subprocess.check_output( | |
| ["git", "tag", "-l", "v*", "--sort=-version:refname"] | |
| ).decode().strip() | |
| for tag in [t for t in out.splitlines() if t][:10]: | |
| versions.append({"label": tag, "base": f"/{tag}/"}) | |
| with open("gh-pages/versions.json", "w", encoding="utf-8") as f: | |
| json.dump(versions, f, ensure_ascii=False, indent=2) | |
| print(json.dumps(versions, ensure_ascii=False, indent=2)) | |
| EOF | |
| - name: Commit and push versions.json | |
| working-directory: gh-pages | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add versions.json | |
| git diff --cached --quiet && echo "versions.json unchanged" && exit 0 | |
| git commit -m "chore: update versions.json [skip ci]" | |
| git push origin gh-pages | |
| # master → корень GitHub Pages через официальный механизм Actions Pages | |
| deploy-master: | |
| if: github.ref_name == 'master' | |
| needs: [build, update-versions-json] | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: github-pages | |
| url: ${{ steps.deployment.outputs.page_url }} | |
| steps: | |
| - name: Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v4 | |
| # develop и теги → подпапка в gh-pages (/develop/, /v1.2.3/, …) | |
| deploy-subpath: | |
| if: github.ref_name != 'master' | |
| needs: [build, update-versions-json] | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout gh-pages | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: gh-pages | |
| path: gh-pages | |
| - name: Download build artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: github-pages | |
| path: artifact | |
| - name: Extract into subpath | |
| run: | | |
| SUBPATH="gh-pages/${{ needs.build.outputs.base }}" | |
| mkdir -p "$SUBPATH" | |
| tar -xf artifact/artifact.tar -C "$SUBPATH" | |
| - name: Push to gh-pages | |
| working-directory: gh-pages | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add . | |
| git diff --cached --quiet && echo "Nothing to commit" && exit 0 | |
| git commit -m "docs: deploy ${{ github.ref_name }} @ ${{ github.sha }}" | |
| git push origin gh-pages |