main.yml 9.0 KB

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