main.yml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. name: Build
  2. on:
  3. push:
  4. branches:
  5. - master
  6. pull_request:
  7. schedule:
  8. - cron: "0 12 * * *"
  9. concurrency:
  10. group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  11. cancel-in-progress: true
  12. jobs:
  13. build:
  14. name: Build
  15. runs-on: ubuntu-24.04-arm
  16. steps:
  17. # - name: Tune GitHub-hosted runner network
  18. # # https://github.com/actions/runner-images/issues/1187
  19. # uses: smorimoto/tune-github-hosted-runner-network@v1
  20. - uses: actions/checkout@v4
  21. with:
  22. persist-credentials: false
  23. - uses: pnpm/action-setup@v4
  24. with:
  25. run_install: false
  26. - uses: actions/setup-node@v4
  27. with:
  28. node-version-file: ".node-version"
  29. cache: "pnpm"
  30. - name: Create RAM Disk for building
  31. id: ramdisk
  32. run: |
  33. BUILD_DIR=$(mktemp -d -p /dev/shm/ -t sukka-surge-public.XXXXXXXXXX)
  34. echo "Build dir created at $BUILD_DIR"
  35. echo "build_dir=$BUILD_DIR" >> $GITHUB_OUTPUT
  36. - name: Download Previous Build
  37. uses: actions/checkout@v4
  38. with:
  39. repository: SukkaLab/ruleset.skk.moe
  40. persist-credentials: false
  41. path: previous-build-${{ github.run_id }}-${{ github.run_number }}
  42. - name: Setup build folder
  43. run: |
  44. mv previous-build-${{ github.run_id }}-${{ github.run_number }}/{.,}* ${{ steps.ramdisk.outputs.build_dir }}/
  45. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/.git ]; then
  46. echo ".git not found"
  47. exit 1
  48. fi
  49. rm -rf "${{ steps.ramdisk.outputs.build_dir }}/.git"
  50. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/List ]; then
  51. echo "List not found"
  52. exit 1
  53. fi
  54. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  55. - name: Get current date
  56. id: date
  57. run: |
  58. echo "date=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
  59. echo "year=$(date +'%Y')" >> $GITHUB_OUTPUT
  60. echo "month=$(date +'%m')" >> $GITHUB_OUTPUT
  61. echo "day=$(date +'%d')" >> $GITHUB_OUTPUT
  62. echo "hour=$(date +'%H')" >> $GITHUB_OUTPUT
  63. echo "minute=$(date +'%M')" >> $GITHUB_OUTPUT
  64. echo "second=$(date +'%S')" >> $GITHUB_OUTPUT
  65. - name: Restore cache.db
  66. uses: actions/cache/restore@v4
  67. id: cache-db-restore
  68. with:
  69. path: |
  70. .cache
  71. 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 }}
  72. # If source files changed but packages didn't, rebuild from a prior cache.
  73. restore-keys: |
  74. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:
  75. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:
  76. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }}
  77. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-
  78. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-
  79. ${{ runner.os }}-v3-
  80. - run: pnpm install
  81. - uses: catchpoint/workflow-telemetry-action@v2
  82. with:
  83. comment_on_pr: false
  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@v4
  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@v4
  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-24.04-arm
  125. steps:
  126. - uses: actions/download-artifact@v4
  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://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. deploy_to_cloudflare_pages:
  139. needs:
  140. - build
  141. name: Deploy to Cloudflare Pages
  142. if: github.ref == 'refs/heads/master'
  143. runs-on: ubuntu-24.04-arm
  144. steps:
  145. - name: Get NPM cache directory path
  146. id: npm_cache_path
  147. shell: sh
  148. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  149. - name: Cache NPM cache
  150. uses: actions/cache@v4
  151. with:
  152. path: |
  153. ${{ steps.npm_cache_path.outputs.dir }}
  154. node_modules
  155. key: ${{ runner.os }}-${{ runner.arch }}-deploy-to-cloudflare-npm
  156. - uses: actions/download-artifact@v4
  157. with:
  158. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  159. path: public
  160. - name: Deploy to Cloudflare Pages
  161. uses: cloudflare/wrangler-action@v3
  162. with:
  163. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  164. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  165. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  166. wranglerVersion: '3.81.0'
  167. deploy_to_github_gitlab:
  168. needs:
  169. - build
  170. name: Deploy to GitHub and GitLab
  171. if: github.ref == 'refs/heads/master'
  172. runs-on: ubuntu-24.04-arm
  173. steps:
  174. - uses: actions/download-artifact@v4
  175. with:
  176. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  177. path: public
  178. - name: Upload Dist to GitLab
  179. continue-on-error: true
  180. run: |
  181. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  182. cd ./deploy-git
  183. git config --global push.dgefault matching
  184. git config --global user.email "${GITLAB_EMAIL}"
  185. git config --global user.name "${GITLAB_USER}"
  186. rm -rf ./*
  187. cp -rf ../public/* ./
  188. git add --all .
  189. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  190. git push --quiet --force origin HEAD:master
  191. cd ..
  192. rm -rf ./deploy-git
  193. env:
  194. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  195. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  196. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  197. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  198. - name: Upload Dist to GitHub
  199. continue-on-error: true
  200. run: |
  201. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git
  202. cd ./deploy-git
  203. git config --global push.default matching
  204. git config --global user.email "${GH_EMAIL}"
  205. git config --global user.name "${GH_USER}"
  206. rm -rf ./*
  207. cp -rf ../public/* ./
  208. echo "ruleset.skk.moe" > CNAME
  209. git add --all .
  210. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  211. git push --quiet --force origin HEAD:master
  212. cd ..
  213. rm -rf ./deploy-git
  214. env:
  215. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  216. GH_USER: ${{ secrets.GIT_USER }}
  217. GH_TOKEN: ${{ secrets.GIT_TOKEN }}