main.yml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "$TMPDIR" -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. token: ${{ secrets.GIT_TOKEN }}
  43. - name: Setup build folder
  44. run: |
  45. mv previous-build-${{ github.run_id }}-${{ github.run_number }}/{.,}* ${{ steps.ramdisk.outputs.build_dir }}/
  46. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/.git ]; then
  47. echo ".git not found"
  48. exit 1
  49. fi
  50. rm -rf "${{ steps.ramdisk.outputs.build_dir }}/.git"
  51. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }}/List ]; then
  52. echo "List not found"
  53. exit 1
  54. fi
  55. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  56. - name: Get current date
  57. id: date
  58. run: |
  59. echo "date=$(date +'%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT
  60. echo "year=$(date +'%Y')" >> $GITHUB_OUTPUT
  61. echo "month=$(date +'%m')" >> $GITHUB_OUTPUT
  62. echo "day=$(date +'%d')" >> $GITHUB_OUTPUT
  63. echo "hour=$(date +'%H')" >> $GITHUB_OUTPUT
  64. echo "minute=$(date +'%M')" >> $GITHUB_OUTPUT
  65. echo "second=$(date +'%S')" >> $GITHUB_OUTPUT
  66. - name: Restore cache.db
  67. uses: actions/cache/restore@v4
  68. id: cache-db-restore
  69. with:
  70. path: |
  71. .cache
  72. 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 }}
  73. # If source files changed but packages didn't, rebuild from a prior cache.
  74. restore-keys: |
  75. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:${{ steps.date.outputs.minute }}:
  76. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }} ${{ steps.date.outputs.hour }}:
  77. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-${{ steps.date.outputs.day }}
  78. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-${{ steps.date.outputs.month }}-
  79. ${{ runner.os }}-v3-${{ steps.date.outputs.year }}-
  80. ${{ runner.os }}-v3-
  81. - run: pnpm install
  82. - run: pnpm run build
  83. env:
  84. PUBLIC_DIR: ${{ steps.ramdisk.outputs.build_dir }}
  85. - name: Pre-deploy check
  86. # If the public directory doesn't exist, the build should fail.
  87. # If the public directory is empty, the build should fail.
  88. run: |
  89. if [ ! -d ${{ steps.ramdisk.outputs.build_dir }} ]; then
  90. echo "public directory not found"
  91. exit 1
  92. fi
  93. if [ ! "$(ls -A ${{ steps.ramdisk.outputs.build_dir }})" ]; then
  94. echo "public directory is empty"
  95. exit 1
  96. fi
  97. if [ ! -f .BUILD_FINISHED ]; then
  98. echo ".BUILD_FINISHED not found"
  99. exit 1
  100. fi
  101. echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
  102. - uses: actions/upload-artifact@v4
  103. with:
  104. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  105. path: ${{ steps.ramdisk.outputs.build_dir }}
  106. if-no-files-found: error
  107. retention-days: 1
  108. compression-level: 4
  109. include-hidden-files: false
  110. - name: Cache cache.db
  111. if: always()
  112. uses: actions/cache/save@v4
  113. with:
  114. path: |
  115. .cache
  116. 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 }}
  117. diff_deployment_on_pr:
  118. if: github.ref != 'refs/heads/master'
  119. needs:
  120. - build
  121. name: Diff output
  122. runs-on: ubuntu-24.04-arm
  123. steps:
  124. - uses: actions/download-artifact@v4
  125. with:
  126. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  127. path: public
  128. - name: Diff
  129. run: |
  130. git clone --filter=tree:0 --no-tags https://github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git >/dev/null 2>&1
  131. cd ./deploy-git
  132. git fetch origin master >/dev/null 2>&1
  133. rm -rf ./*
  134. cp -rf ../public/* ./
  135. git --no-pager diff --minimal
  136. deploy_to_cloudflare_pages:
  137. needs:
  138. - build
  139. name: Deploy to Cloudflare Pages
  140. if: github.ref == 'refs/heads/master'
  141. runs-on: ubuntu-24.04-arm
  142. steps:
  143. - name: Get NPM cache directory path
  144. id: npm_cache_path
  145. shell: sh
  146. run: echo dir=$(npm config get cache) >> $GITHUB_OUTPUT
  147. - uses: actions/cache/restore@v4
  148. with:
  149. path: |
  150. ${{ steps.npm_cache_path.outputs.dir }}
  151. node_modules
  152. key: ${{ runner.os }}-${{ runner.arch }}-deploy-to-cloudflare-npm
  153. - uses: actions/download-artifact@v4
  154. with:
  155. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  156. path: public
  157. - name: Deploy to Cloudflare Pages
  158. uses: cloudflare/wrangler-action@v3
  159. with:
  160. apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  161. accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
  162. command: pages deploy public --project-name=sukkaw-ruleset --commit-dirty=true --branch=main
  163. wranglerVersion: '3.90.0'
  164. - uses: actions/cache/save@v4
  165. with:
  166. path: |
  167. ${{ steps.npm_cache_path.outputs.dir }}
  168. node_modules
  169. key: ${{ runner.os }}-${{ runner.arch }}-deploy-to-cloudflare-npm
  170. deploy_to_github_gitlab:
  171. needs:
  172. - build
  173. name: Deploy to GitHub and GitLab
  174. if: github.ref == 'refs/heads/master'
  175. runs-on: ubuntu-24.04-arm
  176. steps:
  177. - uses: actions/download-artifact@v4
  178. with:
  179. name: build-artifact-${{ github.sha }}-${{ github.run_number }}
  180. path: public
  181. - name: Upload Dist to GitLab
  182. continue-on-error: true
  183. run: |
  184. git clone --filter=tree:0 --no-tags https://${GITLAB_TOKEN_NAME}:${GITLAB_TOKEN}@gitlab.com/SukkaW/ruleset.skk.moe.git ./deploy-git
  185. cd ./deploy-git
  186. git config --global push.dgefault matching
  187. git config --global user.email "${GITLAB_EMAIL}"
  188. git config --global user.name "${GITLAB_USER}"
  189. rm -rf ./*
  190. cp -rf ../public/* ./
  191. git add --all .
  192. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  193. git push --quiet --force origin HEAD:master
  194. cd ..
  195. rm -rf ./deploy-git
  196. env:
  197. GITLAB_EMAIL: ${{ secrets.GITLAB_EMAIL }}
  198. GITLAB_USER: ${{ secrets.GITLAB_USER }}
  199. GITLAB_TOKEN_NAME: ${{ secrets.GITLAB_TOKEN_NAME }}
  200. GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
  201. - name: Upload Dist to GitHub
  202. continue-on-error: true
  203. run: |
  204. git clone --filter=tree:0 --no-tags https://${GH_USER}:${GH_TOKEN}@github.com/SukkaLab/ruleset.skk.moe.git ./deploy-git
  205. cd ./deploy-git
  206. git config --global push.default matching
  207. git config --global user.email "${GH_EMAIL}"
  208. git config --global user.name "${GH_USER}"
  209. rm -rf ./*
  210. cp -rf ../public/* ./
  211. echo "ruleset.skk.moe" > CNAME
  212. git add --all .
  213. git commit -m "deploy: https://github.com/SukkaW/Surge/commit/${GITHUB_SHA}"
  214. git push --quiet --force origin HEAD:master
  215. cd ..
  216. rm -rf ./deploy-git
  217. env:
  218. GH_EMAIL: ${{ secrets.GIT_EMAIL }}
  219. GH_USER: ${{ secrets.GIT_USER }}
  220. GH_TOKEN: ${{ secrets.GIT_TOKEN }}