main.yml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. name: Build
  2. on:
  3. push:
  4. branches:
  5. - master
  6. pull_request:
  7. schedule:
  8. - cron: "17 5 * * *" # Runs at 05:17 UTC
  9. - cron: "17 17 * * *" # Runs at 17:17 UTC
  10. workflow_dispatch:
  11. concurrency:
  12. group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  13. cancel-in-progress: true
  14. jobs:
  15. build:
  16. name: Build
  17. runs-on: ubuntu-24.04-arm
  18. steps:
  19. - uses: smorimoto/tune-github-hosted-runner-network@v1
  20. # https://github.com/actions/runner-images/issues/1187
  21. - uses: actions/checkout@v6
  22. with:
  23. persist-credentials: false
  24. - uses: pnpm/action-setup@v5
  25. with:
  26. run_install: false
  27. - uses: actions/setup-node@v6
  28. with:
  29. node-version-file: ".node-version"
  30. cache: "pnpm"
  31. - name: Grab Building Folder
  32. id: ramdisk
  33. run: |
  34. echo "build_dir=previous-build-${{ github.run_id }}-${{ github.run_number }}" >> $GITHUB_OUTPUT
  35. - name: Download Previous Build
  36. uses: actions/checkout@v6
  37. with:
  38. repository: SukkaLab/ruleset.skk.moe
  39. # during a race condition the dist repo may be private, use token to clone
  40. token: ${{ secrets.GIT_TOKEN }}
  41. persist-credentials: false
  42. filter: "tree:0" # we don't care about git history here
  43. fetch-tags: false
  44. path: ${{ steps.ramdisk.outputs.build_dir }}
  45. - name: Setup build folder
  46. run: |
  47. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/.git ]; then
  48. echo ".git not found"
  49. exit 1
  50. fi
  51. rm -rf "${{ steps.ramdisk.outputs.build_dir }}/.git"
  52. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/List ]; then
  53. echo "List not found"
  54. exit 1
  55. fi
  56. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  57. - name: Get current date
  58. id: date
  59. run: |
  60. echo "date=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
  61. echo "year=$(date +'%Y')" >> $GITHUB_OUTPUT
  62. echo "month=$(date +'%m')" >> $GITHUB_OUTPUT
  63. echo "day=$(date +'%d')" >> $GITHUB_OUTPUT
  64. echo "hour=$(date +'%H')" >> $GITHUB_OUTPUT
  65. echo "minute=$(date +'%M')" >> $GITHUB_OUTPUT
  66. echo "second=$(date +'%S')" >> $GITHUB_OUTPUT
  67. - name: Restore cache.db
  68. uses: actions/cache/restore@v6
  69. id: cache-db-restore
  70. with:
  71. path: |
  72. .cache
  73. key: ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:${{ steps.date.outputs.second }}
  74. # If source files changed but packages didn't, rebuild from a prior cache.
  75. restore-keys: |
  76. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:
  77. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:
  78. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }}
  79. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-
  80. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-
  81. ${{ runner.os }}-v3-
  82. - run: pnpm config set --location=global minimumReleaseAge 0
  83. - run: pnpm install
  84. - run: pnpm run build
  85. env:
  86. PUBLIC_DIR: ${{ steps.ramdisk.outputs.build_dir }}
  87. - name: Pre-deploy check
  88. # If the public directory doesn't exist, the build should fail.
  89. # If the public directory is empty, the build should fail.
  90. run: |
  91. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }} ]; then
  92. echo "public directory not found"
  93. exit 1
  94. fi
  95. if [ ! "$(ls -A ${{ steps.ramdisk.outputs.build_dir }})" ]; then
  96. echo "public directory is empty"
  97. exit 1
  98. fi
  99. if [ ! -f .BUILD_FINISHED ]; then
  100. echo ".BUILD_FINISHED not found"
  101. exit 1
  102. fi
  103. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  104. - uses: actions/upload-artifact@v7
  105. with:
  106. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  107. path: ${{ steps.ramdisk.outputs.build_dir }}
  108. if-no-files-found: error
  109. retention-days: 1
  110. compression-level: 4
  111. include-hidden-files: false
  112. - name: Cache cache.db
  113. if: always()
  114. uses: actions/cache/save@v6
  115. with:
  116. path: |
  117. .cache
  118. key: ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:${{ steps.date.outputs.second }}
  119. diff_deployment_on_pr:
  120. if: github.ref != 'refs/heads/master'
  121. needs:
  122. - build
  123. name: Diff output
  124. runs-on: ubuntu-slim
  125. steps:
  126. - uses: actions/download-artifact@v8
  127. with:
  128. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  129. path: public
  130. - name: Diff
  131. run: |
  132. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git >/dev/null 2>&1
  133. cd ./deploy-git
  134. git fetch origin master >/dev/null 2>&1
  135. rm -rf ./*
  136. cp -rf ../public/* ./
  137. git --no-pager diff --minimal
  138. env:
  139. GH_USER: ${{ secrets.GIT_USER }}
  140. GH_TOKEN: ${{ secrets.GIT_TOKEN }}
  141. deploy:
  142. needs:
  143. - build
  144. name: Deploy
  145. if: github.ref == 'refs/heads/master'
  146. runs-on: ubuntu-slim
  147. # matrix is a tricky way to define a variable directly in the action yaml
  148. strategy:
  149. matrix:
  150. wranglerVersion: ["3.114.12"]
  151. steps:
  152. - name: Get NPM cache directory path
  153. id: npm_cache_path
  154. shell: sh
  155. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  156. - uses: actions/cache@v6
  157. with:
  158. path: |
  159. ${{ steps.npm_cache_path.outputs.dir }}
  160. node_modules
  161. key: deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-${{ matrix.wranglerVersion }}
  162. restore-keys: |
  163. deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-
  164. - uses: actions/download-artifact@v8
  165. with:
  166. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  167. path: public
  168. - name: Prepare GitHub Pages deployment marker
  169. id: deploy_marker
  170. run: |
  171. echo "marker_content=${GITHUB_SHA}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}" >> $GITHUB_OUTPUT
  172. - name: Remove build artifact
  173. uses: geekyeggo/delete-artifact@v6
  174. background: true
  175. with:
  176. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  177. # Deploy to Cloudflare, GitLab, and GitHub in parallel
  178. - name: Deploy to Cloudflare Pages
  179. id: deploy_cloudflare
  180. uses: cloudflare/wrangler-action@v3
  181. background: true
  182. with:
  183. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  184. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  185. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  186. wranglerVersion: ${{ matrix.wranglerVersion }}
  187. - name: Upload Dist to GitLab
  188. id: deploy_gitlab
  189. background: true
  190. run: |
  191. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git-gitlab
  192. cd ./deploy-git-gitlab
  193. git config push.default matching
  194. git config user.email "${GITLAB_EMAIL}"
  195. git config user.name "${GITLAB_USER}"
  196. rm -rf ./*
  197. cp -rf ../public/* ./
  198. git add --all .
  199. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  200. git push --quiet --force origin HEAD:master
  201. cd ..
  202. rm -rf ./deploy-git-gitlab
  203. env:
  204. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  205. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  206. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  207. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  208. - name: Upload Dist to GitHub
  209. id: upload_dist_to_github
  210. background: true
  211. run: |
  212. gh repo unarchive SukkaLab/ruleset.skk.moe --yes
  213. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git-github
  214. cd ./deploy-git-github
  215. git config push.default matching
  216. git config user.email "${GH_EMAIL}"
  217. git config user.name "${GH_USER}"
  218. rm -rf ./*
  219. cp -rf ../public/* ./
  220. echo "ruleset-mirror.skk.moe" > CNAME
  221. echo "${{ steps.deploy_marker.outputs.marker_content }}" > deploy-check.txt
  222. git add --all .
  223. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  224. git push --quiet --force origin HEAD:master
  225. cd ..
  226. rm -rf ./deploy-git-github
  227. env:
  228. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  229. GH_USER: ${{ secrets.GIT_USER }}
  230. GH_TOKEN: ${{ secrets.GIT_TOKEN }}
  231. - wait: upload_dist_to_github
  232. - name: Wait for GitHub Pages to serve deployed marker
  233. if: steps.upload_dist_to_github.outcome == 'success'
  234. id: wait_for_github_pages
  235. run: |
  236. expected="${{ steps.deploy_marker.outputs.marker_content }}"
  237. marker_url="https://ruleset-mirror.skk.moe/deploy-check.txt"
  238. for _ in $(seq 1 30); do
  239. content=$(curl --fail --silent --show-error --location "${marker_url}" || true)
  240. if [ "${content}" = "${expected}" ]; then
  241. echo "GitHub Pages is serving the correct deploy marker"
  242. exit 0
  243. fi
  244. sleep 10
  245. done
  246. echo "Timed out waiting for GitHub Pages deployment"
  247. exit 1
  248. - name: Archive dist repo
  249. if: steps.wait_for_github_pages.outcome == 'success'
  250. run: |
  251. gh repo archive SukkaLab/ruleset.skk.moe --yes
  252. env:
  253. GH_TOKEN: ${{ secrets.GIT_TOKEN }}
  254. - wait-all: