deploy #1849
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: | |
- solution/** | |
- lcs/** | |
- lcp/** | |
- lcof2/** | |
- lcof/** | |
- lcci/** | |
- basic/** | |
concurrency: | |
group: ${{github.workflow}} - ${{github.ref}} | |
cancel-in-progress: true | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
# 1️⃣ Checkout main 分支代码到 ./main 目录 | |
- uses: actions/checkout@v4 | |
with: | |
path: main | |
# 2️⃣ Checkout docs 分支到 ./mkdocs 目录 | |
- uses: actions/checkout@v4 | |
with: | |
ref: docs | |
path: mkdocs | |
# 3️⃣ 移动竞赛 README 到 mkdocs/docs 结构中 | |
- name: Move contest files | |
run: | | |
cp main/solution/CONTEST_README.md mkdocs/docs/contest.md | |
cp main/solution/CONTEST_README_EN.md mkdocs/docs-en/contest.md | |
# 4️⃣ 安装 Python | |
- uses: actions/setup-python@v5 | |
with: | |
python-version: 3.x | |
# 5️⃣ 设置缓存 Key(按周) | |
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV | |
# 6️⃣ 缓存 mkdocs-material 的编译产物 | |
- uses: actions/cache@v4 | |
with: | |
key: mkdocs-material-${{ env.cache_id }} | |
path: mkdocs/.cache | |
restore-keys: | | |
mkdocs-material- | |
# 7️⃣ 安装依赖 | |
- name: Install dependencies | |
working-directory: mkdocs | |
run: | | |
python3 -m pip install --upgrade pip | |
python3 -m pip install -r requirements.txt | |
python3 -m pip install "mkdocs-material[imaging]" | |
sudo apt-get install pngquant | |
# 8️⃣ 设置 API token 环境变量 | |
- name: Set MKDOCS_API_KEYS environment variable | |
run: echo "MKDOCS_API_KEYS=${{ secrets.MKDOCS_API_KEYS }}" >> $GITHUB_ENV | |
# 9️⃣ 执行构建 | |
- name: Build site | |
working-directory: mkdocs | |
run: | | |
python3 main.py | |
mkdocs build -f mkdocs.yml | |
mkdocs build -f mkdocs-en.yml | |
echo "leetcode.doocs.org" > ./site/CNAME | |
# 🔟 提交缓存到 docs 分支 | |
- name: Commit cache files back to docs branch | |
working-directory: mkdocs | |
run: | | |
if [ -d ".git" ]; then | |
git config user.name "github-actions[bot]" | |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
git add .cache/path-map.json || true | |
git add .cache/plugin/git-committers/page-authors.json || true | |
if git diff --cached --quiet; then | |
echo "No changes to commit" | |
else | |
git commit -m "chore: update committers and path map [bot]" | |
git push origin HEAD:docs | |
fi | |
else | |
echo "::error ::Git directory not found in mkdocs/. Are you sure checkout was successful?" | |
exit 1 | |
# 1️⃣1️⃣ 上传构建产物 | |
- name: Upload artifact | |
uses: actions/upload-pages-artifact@v3 | |
with: | |
path: mkdocs/site | |
deploy: | |
needs: build | |
permissions: | |
pages: write | |
id-token: write | |
environment: | |
name: github_pages | |
url: ${{ steps.deployment.outputs.page_url }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Deploy to GitHub Pages | |
id: deployment | |
uses: actions/deploy-pages@v4 |