main.yml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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@v5
  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 install
  83. - run: pnpm run build
  84. env:
  85. PUBLIC_DIR: ${{ steps.ramdisk.outputs.build_dir }}
  86. - name: Pre-deploy check
  87. # If the public directory doesn't exist, the build should fail.
  88. # If the public directory is empty, the build should fail.
  89. run: |
  90. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }} ]; then
  91. echo "public directory not found"
  92. exit 1
  93. fi
  94. if [ ! "$(ls -A ${{ steps.ramdisk.outputs.build_dir }})" ]; then
  95. echo "public directory is empty"
  96. exit 1
  97. fi
  98. if [ ! -f .BUILD_FINISHED ]; then
  99. echo ".BUILD_FINISHED not found"
  100. exit 1
  101. fi
  102. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  103. - uses: actions/upload-artifact@v7
  104. with:
  105. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  106. path: ${{ steps.ramdisk.outputs.build_dir }}
  107. if-no-files-found: error
  108. retention-days: 1
  109. compression-level: 4
  110. include-hidden-files: false
  111. - name: Cache cache.db
  112. if: always()
  113. uses: actions/cache/save@v5
  114. with:
  115. path: |
  116. .cache
  117. 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 }}
  118. diff_deployment_on_pr:
  119. if: github.ref != 'refs/heads/master'
  120. needs:
  121. - build
  122. name: Diff output
  123. runs-on: ubuntu-slim
  124. steps:
  125. - uses: actions/download-artifact@v8
  126. with:
  127. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  128. path: public
  129. - name: Diff
  130. run: |
  131. 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
  132. cd ./deploy-git
  133. git fetch origin master >/dev/null 2>&1
  134. rm -rf ./*
  135. cp -rf ../public/* ./
  136. git --no-pager diff --minimal
  137. env:
  138. GH_USER: ${{ secrets.GIT_USER }}
  139. GH_TOKEN: ${{ secrets.GIT_TOKEN }}
  140. deploy_to_cloudflare_pages:
  141. needs:
  142. - build
  143. name: Deploy to Cloudflare Pages
  144. if: github.ref == 'refs/heads/master'
  145. runs-on: ubuntu-slim
  146. # matrix is a tricky way to define a variable directly in the action yaml
  147. strategy:
  148. matrix:
  149. wranglerVersion: ['3.114.12']
  150. steps:
  151. - name: Get NPM cache directory path
  152. id: npm_cache_path
  153. shell: sh
  154. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  155. - uses: actions/cache@v5
  156. with:
  157. path: |
  158. ${{ steps.npm_cache_path.outputs.dir }}
  159. node_modules
  160. key: deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-${{ matrix.wranglerVersion }}
  161. restore-keys: |
  162. deploy-to-cloudflare-npm-${{ runner.os }}-${{ runner.arch }}-wrangler-
  163. - uses: actions/download-artifact@v8
  164. with:
  165. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  166. path: public
  167. - uses: cloudflare/wrangler-action@v3
  168. with:
  169. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  170. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  171. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  172. wranglerVersion: ${{ matrix.wranglerVersion }}
  173. deploy_to_github_gitlab:
  174. needs:
  175. - build
  176. name: Deploy to GitHub and GitLab
  177. if: github.ref == 'refs/heads/master'
  178. runs-on: ubuntu-slim
  179. steps:
  180. - uses: actions/download-artifact@v8
  181. with:
  182. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  183. path: public
  184. - name: Upload Dist to GitLab
  185. continue-on-error: true
  186. run: |
  187. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  188. cd ./deploy-git
  189. git config --global push.dgefault matching
  190. git config --global user.email "${GITLAB_EMAIL}"
  191. git config --global user.name "${GITLAB_USER}"
  192. rm -rf ./*
  193. cp -rf ../public/* ./
  194. git add --all .
  195. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  196. git push --quiet --force origin HEAD:master
  197. cd ..
  198. rm -rf ./deploy-git
  199. env:
  200. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  201. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  202. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  203. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  204. - name: Prepare GitHub Pages deployment marker
  205. id: deploy_marker
  206. run: |
  207. echo "marker_file=deploy-check-${GITHUB_SHA}-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}.txt" >> $GITHUB_OUTPUT
  208. - name: Upload Dist to GitHub
  209. id: upload_dist_to_github
  210. continue-on-error: 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
  214. cd ./deploy-git
  215. git config --global push.default matching
  216. git config --global user.email "${GH_EMAIL}"
  217. git config --global user.name "${GH_USER}"
  218. rm -rf ./*
  219. cp -rf ../public/* ./
  220. echo "ruleset-mirror.skk.moe" > CNAME
  221. : > "${{ steps.deploy_marker.outputs.marker_file }}"
  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
  227. env:
  228. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  229. GH_USER: ${{ secrets.GIT_USER }}
  230. GH_TOKEN: ${{ secrets.GIT_TOKEN }}
  231. - name: Wait for GitHub Pages to serve deployed marker
  232. if: steps.upload_dist_to_github.outcome == 'success'
  233. id: wait_for_github_pages
  234. run: |
  235. marker_file="${{ steps.deploy_marker.outputs.marker_file }}"
  236. marker_url="https://ruleset-mirror.skk.moe/${marker_file}"
  237. for _ in $(seq 1 30); do
  238. if curl --fail --silent --show-error --location --head "${marker_url}" >/dev/null; then
  239. echo "GitHub Pages is serving ${marker_file}"
  240. exit 0
  241. fi
  242. sleep 10
  243. done
  244. echo "Timed out waiting for GitHub Pages deployment: ${marker_url}"
  245. exit 1
  246. - name: Archive dist repo
  247. if: steps.wait_for_github_pages.outcome == 'success'
  248. run: |
  249. gh repo archive SukkaLab/ruleset.skk.moe --yes
  250. env:
  251. GH_TOKEN: ${{ secrets.GIT_TOKEN }}
  252. remove_artifacts:
  253. needs:
  254. - deploy_to_cloudflare_pages
  255. - deploy_to_github_gitlab
  256. name: Remove Artifacts after Deployment
  257. runs-on: ubuntu-slim
  258. steps:
  259. - uses: geekyeggo/delete-artifact@v6
  260. with:
  261. name: build-artifact-${{ github.sha }}-${{ github.run_number }}