ci: fix deploy artifact and cache restore warnings (#5409) #2764
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 | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - .github/workflows/deploy.yml | |
| concurrency: | |
| group: deploy-site | |
| cancel-in-progress: false | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| fail-fast: true | |
| matrix: | |
| include: | |
| - lang: zh | |
| config: mkdocs.yml | |
| site_path: site | |
| - lang: en | |
| config: mkdocs-en.yml | |
| site_path: site/en | |
| permissions: | |
| contents: read | |
| actions: write | |
| steps: | |
| - name: Checkout main branch | |
| uses: actions/checkout@v7 | |
| with: | |
| ref: main | |
| fetch-depth: 0 | |
| - name: Checkout docs branch | |
| uses: actions/checkout@v7 | |
| with: | |
| ref: docs | |
| path: mkdocs | |
| fetch-depth: 0 | |
| - name: Setup Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: 3.x | |
| - name: Restore pip cache | |
| uses: actions/cache@v5 | |
| with: | |
| path: ~/.cache/pip | |
| key: ${{ runner.os }}-pip-docs-${{ matrix.lang }}-${{ hashFiles('mkdocs/requirements.txt') }} | |
| restore-keys: | | |
| ${{ runner.os }}-pip-docs-${{ matrix.lang }}- | |
| ${{ runner.os }}-pip-docs- | |
| - name: Install site dependencies | |
| run: | | |
| python3 -m pip install --upgrade pip | |
| python3 -m pip install -r mkdocs/requirements.txt | |
| sudo apt-get install -y pngquant | |
| - name: Overlay docs site engine | |
| run: | | |
| set -euo pipefail | |
| items=( | |
| docs | |
| docs-en | |
| hooks | |
| overrides | |
| mkdocs.yml | |
| mkdocs-en.yml | |
| .git-committers-cache.json | |
| ) | |
| for item in "${items[@]}"; do | |
| if [ -e "mkdocs/$item" ]; then | |
| rsync -a "mkdocs/$item" ./ | |
| fi | |
| done | |
| if [ ! -f mkdocs/build_site.py ]; then | |
| echo "docs branch is missing build_site.py." | |
| exit 1 | |
| fi | |
| rsync -a mkdocs/build_site.py ./ | |
| rm -rf mkdocs | |
| if [ -f solution/CONTEST_README.md ]; then | |
| mv solution/CONTEST_README.md docs/contest.md | |
| else | |
| echo "warning: solution/CONTEST_README.md missing; writing a stub." | |
| printf '%s\n' '---' 'comments: true' '---' '' '# 力扣竞赛' '' '竞赛列表暂未包含在本次构建中。' > docs/contest.md | |
| fi | |
| if [ -f solution/CONTEST_README_EN.md ]; then | |
| mv solution/CONTEST_README_EN.md docs-en/contest.md | |
| else | |
| echo "warning: solution/CONTEST_README_EN.md missing; writing a stub." | |
| printf '%s\n' '---' 'comments: true' '---' '' '# LeetCode Contest' '' 'The contest list is not included in this build.' > docs-en/contest.md | |
| fi | |
| - name: Build ${{ matrix.lang }} site | |
| env: | |
| MKDOCS_API_KEYS: ${{ secrets.MKDOCS_API_KEYS }} | |
| run: | | |
| python3 build_site.py | |
| mkdocs build -f ${{ matrix.config }} | |
| - name: Upload ${{ matrix.lang }} site | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: site-${{ matrix.lang }} | |
| path: ${{ matrix.site_path }} | |
| if-no-files-found: error | |
| - name: Ensure committer cache exists | |
| run: | | |
| if [[ ! -f .git-committers-cache.json ]]; then | |
| echo "committer cache missing after build; writing an empty stub." | |
| printf '%s\n' '{"cache_date":0,"page_authors":{}}' > .git-committers-cache.json | |
| fi | |
| - name: Upload ${{ matrix.lang }} committer cache | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: committer-cache-${{ matrix.lang }} | |
| path: .git-committers-cache.json | |
| include-hidden-files: true | |
| if-no-files-found: error | |
| package: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| actions: write | |
| steps: | |
| - name: Download Chinese site | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: site-zh | |
| path: site | |
| - name: Download English site | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: site-en | |
| path: site/en | |
| - name: Generate CNAME | |
| run: echo "leetcode.doocs.org" > ./site/CNAME | |
| - name: Download committer caches | |
| if: github.event_name != 'pull_request' | |
| continue-on-error: true | |
| uses: actions/download-artifact@v8 | |
| with: | |
| pattern: committer-cache-* | |
| path: caches | |
| merge-multiple: false | |
| - name: Commit merged committer cache to docs branch | |
| if: github.event_name != 'pull_request' | |
| env: | |
| GH_REPO: ${{ github.repository }} | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| python3 - <<'PY' | |
| import json | |
| from pathlib import Path | |
| authors = {} | |
| cache_date = 0 | |
| for path in Path("caches").glob("**/.git-committers-cache.json"): | |
| data = json.loads(path.read_text(encoding="utf-8")) | |
| cache_date = max(cache_date, data.get("cache_date") or 0) | |
| for key, val in data.get("page_authors", {}).items(): | |
| prev = authors.get(key) | |
| if prev is None or (val.get("retrieved") or 0) >= ( | |
| prev.get("retrieved") or 0 | |
| ): | |
| authors[key] = val | |
| if not authors: | |
| print("no committer cache to merge") | |
| raise SystemExit(0) | |
| Path(".git-committers-cache.json").write_text( | |
| json.dumps( | |
| {"cache_date": cache_date, "page_authors": authors}, | |
| ensure_ascii=False, | |
| indent=2, | |
| ) | |
| + "\n", | |
| encoding="utf-8", | |
| ) | |
| print(f"merged {len(authors)} committer entries") | |
| PY | |
| if [[ ! -f .git-committers-cache.json ]]; then | |
| echo "Cache file not found; skip commit." | |
| exit 0 | |
| fi | |
| git clone --depth 1 --branch docs "https://x-access-token:${GH_TOKEN}@github.com/${GH_REPO}.git" docs-cache | |
| cp .git-committers-cache.json docs-cache/ | |
| cd docs-cache | |
| git config user.name github-actions[bot] | |
| git config user.email 41898282+github-actions[bot]@users.noreply.github.com | |
| git add .git-committers-cache.json | |
| git commit -m "chore: update committer cache [skip ci]" || echo "No changes to commit" | |
| git push origin docs | |
| - name: Upload to GitHub Pages | |
| uses: actions/upload-pages-artifact@v5 | |
| with: | |
| path: site | |
| deploy: | |
| needs: package | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pages: write | |
| id-token: write | |
| environment: | |
| name: github_pages | |
| url: ${{ steps.deployment.outputs.page_url }} | |
| steps: | |
| - name: Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v5 |