|
| 1 | +# take from https://github.com/LincDocs/Workflow |
| 2 | + |
| 3 | +name: 部署文档 |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: ["main"] # 确保这是你正在使用的分支名称 |
| 8 | + schedule: |
| 9 | + # 定时任务。建议多个仓库错开,避免多个仓库同一时间一起坏掉。我个人用键盘阵列映射周1~5。特别是代理仓库比较需要这个 |
| 10 | + - cron: '42 15 * * 6' # 每周六15:42触发工作流 |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + |
| 16 | +# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. |
| 17 | +# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. |
| 18 | +concurrency: |
| 19 | + group: "pages" |
| 20 | + cancel-in-progress: false |
| 21 | + |
| 22 | +jobs: |
| 23 | + deploy-gh-pages: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: 环境 - 构建库 |
| 27 | + uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + fetch-depth: 1 |
| 30 | + repository: LincZero/LincZero.github.io |
| 31 | + ref: 'main' # 分支,旧raw |
| 32 | + |
| 33 | + - name: 环境 - 安装 pnpm |
| 34 | + uses: pnpm/action-setup@v4 |
| 35 | + with: |
| 36 | + run_install: true |
| 37 | + version: 9 |
| 38 | + |
| 39 | + - name: 环境 - 设置 Node.js |
| 40 | + uses: actions/setup-node@v4 |
| 41 | + with: |
| 42 | + node-version: 22 |
| 43 | + cache: pnpm |
| 44 | + |
| 45 | + # 获取仓库的相关配置 |
| 46 | + # https://docs.github.com/zh/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables |
| 47 | + # TODO:若 “仓库无前缀名” 是.github.io结尾,则设置为"/" |
| 48 | + - name: 配置 - 获取仓库配置 |
| 49 | + id: config1 # 用于给其他步骤引用 |
| 50 | + working-directory: ./scripts/ |
| 51 | + run: | |
| 52 | + > git_config.json # 先清空 |
| 53 | + echo "{" >> git_config.json |
| 54 | + echo " \"GITHUB_WORKSPACE\": \"${GITHUB_WORKSPACE}\"," >> git_config.json # 工作路径 |
| 55 | + echo " \"GITHUB_ACTION_PATH\": \"${GITHUB_ACTION_PATH}\"," >> git_config.json # action路径 |
| 56 | + echo " \"GITHUB_ACTION_REPOSITORY\": \"${GITHUB_ACTION_REPOSITORY}\"," >> git_config.json # action仓库 |
| 57 | + echo " \"GITHUB_REPOSITORY_OWNER\": \"${GITHUB_REPOSITORY_OWNER}\"," >> git_config.json # 仓库所属(格式: 可以是组织) |
| 58 | + echo " \"GITHUB_ACTOR\": \"${GITHUB_ACTOR}\"," >> git_config.json # 仓库作者(格式: 不会是组织) |
| 59 | + echo " \"GITHUB_REPOSITORY\": \"${GITHUB_REPOSITORY}\"," >> git_config.json # 仓库标识(格式: 个人或组织/仓库名) |
| 60 | + echo " \"CALC_URL\": \"${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/\"," >> git_config.json # 仓库url |
| 61 | + REPO_NAME=$(echo "${GITHUB_REPOSITORY}" | cut -d"/" -f2) |
| 62 | + echo " \"CALC_REPO_NAME\": \"${REPO_NAME}\"" >> git_config.json # 仓库无前缀名 |
| 63 | + echo "}" >> git_config.json |
| 64 | + echo "::set-output name=REPO_NAME::$REPO_NAME" |
| 65 | +
|
| 66 | + - name: 文档 - 文档克隆1 # 文档的克隆、构建、部署。注意 `clone --depth 1` 只拉最近一次提交,减少时间 |
| 67 | + working-directory: ./src/ |
| 68 | + run: | |
| 69 | + rm README.md |
| 70 | + |
| 71 | + # git clone --depth 1 https://github.com/${GITHUB_REPOSITORY}.git # 如果有多个clone项则替换成这个,避免冲突 |
| 72 | + git clone --depth 1 https://github.com/${GITHUB_REPOSITORY}.git temp_repo |
| 73 | +
|
| 74 | + - name: 文档 - 文档克隆2, 允许使用代理仓库/指定文件夹 |
| 75 | + working-directory: ./src/ |
| 76 | + run: | |
| 77 | + # step1. 克隆被代理仓库 |
| 78 | + # 该仓库为代理仓库时,使用链接仓库而非此仓库 |
| 79 | + # TODO 支持agency多个仓库。如果想更通用,干脆支持直接运行agency里的命令组 |
| 80 | + if [ -f temp_repo/agency ]; then |
| 81 | + echo "with agency" |
| 82 | + GIT_LINK=$(sed -n '1p' temp_repo/agency | tr -d '\r') |
| 83 | + DIR=$(sed -n '2p' temp_repo/agency | tr -d '\r') |
| 84 | + rm -rf temp_repo |
| 85 | + git clone --depth 1 $GIT_LINK temp_repo |
| 86 | + else |
| 87 | + echo "without agency" |
| 88 | + DIR="docs/" |
| 89 | + fi |
| 90 | +
|
| 91 | + # 使用指定文件夹 |
| 92 | + if [ -d "temp_repo/${DIR}" ]; then |
| 93 | + echo "with docs folder: temp_repo/${DIR}" |
| 94 | + # find temp_repo/* -maxdepth 0 -name docs -prune -o -exec rm -rf {} \; |
| 95 | + # ls -l temp_repo/website/docs/ # debug |
| 96 | + mv "temp_repo/${DIR}"/* . |
| 97 | + rm -rf temp_repo |
| 98 | + else |
| 99 | + echo "without docs folder: temp_repo/${DIR}" |
| 100 | + rsync -a temp_repo/ . |
| 101 | + rm -rf temp_repo |
| 102 | + fi |
| 103 | + echo "---" |
| 104 | + ls |
| 105 | +
|
| 106 | + # [!code] 根据实际情况修改 (需要在仓库配置写入以及和文档仓库clone这两个步骤的后面) |
| 107 | + - name: 配置 - 设置 |
| 108 | + working-directory: ./ |
| 109 | + run: | |
| 110 | + # sed -i 's/base: \"\/\"/base: \"\/${{steps.config1.outputs.REPO_NAME}}\/\"/g' ./src/.vuepress/config.ts |
| 111 | + cat ./scripts/git_config.json |
| 112 | + rm -f ./src/.vuepress/config_cover.js |
| 113 | + rm -f ./src/.vuepress/theme_cover.js |
| 114 | + pnpm run gen-config |
| 115 | +
|
| 116 | + - name: 文档 - 构建 |
| 117 | + env: |
| 118 | + NODE_OPTIONS: --max_old_space_size=20480 |
| 119 | + run: |- |
| 120 | + node -e 'console.log(v8.getHeapStatistics())' |
| 121 | + pnpm run docs:build |
| 122 | + > src/.vuepress/dist/.nojekyll |
| 123 | +
|
| 124 | + - name: 文档 - 部署 |
| 125 | + uses: JamesIves/github-pages-deploy-action@v4 |
| 126 | + with: |
| 127 | + # 这是文档部署到的分支名称 |
| 128 | + branch: gh-pages |
| 129 | + folder: src/.vuepress/dist |
0 commit comments